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
"=============="
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
"=============="
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 = Object.new
def o.to_a
[1,2,3]
end
q = Object.new
def q.to_ary
[5,6,7]
end
a, b, c = *o
p a,b,c, '--------------'
a, b, c = o
p a,b,c, '--------------'
a, b, c = *q
p a,b,c, '--------------'
a, b, c = q
p a,b,c, '=============='
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
···
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
=> "1.9.1"
o = Object.new
=> #<Object:0x100af964>
class << o
def to_a
[:a]
end
def to_ary
[:ary]
end
end
[*o]
=> [:a]
[1] + o
=> [1, :ary]
The #to_splat method seems to be gone.
--
remember.guy do |as, often| as.you_can - without end