"David A. Black" <dblack / wobblini.net> schrieb im Newsbeitrag
news:Pine.LNX.4.61.0501101703450.10841 / wobblini...
> Hi --
>
> On Tue, 11 Jan 2005, itsme213 wrote:
>
> >
> > "David A. Black" <dblack / wobblini.net> wrote
> >
> >> rather than simply
> >> asking an object to do something at the moment you want it to do that
> >> thing, you're introducing a wrapper/notation mechanism
> >
> > Not always true. Below I've dropped many of the ':' as I think they
are not
> > syntactically essential.
> >
> >    [x,y] = obj
> > is the same as
> >    x = obj[0]
> >    y = obj[1]
> > So I ask obj to 'do' its [0], [1] right where I want it. Variables are
bound
> > to results.
>
> Hmmm...
>
>    irb(main):004:0> obj = [1,2]
>    => [1, 2]
>    irb(main):005:0> [x,y] = obj
>    SyntaxError: compile error
>
> Or did you mean: x,y = obj ?  I don't think I'd characterize it as obj
> "doing its [0], [1]".  You're not sending messages to obj -- not even
> the message #[].  What happens in this scenario depends on assignment
> semantics, not message semantics.

I'd say methods are invoked:

class Foo
  include Enumerable
  def initialize(x) @x=x end
  def each(&b) p "EACH"; @x.times(&b) end
  def to_a() p "TO_A"; super end
end

>> f = Foo.new 5
=> #<Foo:0x1016f950 @x=5>
>> a,b,c = *f
"TO_A"
"EACH"
=> [0, 1, 2, 3, 4]
>> a
=> 0
>> b
=> 1
>> c
=> 2

Now I'm making things more complicated: generic Enumerables and Arrays are
treated differently in this assignment context:

>> a,b,c = [0,1,2,3,4]
=> [0, 1, 2, 3, 4]
>> a
=> 0
>> b
=> 1
>> c
=> 2

>> a,b,c = *[0,1,2,3,4]
=> [0, 1, 2, 3, 4]
>> a
=> 0
>> b
=> 1
>> c
=> 2

>> a,b,c = f
=> [#<Foo:0x1016f950 @x=5>]
>> a
=> #<Foo:0x1016f950 @x=5>
>> b
=> nil
>> c
=> nil

>> a,b,c = *f
"TO_A"
"EACH"
=> [0, 1, 2, 3, 4]
>> a
=> 0
>> b
=> 1
>> c
=> 2

For arrays the star is added implicitely while for generic enumerables
it's not.  That's might be a reason to do away with this implicit
behavior - at least for me.

(In Ruby 1.8.1 that is)

Kind regards

    robert