Skip to content

Instantly share code, notes, and snippets.

@rewinfrey
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rewinfrey/9096272 to your computer and use it in GitHub Desktop.
Save rewinfrey/9096272 to your computer and use it in GitHub Desktop.
Determining if the closure of a lambda referencing an ivar is defined at time of creation, or time of calling.
class Creator
def initialize(input1)
@input1 = input1
end
def create_lambda
->() {
puts @input1
}
end
end
class Caller
def initialize(input1, creator)
@input1 = input1
@creator = creator
end
def call_lambda
@creator.create_lambda.call
end
end
creator = Creator.new("creator input")
caller = Caller.new("caller input", creator)
caller.call_lambda
# >> creator input
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment