DEV Community

es404020
es404020

Posted on

2 2

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)

Sentry workshop image

Sick of your mobile apps crashing?

Let Simon Grimm show you how to fix them without the guesswork. Join the workshop and get to debugging.

Save your spot →

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay