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

R

Rk Ch

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. :)


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"]]
 
T

Thomas Preymesser

[Note: parts of this message were removed to make it a legal post.]

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. :)


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
 
G

Gary Wright

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. :)


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"
 
S

Stefan Rusterholz

Rk said:
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. :)


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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

return in iterator in lambda 5
Chatbot 0
BotSavesPrincess Problem 5
debugger problem in 1.9? 1
Big problem I need to solve with some unix utils 1
Collect Excel Data from Website 5
TF-IDF 2
return statement 14

Members online

Forum statistics

Threads
474,282
Messages
2,571,404
Members
48,095
Latest member
WalkerBore

Latest Threads

Top