Robert Klemme
2/26/2009 8:33:00 AM
2009/2/26 minilith@gmail.com <minilith@gmail.com>:
>> > I'm pretty sure that implicit conversions to array use to_ary, not to_=
a.
>> > This is the way it works in Ruby 1.8, and after an experimental period=
in
>> > 1.9 with to_splat
>
> Building on David Black's examples, I tried the following to clarify
> the use of #to_a and #to_ary for me.
>
>> RUBY_VERSION
> =3D> "1.9.1"
>> o =3D Object.new
> =3D> #<Object:0x100af964>
>> class << o
>> =A0 =A0 def to_a
>> =A0 =A0 =A0 =A0 [:a]
>> =A0 =A0 end
>> =A0 =A0 def to_ary
>> =A0 =A0 =A0 =A0 [:ary]
>> =A0 =A0 end
>> end
>> [*o]
> =3D> [:a]
>> [1] + o
> =3D> [1, :ary]
>
> The #to_splat method seems to be gone.
Here's another test set
09:31:50 OPSC_Gold_bas_dev_R2.0_foundation$ ruby /tmp/o.rb
"1.8.7"
1
2
3
"--------------"
#<Object:0x1002dfcc>
nil
nil
"--------------"
5
6
7
"--------------"
5
6
7
"=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D"
1
2
3
"--------------"
error
error
5
6
7
"--------------"
09:31:55 OPSC_Gold_bas_dev_R2.0_foundation$ ruby19 /tmp/o.rb
"1.9.1"
1
2
3
"--------------"
#<Object:0x1001a3dc>
nil
nil
"--------------"
#<Object:0x1001a378>
nil
nil
"--------------"
5
6
7
"=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D"
1
2
3
"--------------"
error
error
error
09:31:57 OPSC_Gold_bas_dev_R2.0_foundation$ cat /tmp/o.rb
p RUBY_VERSION
def t(a,b,c)
p a,b,c, '--------------'
end
o =3D Object.new
def o.to_a
[1,2,3]
end
q =3D Object.new
def q.to_ary
[5,6,7]
end
a, b, c =3D *o
p a,b,c, '--------------'
a, b, c =3D o
p a,b,c, '--------------'
a, b, c =3D *q
p a,b,c, '--------------'
a, b, c =3D q
p a,b,c, '=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D'
t(*o) rescue puts "error"
t(o) rescue puts "error"
t(q) rescue puts "error"
t(*q) rescue puts "error"
09:32:04 OPSC_Gold_bas_dev_R2.0_foundation$
Cheers
robert
--=20
remember.guy do |as, often| as.you_can - without end