如果在 Ruby 中一切都是对象,为什么这不起作用? [英] Why doesn't this work if in Ruby everything is an Object?

查看:47
本文介绍了如果在 Ruby 中一切都是对象,为什么这不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Considering that in the Ruby programming language everything is said to be an Object, I safely assumed that passing arguments to methods are done by reference. However this little example below puzzles me:

$string = "String"

def changer(s)
  s = 1
end

changer($string)

puts $string.class
String
 => nil

正如您所看到的原始对象没有被修改,我想知道为什么,以及我怎样才能完成所需的行为,即.获取方法以实际更改其参数引用的对象.

As you can see the original Object wasn't modified, I wish to know why, and also, how could I accomplish the desired behavior ie. Getting the method to actually change the object referenced by its argument.

推荐答案

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

The way Ruby works is a combination of pass by value and pass by reference. In fact, Ruby uses pass by value with references.

您可以在以下主题中阅读更多内容:

You can read more in the following threads:

一些值得注意的引述:

绝对正确: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>

没有标准的方法(即除了涉及 eval 和元编程魔法)使调用范围中的变量指向另一个对象.而且,顺便说一句,这独立于对象变量指.Ruby 中的立即对象与其余的(例如与 Java 中的 POD 不同)和来自 Ruby语言角度你看不到任何区别(除了性能可能).这也是 Ruby 如此优雅的原因之一.

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.

两者的区别:

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

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

和:

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

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

是在#my_method 中,您正在调用#gsub!它改变了对象(a) 到位.由于str"变量(在方法范围之外)和'a' 变量(在方法范围内)都有一个值",即对同一个对象的引用,反映了对该对象的更改在调用方法后的str"变量中.在#your_method 中,您调用 #gsub ,它修改原始对象.相反它创建一个包含修改的 String 的新实例.什么时候您将该对象分配给a"变量,您正在更改该值'a' 是对该新 String 实例的引用.然而str"的值仍然包含对原始(未修改)的引用字符串对象.

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.

一个方法是改变引用还是被引用的对象取决于类的类型和方法的实现.

Whether a method changes the reference or the referenced object depends on the class type and method implementation.

string = "hello"

def changer(str)
  str = "hi"
end

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

string 不会改变,因为对字符串的赋值替换了引用,而不是引用的值.我想就地修改字符串,需要使用String#replace.

string is not changed because the assignment on strings replaces the reference, not the referenced value. I you want to modify the string in place, you need to use String#replace.

string = "hello"

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

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

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

String is a common case where the most part of operations works on clones, not on the self instance. For this reason, several methods have a bang version that executes the same operation in place.

str1 = "hello"
str2 = "hello"

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

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

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

Finally, to answer your original question, you cannot change a String. You can only assign a new value to it or wrap the string into a different mutable object and replace the internal reference.

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

def changer(w)
  w.string = 1
end

changer($wrapper)

puts $wrapper.string
# => 1

这篇关于如果在 Ruby 中一切都是对象,为什么这不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆