views:

206

answers:

4

I'm having a hard time imagining a practical use for Procs and Lambdas in a web app. Does anyone have an example of a situation in your code where they come in handy?

+2  A: 

I use them on named scopes:

named_scope :last, lambda { |quantity| {:order => "created_at DESC", :limit=> quantity } }

To create an easy way to get the last NUMBER posts, comments, etc.

Now, I kinda cheated there, it's most of a framework thing than a "web app", but I can't think web app are that different from other desktop ones. Both Procs and Lambdas allows you to "save" code for later use, and can help to use behaviour, use more "domain oriented" methods and cleaning up some more "regular code"

Yaraher
+3  A: 

Why in webapps in particular? Do you mean the Ruby on Rails framework, perhaps?

Procs and lambdas are useful when you want to store pieces of code anonymously. Procs and lambdas are very similar to methods, except that they don't have a name. A good example to illustrate this, in ActiveRecord/Rails:

// methods are named
validates_presence_of :foo, :if => :active?

// procs aren't
validates_presence_of :foo, :if => proc {|r| do_stuff }

I general, procs are used for callbacks and hooks, so that you don't have to write named methods and refer to them, putting the code in question directly in the option hashes and such.

You might find this article useful.

August Lilleaas
Thanks for the link. No, I didn't mean ROR. I just meant everyday typical examples of where you could use procs/lambdas - such as in web applications. Sorry if I wasn't clear.
uzo
+1  A: 

Using lambda or proc is a matter of programming style called functional programming. There are several interfaces in Rails where you might like to pass a block object(lambda or procc), just like validations:

class Person < ActiveRecord::Base
  validates_acceptance_of :terms_of_service :if => Proc.new { |user| user.signup_step > 2 }
end

You can design some interfaces like that, where you can give the user the choice of either passing a method name(symbol) or a proc(or lambda) object to specify some logic.

Another place in Rails for example is where you can set layouts dynamically:

class WeblogController < ActionController::Base
  layout proc{ |controller| current_user.logged_in? ? "writer_layout" : "reader_layout" }
end
khelll
+1  A: 

Obviously, procs and lambdas are a necessary component of anything related to callbacks, but in most cases in ruby this can be accomplished with a block.

One place where procs or lambdas are useful that isn't related to callbacks is things like compiling a regular expression. Of course, regex are part of the language, so you don't need procs for this, but you may find yourself writing some other kind of procedure that has a one-time cost that can be reused, but isn't really worth creating a full blown class.

In my case, I had an object (publist) which contained a list of strings. In one area of the code, I needed to repeatedly check whether some other set had any members in that list. It would have been inconvenient and have poor performance to change the data type so that the object contained a set. So what I did was create a method that returned a matcher lambda that had a local set representation of this list.

I could have returned a set, but the business logic of whether an item matched logically belonged to the publist. I could in the future change this to support substrings or regular expressions and users of the matcher would still have the same api.

Kevin Peterson