Search code examples
ruby-on-railsrubyproc-object

Why use Proc.new to call a method in a Rails callback?


in all the tutorials for RoR I see instances where the coder chose to use Proc.new when seemingly it is both unnecessary and rather unattractive.

Example, here is a callback for placed in a model, one using Proc.new the other presumably doing the same thing:

class Order < ActiveRecord::Base  
  before_save :normalize_card_number,  
    :if => Proc.new { |order| order.paid_with_card? }  
end

class Order < ActiveRecord::Base
  before_save :normalize_card_number, :if => "paid_with_card?"
end

So what's the difference? Why use the Proc? Don't they both call the "paid_with_card?" method?

Thanks in advance


Solution

  • In the example above, using a symbol for the conditional method would probably be the best choice.

    class Order < ActiveRecord::Base
      before_save :normalize_card_number, :if => :paid_with_card?
    end
    

    The string option uses eval to evaluate the Ruby code in the string. So personally, I would prefer to use a symbol if calling a method or a Proc if writing a short inline condition.

    Per the RailsGuides documentation:

    Using a Proc object gives you the ability to write an inline condition instead of a separate method. This option is best suited for one-liners.

    I think using a Proc might be better illustrated this way:

    class Order < ActiveRecord::Base
      before_save :normalize_card_number,
        :if => Proc.new { |order| order.payment_type == "card" }
    end
    

    This would possibly eliminate the need for the paid_with_card? method.