我知道reverse会创建一个新的字符串,并且字符串的字符反向并且相反! 突变(反转)当前的字符串。 我的问题是为什么,例如测试回文时,会发生这种情况?
a = "foobar" a == a.reverse # => false a == a.reverse! # => true是因为它在内存中是相同的对象,因此==只是检查它们是否具有相同的内存位置?
谢谢!
I know that reverse creates a new string with the characters of the string in reverse and that reverse! mutates (reverses) the current string in place. My question is why, when for example testing for a palindrome, this occurs?:
a = "foobar" a == a.reverse # => false a == a.reverse! # => trueIs it because it is the same object in memory, therefore == just checks if they have the same memory location?
Thanks!
最满意答案
String#reverse! 方法返回它所调用的字符串
a == a.reverse!就像说的一样
a.reverse! a == a当然a == a是真的。
请注意,至少reverse!并不重要reverse! 对字符串做了什么,重要的是在o == om是方法( m )返回的内容。
The String#reverse! method returns the string it is called on so
a == a.reverse!is the same as saying
a.reverse! a == aand of course a == a is true.
Note that it doesn't matter in the least what reverse! does to the string, what matters to == in o == o.m is what the method (m) returns.
更多推荐
发布评论