From: "David A. Black" Date: 2007-03-26T07:31:54+09:00 Subject: Re: Suggestion: swap name of "puts" and "print" and rename "puts" to "put_s" Hi -- On 3/25/07, Brian Candler wrote: > > irb(main):006:0> $_ = "abcdef" > > => "abcdef" > > irb(main):007:0> puts "xxx" if /abc/ > > (irb):7: warning: regex literal in condition > > xxx > > => nil > > irb(main):008:0> puts "xxx" if /ghi/ > > (irb):8: warning: regex literal in condition > > => nil > > > > Ugh. So the rule which says "anything which is not 'nil' or 'false' is true" > > is broken in this construct, and this is just to make ruby more perlish. > > Hmm. Thinking about this a bit more: since this applies only to regexp > *literals*, > > do_something if /foo/ > > would not be of any use if it were semantically equivalent to > > do_something if true When I first saw this: irb(main):001:0> puts "yes" if a = 1 (irb):1: warning: found = in conditional, should be == I found it annoyingly micromanaging -- until I saw that it only happened with literals, and then I realized that it was being very shrewd. It's interesting, then, that this non-construct can be used to do the $_ thing. I wonder whether other cases, like if a = 1, are going to pressed into service :-) I hope not; I think the fact that the regex one warns you and then goes ahead and does something other than what it's warning you against suggests it could get pretty ugly. > So I started thinking along the lines of: instead of using global variable > $_ as a kind of temporary register to pass data between methods, how could > this construct be made more Rubyish? For example, like a case statement, > "if /foo/" could translate to "if /foo/ === self" ? > > But then I realised: perhaps this is all just a marketing scam. Maybe $_ and > friends exist in the language, not because they have any real use, but just > to sucker in Perl programmers. They see them there, and think "great, this > is like Perl, I'll give Ruby a try". Once they're hooked, they won't use > them any more than the rest of us do. > > If that's true, the construct clearly *has* to use $_ to fulfil its purpose > :-) I'd love to see some of the Perl stuff disappear, and it's perenially in the Ruby TODO file. I can hardly think of a case where I've seen anyone use $_ in Ruby. David -- Q. What is THE Ruby book for Rails developers? A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black) (See what readers are saying! http://www.rubypal.com/r4rrevs.pdf) Q. Where can I get Ruby/Rails on-site training, consulting, coaching? A. Ruby Power and Light, LLC (http://www.rubypal.com)