正如问题所问,它只是在嵌入表达式的返回值上调用 to_s
吗?
像这样?
"1, 2, #{1 + 2}" == "1, 2, " + 3.to_s
还是这里发生了更多事情?
最佳答案
是的,它在表达式的结果上调用 to_s
。试试吧。
:001> class Monkey
:002> def to_s
:003> "monkey"
:004> end
:005> end
=> nil
:006> m = Monkey.new
=> monkey
:007> "I am a #{m}"
=> "I am a monkey"
关于ruby - ruby 字符串中的 #{} 究竟做了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11955792/