Possible Duplicate:
Ruby 1.9 Array.to_s behaves differently?
I wonder if anyone can tell me what changed between Ruby 1.8.7 and Ruby 1.9.3. I have an example listed below that behaves totally different in the 2 versions but according to the Ruby docs it doesn't appear anything changed between these versions.
Ruby 1.8
number = '123-45-6789'
# => "123-45-6789"
number.scan(/[0-9]/)
# => ["1", "2", "3", "4", "5", "6", "7", "8", "9"]
number.scan(/[0-9]/).to_s
# => "123456789"
Ruby 1.9
number = '123-45-6789'
# => "123-45-6789"
number.scan(/[0-9]/)
# => ["1", "2", "3", "4", "5", "6", "7", "8", "9"]
number.scan(/[0-9]/).to_s
# => "[\"1\", \"2\", \"3\", \"4\", \"5\", \"6\", \"7\", \"8\", \"9\"]"
Not really looking for a different way to do this just curious as to what changed between the two versions.
Here's what is actually in Ruby source code:
1.8.7:
rb_ary_to_s(ary)
VALUE ary;
{
if (RARRAY(ary)->len == 0) return rb_str_new(0, 0);
return rb_ary_join(ary, rb_output_fs);
}
In other words, in 1.8.7, to_s calls join.
1.9.3:
rb_ary_inspect(VALUE ary)
{
if (RARRAY_LEN(ary) == 0) return rb_usascii_str_new2("[]");
return rb_exec_recursive(inspect_ary, ary, 0);
}
VALUE
rb_ary_to_s(VALUE ary)
{
return rb_ary_inspect(ary);
}
In other words, in 1.9.3, to_s delegates to inspect.
Note: in future, if you're wondering about a difference you're observing between two versions, you can try taking a look at source code. Easy to pull down from here: https://github.com/ruby/ruby
Not everything is going to be easy to find in there of course, but if you search around for a bit you can often find good leads. In this case, array.c has what you're looking for.
Then you can switch back and forth between versions by issuing git checkout ruby_1_8_7 and git checkout ruby_1_9_3.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With