DEV Community

loading...

Discussion on: Comparing function handles across languages

Collapse
jaredcwhite profile image
Jared White

Regarding Ruby, it's actually less confusing than you think. 😃 Lambas are procs. They have slightly different characteristics regarding required arguments and return behavior, but otherwise lambdas == procs. You can also pass lambdas to a method with an ampersand just like in your proc example. I also think the new ->(x, y) { x * y } syntax is cooler than the old lambda keyword.

In addition, you can directly call procs/lambdas with the call method, or the shorthand .(). For example say_hi = ->() { puts "Hi!" }; say_hi.(). You can also do this with any object that has a call method!

class MyClass
  def call(name)
    puts "Hi #{name}"
  end
end

obj = MyClass.new
obj.("Jared") # => Hi Jared
Enter fullscreen mode Exit fullscreen mode
Collapse
shalvah profile image
Shalvah Author • Edited

lambdas == procs

Thank you. Just looked it up and noticed that the docs actually list lambdas as a proc flavour. Frankly, I'm not sure if that makes it better or worse.🤣

I also think the new ->(x, y) { x * y } syntax is cooler than the old lambda keyword.

Hehe, it's the syntax I see in most places too, but I prefer lambda, as it's clearer. Same thing for the .() (and all the different ways you can call in Ruby—I favour .call().😄 But I did not know that it worked in the reverse too (replacing a user-defined .call() method. Interesting.