From: Robert Klemme Date: 2007-05-27T20:35:02+09:00 Subject: Re: Introducing the "it" keyword On 27.05.2007 11:46, Robert Dober wrote: > On 5/27/07, Robert Klemme wrote: > >> >> >> > That is something I am surprised about, b/c the intention was to have >> > an idiom for >> > return it if h(x) < 10 >> >> No, it's "return it if it < 10" with "it" being "v+1" in this case. > Well that was the original idiom but we are looking for existing ones, > right? Yes, and that's why I presented what I presented. I am not sure I understand your point here. :-) >> > and >> > return it_test(h(x)) >> > really is not the same, right? >> >> It's not the same but I believe you misread the task. it_test is just a >> method to demonstrate the idiom in lines 2-4. > > Ah that's why you called the method test_..., not stupid at all ;) > Well I will get to understand what is going on here... > .. eventually... > .. potentially... > .. maybe... > .. hardly... Um... It's really not that difficult. I just used instance_eval because that will rebind "self" inside the block but retain the original binding afterwards. That way you get something like a "temporary variable" but without needing to manually make sure the reference is released after the test. Remeber that the problem with my first suggestion was that you had to add tmp=nil after the line with return in order to make sure the object would be released again. It is really just the built in way of doing what is known in other languages as "with": irb(main):001:0> def with(x,&b) x.instance_eval(&b) end => nil irb(main):002:0> with "" do irb(main):003:1* p length irb(main):004:1> p empty? irb(main):005:1> end 0 true => nil irb(main):009:0> with "aaaaaaaaa" do irb(main):010:1* scan(/a{1,3}/) {|x| p x} irb(main):011:1> end "aaa" "aaa" "aaa" => "aaaaaaaaa" irb(main):012:0> Hope that explains it. If not, let me know. Kind regards robert