Created
November 28, 2011 07:28
-
-
Save deepak/1399474 to your computer and use it in GitHub Desktop.
proc creating and order of defination of local variable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
default = Proc.new { { :foo => 12, :bar => "0.0.0.0/thank-you/#{encoded_time}" } } | |
encoded_time = "15_32" | |
begin | |
puts default.call | |
rescue | |
puts "#~ undefined local variable or method `encoded_time'" | |
puts "proc does not pick up the local variable defined after proc creation" | |
end | |
puts "" | |
puts "--------" | |
puts "" | |
default = Proc.new { { :foo => 12, :bar => "0.0.0.0/thank-you/#{encoded_time}" } } | |
puts default.call #{:foo=>12, :bar=>"0.0.0.0/thank-you/15_32"} | |
puts "but works now that the local variable is defined before creating the proc" | |
puts "" | |
puts "--------" | |
puts "" | |
puts "reassigning the lvar" | |
encoded_time = "16_32" | |
# proc enclosed over the variable but it takes up the changed value | |
puts default.call #{:foo=>12, :bar=>"0.0.0.0/thank-you/16_32"} | |
puts "" | |
puts "--------" | |
puts "" | |
default = Proc.new { { :foo => 12, :bar => "0.0.0.0/thank-you/#{@request_time}" } } | |
puts default.call #{:foo=>12, :bar=>"0.0.0.0/thank-you/"} | |
@request_time = "17_32" | |
puts default.call #{:foo=>12, :bar=>"0.0.0.0/thank-you/16_32"} | |
puts "but there is no such restriction on using ivars because undefined ivars show as nil not a undefined error" | |
puts "" | |
puts "--------" | |
puts "" | |
puts "reassigning the ivar" | |
@request_time = "18_32" | |
puts default.call #{:foo=>12, :bar=>"0.0.0.0/thank-you/18_32"} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment