[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

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

Rk Ch

2/1/2008 8:26:00 AM

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"]]
--
Posted via http://www.ruby-....

3 Answers

Thomas Preymesser

2/1/2008 3:10:00 PM

0

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

On 01/02/2008, Rk Ch <rollingwoods@gmail.com> 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. :)


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

Gary Wright

2/1/2008 3:56:00 PM

0


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


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"




Stefan Rusterholz

2/2/2008 10:18:00 AM

0

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. :)
>
>
> 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
--
Posted via http://www.ruby-....