From: Greg Fodor Date: 2007-05-26T02:55:08+09:00 Subject: Re: Introducing the "it" keyword > > $_ = "foo" > > puts $_ if ($_ = "bob" && chomp) > > > will print "foo"! > > Which has nothing to do with $_ or chomp using $_ behind the scenes. > It's a problem with operator precedence. I disagree. Using 'it' here would avoid worries about side-effects. Anyway, I thought ruby was moving away from these implicit magic $* variables? Using pipes, the assignment to it would be explicit and safe from side effects. > for () { > return $_ if $_ > 45 and f($_) and whatever( 'with', $_) > } > > the difference from > > $_ = ); > return $_ if $_ > 45 and f($_) and whatever( 'with', $_) > > being that the for(){} localizes the change of $_. Right, this is kinda sorta the pattern I am trying to kill with "it", except it is less verbose. In addition, you still have side effects on $_ as above, you'd generally have to grab $_ into a local (with a name) if you were going to use methods that *could* change it. > This one already works. No changes to ruby needed. NameError: undefined local variable or method `x' for main:Object One thing to mention. Coming up with examples that come close to the semantics of 'it' (without exact semantics) is missing the point. If these other solutions were optimal people would be using them already, but as far as I've seen lots of ruby code has the pattern implemented lazily. There are really two genuine criticisms to 'it' I can see based upon this thread: - The syntax is too confusing - It is not worth polluting the language over, despite its utility Upon reflection, unless I am mistaken, there *is* something close with correct semantics, a la the references to 'let' and 'for' in Scheme and Perl. It seems kind of odd nobody has given this one before, since it is pretty simple and I feel dumb for not mentioning it until now. def with(x) yield x end with (v + 1) { |it| return it if it < 5 } Personally, I think this is the closest we can get in Ruby, and can serve as a concrete starting point for what we gain by introducing a keyword. Based upon this 'it' now officially gains sugar and performance, since this above example can match the same semantics and side effects. Why it is good sugar: - Less code. The value for 'it' moves much closer to its usage. - Less explicit naming. If 'it' becomes the default name for the block parameter and also is set during end of line conditionals, ruby code will be less polluted with throwaway user variable names. (This is a pet peeve of mine, why do I have to keep saying x,y,z and my co-worker says a,b,c? If we just use 'it', its consistent.) - It will result in ruby idioms that are clean and correct. If the above example works go great, why aren't people using it? Probably because it's a lot of extra typing for perceived little gain, even though it is the most 'correct' implementation. I feel like this common pattern should be supported in the most terse form possible. Why it is good for performance: - We save a full block invocation and binding opposed to the above, I think. I don't know enough about the interpreter to be sure of this, though. Also, even if 'it' was just added as the default first name, you could get: def with (x); yield x; end; with (v + 1) { puts it if it < 5 } This might be a good compromise!