[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

How #{} works

François Montel

3/22/2009 1:54:00 PM

Can anyone explain this?

>> a = ["b"]
=> ["b"]
>> a == "b"
=> false
>> "#{a}" == "b"
=> true

Since a is an array, how come the last statement evaluates as true?
3 Answers

Sebastian Hungerecker

3/22/2009 1:55:00 PM

0

=46ran=E7ois Montel wrote:
> >> a =3D ["b"]
>
> =3D> ["b"]
>
> >> a =3D=3D "b"
>
> =3D> false
>
> >> "#{a}" =3D=3D "b"
>
> =3D> true
>
> Since a is an array, how come the last statement evaluates as true?

"#{ ["b"] }" =3D=3D ["b"].to_s #=3D> true
["b"].to_s =3D=3D "b" #=3D> true
["b"].to_s #=3D> "b"

-lim-

3/22/2009 2:14:00 PM

0

> >> "#{a}" == "b"
>
> => true
>
> Since a is an array, how come the last statement evaluates as true?

You could try ruby 1.9, if you prefer false:

> a = ["b"]
=> ["b"]
> "#{a}" == "b"
=> false
> "#{a}"
=> "[\"b\"]"
> ["b"].to_s
=> "[\"b\"]"

Paganoni

3/22/2009 7:00:00 PM

0

le 22/03/2009 14:53, François Montel nous a dit:
> Can anyone explain this?
>
>>> a = ["b"]
> => ["b"]
>>> a == "b"
> => false
>>> "#{a}" == "b"
> => true
>
> Since a is an array, how come the last statement evaluates as true?

Because, since it's embedded in a string Ruby does a .to_s against the
array. And ["b"].to_s == "b" is true