DEV Community

Passing Functions as Arguments in Ruby

Hal Friday on February 13, 2020

It can be done! As per my last blog post, with no further ado, here is how you do it: def first_option puts "space jam" end def second_opti...
Collapse
 
tomboul profile image
Tomboul • Edited

Great Hal Friday !!

I would add a small comment. This is when a function is called, we want to pass the function received as an argument

def first_option (arg)
    puts "space jam for #{arg}"
end

def second_option (arg)
    puts "dogs rule is #{arg}" 
end

def receives_function(func)
   treatFunction(method(func))
end

def treatFunction(callback)
  callback.call("music")
end

receives_function(:first_option)
Enter fullscreen mode Exit fullscreen mode
Collapse
 
ben profile image
Ben Halpern

Very well described. I did not understand this very well.

Collapse
 
k_penguin_sato profile image
K-Sato • Edited

Great post! Thank you!