In Ruby, is it possible to pass by reference a parameter with value-type semantics (e.g. a Fixnum)? I'm looking for something similar to C#'s 'ref' keyword.
在Ruby中,是否可能通过引用传递具有值类型语义的参数(例如一个Fixnum)?我在找一些类似c# 's 'ref'关键字的东西。
Example:
例子:
def func(x)
x += 1
end
a = 5
func(a) #this should be something like func(ref a)
puts a #should read '6'
Btw. I know I could just use:
顺便说一句。我知道我可以用:
a = func(a)
5 个解决方案
#1
31
You can accomplish this by explicitly passing in the current binding:
您可以通过显式地传入当前绑定来实现这一点:
def func(x, bdg)
eval "#{x} += 1", bdg
end
a = 5
func(:a, binding)
puts a # => 6
#2
19
Ruby doesn't support "pass by reference" at all. Everything is an object and the references to those objects are always passed by value. Actually, in your example you are passing a copy of the reference to the Fixnum
Object by value.
Ruby根本不支持“引用传递”。一切都是一个对象,对这些对象的引用总是通过值传递。实际上,在您的示例中,您正在按值将引用的副本传递给Fixnum对象。
The problem with the your code is, that x += 1
doesn't modify the passed Fixnum
Object but instead creates a completely new and independent object.
您的代码的问题是,x += 1不修改传递的Fixnum对象,而是创建一个全新的、独立的对象。
I think, Java programmers would call Fixnum
objects immutable.
我认为,Java程序员会把Fixnum对象称为不可变的。
#3
11
In Ruby you can't pass parameters by reference. For your example, you would have to return the new value and assign it to the variable a or create a new class that contains the value and pass an instance of this class around. Example:
在Ruby中,不能通过引用传递参数。对于您的示例,您必须返回新的值并将其分配给变量a,或者创建一个包含该值的新类,并传递该类的实例。例子:
class Container
attr_accessor :value
def initialize value
@value = value
end
end
def func(x)
x.value += 1
end
a = Container.new(5)
func(a)
puts a.value
#4
6
You can try following trick:
你可以试试下面的技巧:
def func(x)
x[0] += 1
end
a = [5]
func(a) #this should be something like func(ref a)
puts a[0] #should read '6'
#5
2
http://ruby-doc.org/core-2.1.5/Fixnum.html
http://ruby-doc.org/core-2.1.5/Fixnum.html
Fixnum objects have immediate value. This means that when they are assigned or passed as parameters, the actual object is passed, rather than a reference to that object.
Fixnum对象具有立即的值。这意味着当它们作为参数分配或传递时,实际的对象被传递,而不是对该对象的引用。
Also Ruby is pass by value.
Ruby也是通过值传递的。
#1
31
You can accomplish this by explicitly passing in the current binding:
您可以通过显式地传入当前绑定来实现这一点:
def func(x, bdg)
eval "#{x} += 1", bdg
end
a = 5
func(:a, binding)
puts a # => 6
#2
19
Ruby doesn't support "pass by reference" at all. Everything is an object and the references to those objects are always passed by value. Actually, in your example you are passing a copy of the reference to the Fixnum
Object by value.
Ruby根本不支持“引用传递”。一切都是一个对象,对这些对象的引用总是通过值传递。实际上,在您的示例中,您正在按值将引用的副本传递给Fixnum对象。
The problem with the your code is, that x += 1
doesn't modify the passed Fixnum
Object but instead creates a completely new and independent object.
您的代码的问题是,x += 1不修改传递的Fixnum对象,而是创建一个全新的、独立的对象。
I think, Java programmers would call Fixnum
objects immutable.
我认为,Java程序员会把Fixnum对象称为不可变的。
#3
11
In Ruby you can't pass parameters by reference. For your example, you would have to return the new value and assign it to the variable a or create a new class that contains the value and pass an instance of this class around. Example:
在Ruby中,不能通过引用传递参数。对于您的示例,您必须返回新的值并将其分配给变量a,或者创建一个包含该值的新类,并传递该类的实例。例子:
class Container
attr_accessor :value
def initialize value
@value = value
end
end
def func(x)
x.value += 1
end
a = Container.new(5)
func(a)
puts a.value
#4
6
You can try following trick:
你可以试试下面的技巧:
def func(x)
x[0] += 1
end
a = [5]
func(a) #this should be something like func(ref a)
puts a[0] #should read '6'
#5
2
http://ruby-doc.org/core-2.1.5/Fixnum.html
http://ruby-doc.org/core-2.1.5/Fixnum.html
Fixnum objects have immediate value. This means that when they are assigned or passed as parameters, the actual object is passed, rather than a reference to that object.
Fixnum对象具有立即的值。这意味着当它们作为参数分配或传递时,实际的对象被传递,而不是对该对象的引用。
Also Ruby is pass by value.
Ruby也是通过值传递的。