Hi --

On Mon, 24 Nov 2008, SASADA Koichi wrote:

> David A. Black wrote:
>>>> def m(a,b="b",c="c",d); p [a,b,c,d]; end
>> => nil
>>>> m(1,2,3)
>> [1, 2, "c", 3]
>> => [1, 2, "c", 3]
>>
>> I would expect [1, 3, "c", 2], because I would expect d (a required
>> argument) to be handled before b (an optional argument).
>>
>> I know it's almost unthinkable that one would use this method
>> signature, but I'd still like to understand the reasoning fully. I
>> thought the idea was: handle the required arguments first, but it
>> seems to be: move from left to right, looking ahead at every point to
>> see whether there are enough arguments left and, at that point, give
>> the right-hand required arguments priority. Is that right?
>
> class C
>  def []=(a, b=:x, *c, d)
>    p [[a, b, c], d]
>  end
> end
>
> c = C.new
> c[1] = 2
> c[1, 2] = 3
> c[1, 2, 3] = 4
> c[1, 2, 3, 4] = 5
>
>
> #=>
> [[1, :x, []], 2]
> [[1, 2, []], 3]
> [[1, 2, [3]], 4]
> [[1, 2, [3, 4]], 5]
>
> main purpose of post argument is to get rhs (right hand side) value
> at arrey assignedment like method.  On an above example, variable
> "d" is rhs value.

Interesting. I wish I'd known that yesterday (I did a presentation on
this), but now I know, and I can say something about it in my book.
Thanks.


David

-- 
Rails training from David A. Black and Ruby Power and Light:
   Intro to Ruby on Rails  January 12-15   Fort Lauderdale, FL
   Advancing with Rails    January 19-22   Fort Lauderdale, FL *
   * Co-taught with Patrick Ewing!
See http://www.rubypal.com for details and updates!