February 27, 2015 by Daniel P. Clark

Value Assignment and Deferment with Lambdas/Procs

When assigning a variable with a raw value the outcome is as you would expect.

Now while learning about assigning methods to variables I used to wonder if I was assigning the result value from the method, or simply pointing to the method to be run when the variable was called upon.  To show what happen I will use Time.now for the method being assigned to the value since it changes every second.

As you can see the variable x saved the value from Time.now and didn’t call the Time.now method when accessed later.  To show that this is a method being called I’ll show the same done from a block and a method.

As you can see a regular block and method run once when being assigned to a variable and the variable stores the value itself.  Well what if you wanted the variable to always get the value by running the method?

Deferred with Lambdas and Procs

When you assign either a lambda or a proc to a variable then it is available to execute later.  The one thing to note is that you will need to use either .[] or .call methods on the variable to execute the lambda/proc.

So now the code we want to run when we access the variable runs each time.  Each of these can also be assigned to a constant within a class.

With this you see the same rules apply.  But what I think is really cool is now you can see a class constant also work as a method with lambdas and procs.

Summary

This was a short one.  I only recently came across the ability to have constants be methods you could call.  I really like using lambda in this way.  I’ve decided to use lambda constants for internal methods in my PolyBelongsTo project.  So they are methods available in the project, but their purpose was for internal use.  Methods I’ve intended for more general use are standards methods and easily accessible.  The reason I chose to go about it this way is that it’s a bit unusual to see a constant lambda out in the wild.  So I felt that would be more of an intention indicator.  Yes I know; it defies the very meaning of the word constant.  I expect there may be some people who may be adamant against this kind of use of constants in open source.  You’re more than welcome to share why it’s important to you.  For me, I’m just a Ruby programmer enjoying the beauty and flexibility that Ruby gives me.

If you haven’t been using lambdas and procs yet then I believe that this shows you some of the value they provide.  There are times you need to defer work to be done.

Have fun!  Please feel free to comment, share, subscribe to my RSS Feed, and follow me on twitter @6ftdan!

God Bless!
-Daniel P. Clark

Image by David via the Creative Commons Attribution-NonCommercial-NoDerivs 2.0 Generic License.

#defer#deferred#lambda#proc#programming#ruby

5
Leave a Reply

avatar
1 Comment threads
4 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
2 Comment authors
Daniel P. ClarkKeith Bennett Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Keith Bennett
Guest
Keith Bennett

Nice article. I don’t think there’s anything un-constant about a lambda; the constant refers to the lambda, which does not change, not the value that the lambda produces/returns. Deciding whether a lambda should be assigned to a constant or implemented as just another instance method is an interesting question, though. I do like the constant idea in some cases, e.g. very short (0.5 to 1 liners) code fragments for helper type things such as formatters whose missions are not related to the main theme of the class. In some cases, I like using lambdas as nested functions; creating them inside… Read more »

Daniel P. Clark
Guest

Hi Keith! Thanks for your input! I enjoyed the presentation. I haven’t yet used lambdas nested within functions. I was thinking about writing one that did self recursion, but I haven’t found out yet if lambdas can call themselves. I did watch Jim Weirich’s “Y Not- Adventures in Functional Programming” talk a while back and I believe he did something like that so I’m under the impression that I can. On the style of usage I understand that ->(x){ puts x } is a more uniform way to follow the way many languages implement procs/lambdas (whatever the case may be).… Read more »

Keith Bennett
Guest
Keith Bennett

Hi, Dan. I’ve never tried calling a lambda recursively, but you got me curious. It turns out that it works, I guess because the code in the lambda definition is not executed until the lambda is called, and by that time, the identifier is accessible. Ruby [crayon-5cbb30c7ab41c125866717 class="ruby"] f = ->(n) do f.(n - 1) if n > 0 puts n end f.(3) # Output is: 1 2 3 1234567891011121314  [crayon-5cbb30c7ab41c125866717   class="ruby"] f = ->(n) do  f.(n - 1) if n > 0  puts nend f.(3) # Output is:123 [/crayon] I was there at Jim’s talk you referred to. Afterwards I told Jim about a… Read more »

Daniel P. Clark
Guest

I need to form a community here in the Front Royal and Winchester area. Arlington is such a late and long drive for me (90 minutes). I have been to a couple meetups out that way. The last one was a code review for the water fall puzzle demo. I showed up late soon before you headed out. I also went to the code day event in the Vienna area a year or two ago. You might remember I had a netbook, I didn’t know ‘spec testing at the time, and we hooked up and additional keyboard I had brought… Read more »

Keith Bennett
Guest
Keith Bennett

Sorry for not remembering you. I thought you looked familiar. 😉

Best of luck with the community building. I attended the Harrisonburg RUG a few years ago but I think they closed a year or so ago. There might be some people there looking to build community too.

Hopefully we’ll see you at Retro Ruby…