Problem: Array.to_s return [["..."]] in 1.9.0

I use Array.to_s convert to string. Ruby 1.9.0 return like [[“sample
string”]], but 1.8.6 return only the value.
How to resolve this problem? I really don’t want to revise so many lines
of code. :slight_smile:

Here’s the sample code :

text=“Source String line…”
d_lo=text.scan(/Str(.*?)g/).to_s
puts d_lo

1.8.6 return:in
1.9.0 return:[[“in”]]

On 01/02/2008, Rk Ch [email protected] wrote:

I use Array.to_s convert to string. Ruby 1.9.0 return like [[“sample
string”]], but 1.8.6 return only the value.
How to resolve this problem? I really don’t want to revise so many lines
of code. :slight_smile:

you can define your own version of Array#to_s which emulates the
behaviour
of Ruby 1.8

class Array
def to_s
…your code here…
end
end

-Thomas

On Feb 1, 2008, at 3:25 AM, Rk Ch wrote:

I use Array.to_s convert to string. Ruby 1.9.0 return like [[“sample
string”]], but 1.8.6 return only the value.
How to resolve this problem? I really don’t want to revise so many
lines
of code. :slight_smile:

One option is to switch to Array#join, which also works for nested
arrays:

[1,2,3].join
=> “123”

[1,2,[3,4],5].join
=> “12345”

Rk Ch wrote:

I use Array.to_s convert to string. Ruby 1.9.0 return like [[“sample
string”]], but 1.8.6 return only the value.
How to resolve this problem? I really don’t want to revise so many lines
of code. :slight_smile:

Here’s the sample code :

text=“Source String line…”
d_lo=text.scan(/Str(.*?)g/).to_s
puts d_lo

1.8.6 return:in
1.9.0 return:[[“in”]]

Why do you use scan if you don’t really want to scan?
text=“Source String line…”
d_lo=text[/Str(.*?)g/, 1]
puts d_lo

I didn’t test it on 1.9, but it should work the same.

Regards
Stefan