InterviewSolution
Saved Bookmarks
| 1. |
What is lambda ruby? |
|
Answer» Lambda is an anonymous FUNCTION in ruby, which is also be considered an object in Ruby. You will initialize a lambda and execute it by calling ‘call’ on the initialized VARIABLE. The following example will explain how to initialize and call lambda. //----start code-------- l = lambda { "Hello World" } PUTS l.call > Do or do not //----end code--------You can also pass arguments to a Lambda. The following example gives a basic understanding of lambdas with arguments. //----start code-------- l = lambda do |string| if string == "try" return "There's no such thing" else return "Do or do not." end end puts l.call("try") >There's no such thing //----end code--------You can define a lambda either with do..end or {}. As PER convention followed in Ruby, {} for single line lambdas and do..end for multiline lambdas. |
|