ruby - 如果在 Ruby 中一切都是对象,为什么这不起作用?

标签 ruby parameter-passing pass-by-reference

考虑到在 Ruby 编程语言中一切都被称为对象,我有把握地假设向方法传递参数是通过引用完成的。然而,下面这个小例子让我感到困惑:

$string = "String"

def changer(s)
  s = 1
end

changer($string)

puts $string.class
String
 => nil

如您所见,原始对象未被修改,我想知道原因,以及如何实现所需的行为,即。 获取方法以实际更改其参数引用的对象。

最佳答案

Ruby 的工作方式是按值传递和按引用传递的组合。事实上,Ruby 使用引用传递值。

您可以在以下线程中阅读更多内容:

一些著名的引述:

Absolutely right: Ruby uses pass by value - with references.

irb(main):004:0> def foo(x) x = 10 end
=> nil
irb(main):005:0> def bar; x = 20; foo(x); x end
=> nil
irb(main):006:0> bar
=> 20
irb(main):007:0>

There is no standard way (i.e. other than involving eval and metaprogramming magic) to make a variable in a calling scope point to another object. And, btw, this is independent of the object that the variable refers to. Immediate objects in Ruby seamlessly integrate with the rest (different like POD's in Java for example) and from a Ruby language perspective you don't see any difference (other than performance maybe). This is one of the reasons why Ruby is so elegant.

When you pass an argument into a method, you are passing a variable that points to a reference. In a way, it's a combination of pass by value and pass by reference. What I mean is, you pass the value of the variable in to the method, however the value of the variable is always a reference to an object.

The difference between:

def my_method( a )
  a.gsub!( /foo/, 'ruby' )
end

str = 'foo is awesome'
my_method( str )            #=> 'ruby is awesome'
str                                    #=> 'ruby is awesome'

and:

def your_method( a )
  a = a.gsub( /foo/, 'ruby' )
end

str = 'foo is awesome'
my_method( str )            #=> 'ruby is awesome'
str                                    #=> 'foo is awesome'

is that in #my_method, you are calling #gsub! which changes the object (a) in place. Since the 'str' variable (outside the method scope) and the 'a' variable (inside the method scope) both have a "value" that is a reference to the same object, the change to that object is reflected in the 'str' variable after the method is called. In #your_method, you call #gsub which does not modify the original object. Instead it creates a new instance of String that contains the modifications. When you assign that object to the 'a' variable, you are changing the value of 'a' to be a reference to that new String instance. However, the value of 'str' still contains a reference to the original (unmodified) string object.

方法是否改变引用或被引用的对象取决于类类型和方法实现。

string = "hello"

def changer(str)
  str = "hi"
end

changer(string)
puts string
# => "hello"

string 没有改变,因为对字符串的赋值替换了引用,而不是引用的值。 如果您想就地修改字符串,您需要使用 String#replace

string = "hello"

def changer(str)
  str.replace "hi"
end

changer(string)
puts string
# => "hi"

String 是一种常见的情况,其中大部分操作都在克隆上进行,而不是在自身实例上进行。 出于这个原因,几种方法都有一个 bang 版本,可以就地执行相同的操作。

str1 = "hello"
str2 = "hello"

str1.gsub("h", "H")
str2.gsub!("h", "H")

puts str1
# => "hello"
puts str2
# => "Hello"

最后,要回答您原来的问题,您不能更改字符串。您只能为其分配一个新值或将字符串包装到不同的可变对象中并替换内部引用。

$wrapper = Struct.new(:string).new
$wrapper.string = "String"

def changer(w)
  w.string = 1
end

changer($wrapper)

puts $wrapper.string
# => 1

关于ruby - 如果在 Ruby 中一切都是对象,为什么这不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7688391/

相关文章:

swift - 通过委托(delegate)传递类 : property not updated

c# - 为什么 "in"关键字允许 C# 属性发生变化?

python - 使用 Pyobjc 通过引用传递 NSError 对象

ruby-on-rails - Ruby on Rails 中模型的完全限定名称是什么?

ruby - 识别 Treetop 语法中的 Ruby 代码

ruby-on-rails - 为什么人们大多包装 I18n.t 方法而不是委托(delegate)?

Ruby 注入(inject)奇怪的行为

java - 调用方法并传递对象引用并使用相同的引用捕获返回

python-3.x - 使用 yaml 覆盖命名空间

php - 如何在没有引用的情况下复制对象?