How #{} works

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?

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”

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

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs