From: Robert Klemme Date: 2007-05-26T17:30:03+09:00 Subject: Re: Introducing the "it" keyword On 25.05.2007 11:40, Robert Klemme wrote: > On 25.05.2007 10:21, Greg Fodor wrote: >>> And don't tell me that this is too much typing for you. >> Well, it is kinda, but that's not as bad as the fact that >> 'it' will be un Gc-able till the end of the block unless >> we do: >> >> it = v+1 >> return it if it < 10 >> it = nil >> >> It's reminiscent of the all too common ruby memory 'leak': >> (http://whytheluckystiff.net/articles/theFullyUpturnedBin.html) >> >> x = [1, 2, 3] >> loop do >> # add some crap to x and use it >> end >> >> slow_method_that_doesnt_use_x() >> >> This problem is even trickier w/ closures grabbing x into their >> binding too AFAIK. "it" needs to show "it"self to the door >> when "it"'s no longer wanted. (It's late) > > I see your point. I do wonder though how relevant this is in practice. > If you write 100+ lines methods then you have other problems than a few > objects that are kept around longer than needed. Even with short > methods that exhibit the behavior you described above it is only an > issue if the object kept around keeps a lot of memory from being > reclaimed (which is not the case for the example above) or if the method > is active multiple times. After all the cost of changing the language > should be smaller than the cost incurred by this missing feature. Btw, here is a solution that does not need new syntax and does not exibit memory issues (I think): irb(main):001:0> def it_test(v) irb(main):002:1> (v+1).instance_eval do irb(main):003:2* return self if self < 10 irb(main):004:2> end irb(main):005:1> "nothing" irb(main):006:1> end => nil irb(main):007:0> it_test 0 => 1 irb(main):008:0> (0..10).map {|i| it_test i} => [1, 2, 3, 4, 5, 6, 7, 8, 9, "nothing", "nothing"] Kind regards robert