Hi --

On Mon, 2 Nov 2009, Brian Candler wrote:

> Thomas Sevestre wrote:
>>
>> The following script doesn't work...
>> Do you have any idea of what is wrong?
>
> There are loads of sub! calls in date/format.rb, but I have no idea why
> delegating sub! in this way would break it.
>
> I note that the same error occurs in 1.8.6 too:
> ArgumentError: argument out of range
>  from /usr/lib/ruby/1.8/time.rb:184:in `local'
>  from /usr/lib/ruby/1.8/time.rb:184:in `make_time'
>  from /usr/lib/ruby/1.8/time.rb:243:in `parse'
>  from (irb):19
>
> I copied time.rb to my local directory and modified it to puts the
> arguments to local. I see that it is calling
>
> Time.local(1999, 10, 31, 16, 46, 50, 0)
>
> before the sub! change, but
>
> Time.local(2009,0,1,0,0,0,0)
>
> after it. Most bizarre.

I believe it's about the $1, $2... variables:

"abc".sub!(/(.)/, "z")
p $1                          # nil
"abc".old_sub!(/(.)/, "z")
p $1                          # "a"

When sub! calls old_sub!, the $n variables inside sub! are not set,
but the ones in old_sub! are. When the date parser hits _parse_iso (or
whichever of that family of methods it hits), there are calls to sub!
followed by usage of the $n variables, which are actually not set
because they're two methods removed.

A more generic example:

def y(str)
   /(.)/.match(str)
   p $1
end

def x(str)
   y(str)
   p $1
end

x("abc")

   =>   "a"
        nil


David

-- 
The          Ruby training with D. Black, G. Brown, J.McAnally
Compleat     Jan 22-23, 2010, Tampa, FL
Rubyist      http://www.thecompleatrubyist.com

David A. Black/Ruby Power and Light, LLC (http://www.rubypal.com)