From: Robert Klemme Date: 2007-07-15T08:40:10+09:00 Subject: Re: gets On 15.07.2007 00:52, Robert Dober wrote: > On 7/14/07, Chad Perrin wrote: >> On Sat, Jul 14, 2007 at 09:16:35PM +0900, Robert Dober wrote: > >> I wasn't even aware that "foo = gets; foo.chomp!" would be more efficient >> than "foo = gets.chomp" -- that's news to me, and I'm not sure I get why >> that should be the case at the moment. > > My bad, I thought it was > foo = foo.chomp vs. foo.chomp! > sorry Actually you can have foo = gets.chomp as well as foo = gets foo.chomp! as well as foo = gets.chomp! I usually do ARGF.each do |line| line.chomp! ... end Which probably also explains why I use chomp!. :-) With regard to premature optimization: I just made it a habit to use chomp! in this case because I dislike creating an object that is immediately thrown away when I can avoid it easily. I'm not religious here - it's just my personal rule of thumb. As I said "inplace modification" is nothing special, it's just the normal OO way of doing things: object state changes. Maybe people tend to forget that String and Fixnum are objects just like any other object (ok Fixnum is immutable and immediate but that's about it) and maybe that's the reason why they feel that inplace modification is special or bad. Dunno. Chad mentioned issues that can arise in complex code from inplace modification and took that as his personal guideline to avoid it. I have a different personal guideline... Kind regards robert