How #{} works


#1

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?


#2

François Montel wrote:

=> true

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

“#{ [“b”] }” == [“b”].to_s #=> true
[“b”].to_s == “b” #=> true
[“b”].to_s #=> “b”


#3

“#{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”]”


#4

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