Ruby Methods
Ruby Methods
The Ruby methods syntax and example.
A Ruby method consists of:
The def keyword
Method name
The body of the method
Return value
The end keyword
Methods syntax
def method_name -- Ruby code end --or def method_name(value_1, value_2) -- Ruby code end
Methods example
def test_method a = 1 b = 2 c = 3 d = "xyz" return a, b, c, d end puts test_method
Output
1
2
3
xyz
Method with Arguments example
def test_method(x,y) x+y end puts test_method(1,2)
Output
3
Method with Arguments example
def test_method(id = 123, name = "Ruby") puts "The id is: #{id}; The name is: #{name};" end test_method
Output
The id is: 123; The name is: Ruby;