【发布时间】:2018-03-31 21:58:15
【问题描述】:
在 koans/about_strings.rb:100 文件中
def test_the_shovel_operator_modifies_the_original_string
original_string = "Hello, "
hi = original_string
there = "World"
hi << there
assert_equal "Hello, World", original_string
# THINK ABOUT IT:
#
# Ruby programmers tend to favor the shovel operator (<<) over the
# plus equals operator (+=) when building up strings. Why?
end
这通过了,虽然我认为 original_string 将等于“Hello”,而 hi 将等于“Hello, World”
我看到这张海报有一个类似的问题,但不完全是:
Why is the shovel operator (<<) preferred over plus-equals (+=) when building a string in Ruby?
我错过了什么?
【问题讨论】:
-
谁创造了“铲子操作员”这个词?
-
我不确定我是否见过用于连接字符串的铲子运算符。一些更常见的技术是
"#{hi}#{there}"或[hi, there].join。 -
再次查看第 3 行:在第 3 行中,您告诉 Ruby 使它们相同!因此,它们是相同的。