Skip to content

Instantly share code, notes, and snippets.

@simon-engledew
Created August 11, 2010 10:09
Show Gist options
  • Save simon-engledew/518775 to your computer and use it in GitHub Desktop.
Save simon-engledew/518775 to your computer and use it in GitHub Desktop.
ruby lambda in a different scope
class ApplicationController < ActionController::Base
def self.before_filter_in_instance(&block)
before_filter do |controller|
controller.instance_eval(&block)
end
end
# fails!
# params is not available
# this lambda will be executed in the class scope where it was defined
before_filter lambda { Rails.logger.info(params[:transaction_id]) }
# succeeds!
# params is available
# this lambda will be executed inside the instance of controller
before_filter_in_instance { Rails.logger.info(params[:transaction_id]) }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment