Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 试图理解电铲操作员的弦乐_Ruby_String - Fatal编程技术网

Ruby 试图理解电铲操作员的弦乐

Ruby 试图理解电铲操作员的弦乐,ruby,string,Ruby,String,我正在经历 在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

我正在经历

在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
def test\u电铲操作员修改原始字符串
原始字符串=“你好,”
hi=原始字符串
这里是“世界”

hi当您设置
hi=original_string
时,您的
hi
变量只是指向同一对象的新变量。如果查看
hi.object\u id
original\u string.object\u id
,您会发现它们是相同的。如果您想要一个对象的克隆,可以在不影响
original\u string
,您需要说类似于
hi=original\u string.clone
hi=original\u string.dup

谁创造了术语“铲式操作符”?我不确定我是否见过用于连接字符串的铲式操作符。一些更常见的技术是
“#{hi}{there}”
[hi,there]。加入
。再次查看第3行:在第3行中,您告诉Ruby使它们相同!因此,它们是相同的。