Ruby 中的字符串是可变的吗?

发布于 2024-11-01 22:19:22 字数 380 浏览 5 评论 0原文

Ruby 中的字符串是可变的吗?根据文档

str = "hello"
str = str + " world"

创建一个具有值的新字符串对象"hello world" 但当我们这样做时,

str = "hello"
str << " world"

它并没有提到它创建了一个新对象,因此它会改变 str 对象,该对象现在的值为 “你好世界”?

Are Strings mutable in Ruby? According to the documentation doing

str = "hello"
str = str + " world"

creates a new string object with the value "hello world" but when we do

str = "hello"
str << " world"

It does not mention that it creates a new object, so does it mutate the str object, which will now have the value "hello world"?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

彡翼 2024-11-08 22:19:23

是的,<< 改变同一个对象,而 + 创建一个新对象。示范:

irb(main):011:0> str = "hello"
=> "hello"
irb(main):012:0> str.object_id
=> 22269036
irb(main):013:0> str << " world"
=> "hello world"
irb(main):014:0> str.object_id
=> 22269036
irb(main):015:0> str = str + " world"
=> "hello world world"
irb(main):016:0> str.object_id
=> 21462360
irb(main):017:0>

Yes, << mutates the same object, and + creates a new one. Demonstration:

irb(main):011:0> str = "hello"
=> "hello"
irb(main):012:0> str.object_id
=> 22269036
irb(main):013:0> str << " world"
=> "hello world"
irb(main):014:0> str.object_id
=> 22269036
irb(main):015:0> str = str + " world"
=> "hello world world"
irb(main):016:0> str.object_id
=> 21462360
irb(main):017:0>
苦妄 2024-11-08 22:19:23

作为补充,这种可变性的一个含义如下:

ruby-1.9.2-p0 :001 > str = "foo"
 => "foo" 
ruby-1.9.2-p0 :002 > ref = str
 => "foo" 
ruby-1.9.2-p0 :003 > str = str + "bar"
 => "foobar" 
ruby-1.9.2-p0 :004 > str
 => "foobar" 
ruby-1.9.2-p0 :005 > ref
 => "foo" 

因此

ruby-1.9.2-p0 :001 > str = "foo"
 => "foo" 
ruby-1.9.2-p0 :002 > ref = str
 => "foo" 
ruby-1.9.2-p0 :003 > str << "bar"
 => "foobar" 
ruby-1.9.2-p0 :004 > str
 => "foobar" 
ruby-1.9.2-p0 :005 > ref
 => "foobar" 

,您应该明智地选择对字符串使用的方法,以避免出现意外行为。

另外,如果您希望在整个应用程序中具有不可变且独特的东西,您应该使用符号:

ruby-1.9.2-p0 :001 > "foo" == "foo"
 => true 
ruby-1.9.2-p0 :002 > "foo".object_id == "foo".object_id
 => false 
ruby-1.9.2-p0 :003 > :foo == :foo
 => true 
ruby-1.9.2-p0 :004 > :foo.object_id == :foo.object_id
 => true 

Just to complement, one implication of this mutability is seem below:

ruby-1.9.2-p0 :001 > str = "foo"
 => "foo" 
ruby-1.9.2-p0 :002 > ref = str
 => "foo" 
ruby-1.9.2-p0 :003 > str = str + "bar"
 => "foobar" 
ruby-1.9.2-p0 :004 > str
 => "foobar" 
ruby-1.9.2-p0 :005 > ref
 => "foo" 

and

ruby-1.9.2-p0 :001 > str = "foo"
 => "foo" 
ruby-1.9.2-p0 :002 > ref = str
 => "foo" 
ruby-1.9.2-p0 :003 > str << "bar"
 => "foobar" 
ruby-1.9.2-p0 :004 > str
 => "foobar" 
ruby-1.9.2-p0 :005 > ref
 => "foobar" 

So, you should choose wisely the methods you use with strings in order to avoid unexpected behavior.

Also, if you want something immutable and unique throughout your application you should go with symbols:

ruby-1.9.2-p0 :001 > "foo" == "foo"
 => true 
ruby-1.9.2-p0 :002 > "foo".object_id == "foo".object_id
 => false 
ruby-1.9.2-p0 :003 > :foo == :foo
 => true 
ruby-1.9.2-p0 :004 > :foo.object_id == :foo.object_id
 => true 
始终不够 2024-11-08 22:19:23

虽然上面的答案很完美,但只是为未来的读者添加这个答案。

在大多数语言中,字符串文字也是不可变的,就像
数字和符号。在小于 3 的 Ruby 版本中,所有
默认情况下,字符串是可变的。这在 Ruby 3 中发生了变化。现在都是字符串
在 Ruby 3++ 版本中,文字默认是不可变的。

While above answers are perfect, Just adding this answer for future readers.

In most languages, string literals are also immutable, just like
numbers and symbols. In Ruby versions that are less than 3, all
strings are mutable by default. This changed in Ruby 3. Now all string
literals are immutable by default in Ruby 3++ versions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文