From: Robert Klemme Date: 2007-11-20T21:45:59+09:00 Subject: Re: using lambda, binding happens immediately? 2007/11/19, Sam Larbi : > I have this code: > > > require 'test/unit' > require 'leapyear' > class LeapYearTest < Test::Unit::TestCase > def setup > @ly = LeapYear.new > end > def LeapYearTest.generate_tests > filename = "testdata.dat" > file = File.new(filename, "r") > file.each_line do |line| > year, is_leap = line.split; > code = lambda { assert_equal(is_leap.downcase=="true", @ly.isleap?( > year.to_i)) } > define_method("test_isleap_" + year, code) > end > file.close > end > end > > > What I want to know is why when I use define_method("name") {block;} instead > of define_method("name", lambda{block}) the binding seems to happen later > (after the each_line loop)? > > Is there a reason behind that? I'm not sure I understand you but why don't you just do define_method("test_isleap_" + year) do assert_equal(is_leap.downcase=="true", @ly.isleap?(year.to_i)) end Btw, I'd also use the block form of File.open because it is more robust than the idiom you are using. Kind regards robert -- use.inject do |as, often| as.you_can - without end