Ruby如何用双引号("")与String.new?初始化新字符串有什么不同?出于好奇和实验的目的,我覆盖了String#initialize:
class String
def initialize
puts "I <3 bananas" # they're delicious!
end
end
Run Code Online (Sandbox Code Playgroud)
我想弄清楚的是:为什么这两个例子不同?
# Calling the String class directly, I can declare banana love!
irb(main):054:0> String.new
I <3 bananas
=> ""
# Using double quotes, this string is not as tasty :(
irb(main):055:0> ""
=> ""
Run Code Online (Sandbox Code Playgroud)
这对研究很烦人,因为每个Google结果似乎都集中在基本的Ruby语法上,而我在Ruby文档中找不到任何东西.