DEV Community

es404020
es404020

Posted on

Methods in Ruby

Methods or Function in ruby allows us to put our code in a container and call it when necessary. This allows reusability and prevent duplication ,take for instance we want multiply 3 different pairs of number 9 time we would do this

puts "#{3 * 4}"
puts "#{1 * 0}"
puts "#{8 * 2}"
puts "#{7 * 6}"
.....

Enter fullscreen mode Exit fullscreen mode

This would be a very long and repetitive process but with methods we can do this.

def multiply(a,b)
    a*b
end
multiply(3,4)
multiply(1,0)


Enter fullscreen mode Exit fullscreen mode

This is easier to maintain and be used by anyone

Top comments (0)