Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

# 7.1 Lambdas In Ruby Lambdas ======= You may have heard of lambdas before.

Perhaps you've used them in other language s. Despite the fancy name, a lambda is just a function... peculiarly... without a name. They're anonymous, little functional spies sneaking into the rest of you r code. Lambdas in Ruby are also objects, just like everything else! The last ex pression of a lambda is its return value, just like regular functions. As boring and familiar as that all sounds, it gives us a lot of power. As objects, lambdas have methods and can be assigned to variables. Let's try it! Example Code: l = lambda { "Do or do not" } puts l.call Cool. Notice that our anticipatorily apologetic string is the return value of th e lambda which we see by printing it using puts. Now, while this is a lovely str ing, perhaps we'd like to return something more interesting. Lambdas take parame ters by surrounding them with pipes. Example 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") # Feel free to experiment with this Even cooler. Note that we replaced the {} that wrapped the lambda with do..end. Both work equally well, but the convention followed in Ruby is to use {} for sin gle line lambdas and do..end for lambdas that are longer than a single line. Now go ahead and add a lambda to the following code which increments any number passed to it by 1. Increment = lambda { |input| input + 1 } # your lambda between the braces # 7.2 Blocks in Ruby Lambdas vs. Blocks ================== A lambda is a piece of code that you can store in a variable, and is an object. The simplest explanation for a block is that it is a piece of code that can't be stored in a variable and isn't an object. It is, as a consequence, significantl y faster than a lambda, but not as versatile and also one of the rare instances where Ruby's "everything is an object" rule is broken. As with most things in programming, there is more to this story, but blocks are an advanced topic so lets keep things simple for now. If you're interested in st udying blocks in more detail, take a look at our chapter on blocks in our book " Ruby Primer: Ascent" which addresses intermediate and advanced topics. Let's look at an example of a block that does the same thing as the lambda you w rote in the previous exercise. Example Code: def demonstrate_block(number) yield(number)

end puts demonstrate_block(1) { |number| number + 1 } Let's list the ways in which this example is different from the last exercise th at we did when learning about lambdas. There's no lambda. There's something new called yield. There's a method that has the body of a lambda immediately after the paramet er list, which is a little weird. Skipping the details ==================== It turns out that one of the most common uses for a lambda involves passing exac tly one block to a method which in turn uses it to get some work done. You'll se e this all over the place in Ruby - Array iteration is an excellent example. Ruby optimizes for this use case by offering the yield keyword that can call a s ingle lambda that has been implicitly passed to a method without using the param eter list. If you'll review the example in the previous section, you'll notice the same pat tern - one method, one block passed to it outside of the parameter list, and wit h the block called using yield. Now for some practice. Using what you've learned from earlier examples and exerc ises, make the tests pass. def calculate(a, b) yield(a, b) end calculate(2, 3) { |a, b| a + b } returns 5 calculate(2, 3) { |a, b| a - b } returns -1 calculate(2, 3) { |a, b| a * b } returns 6

You might also like