From: Robert Klemme Date: 2004-12-02T21:27:43+09:00 Subject: Re: singleton methods : when are they not permitted? "Brian Schr�der" schrieb im Newsbeitrag news:20041202123358.431849d2@black.wg... > On Thu, 2 Dec 2004 19:12:43 +0900 > "Robert Klemme" wrote: > > > > I prefer to view a literal as a special kind of expression that yields an > > instance on each execution. Whether this is always the same instance (as > > for symbols, Fixnum and surprisingly also for Bignum and Regexp) or > > whether it's a different instance (as for strings, arrays and hashes) > > depends on the literal. > > That regexps are always the same surprised me also, but it is nice. So it is > not optimization to extract a regexp into a constant instead of writing it in > the loop. Except you need some interpolation. :-) > (Btw: Always when I'm trying to optimze something in ruby, it gets slower. Ruby > follows exactly my mindset when programming, and when I try to change my mind > on how the computer actually works it out, I'm running in the wrong > direction.;) :-) > irb(main):005:0> 2.times do |i| puts /abc/.id end > 538420744 > 538420744 > => 2 > > But it can't always be true > > irb(main):006:0> 2.times do |i| puts /abc#{i}/.id end > 538396754 > 538396704 > => 2 > > Or maybe one should clarify it such, that a regexp with interpolation is no > longer a literal. But >> 2.times do |i| puts /abc#{i}/o.id end 135012888 135012888 => 2 Note the usage of flag "o". Kind regards robert