更改方法中传递的变量

发布于 2024-11-05 12:24:18 字数 210 浏览 1 评论 0原文

如何使用方法更改变量的内容?也许我说得不正确。有什么方法可以像 C 中那样获取对变量的引用?示例:

// main stuff
int gorilla = 29;
makeMeABanana(&gorilla);

void makeMeABanana(int *gorilla) { }

我怎样才能在 Ruby 中做这样的事情?

How can I change the contents of a variable using a method? Maybe I'm not saying this correctly. What is a way to get the reference to a variable like in C? Example:

// main stuff
int gorilla = 29;
makeMeABanana(&gorilla);

void makeMeABanana(int *gorilla) { }

How can I do something like this in Ruby?

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

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

发布评论

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

评论(3

空城之時有危險 2024-11-12 12:24:18

您不应该这样做 - 您只是将完全适合 C 的技术移植到 Ruby,而它们在 Ruby 中不再适用。有几种奇特的方法可以解决这个问题(例如,使用在调用名称空间上关闭的 Proc 或 eval),但它们在 Ruby 中通常不合适,除非您确切地知道自己在做什么。

You should not do this - you're just porting techniques that are fully appropriate to C to Ruby, where they are no longer appropriate. There are several fancy ways around this (eg using a Proc closed over your calling namespace, or eval) but they are usually inappropriate in Ruby unless you know precisely what you're doing.

夏末 2024-11-12 12:24:18

最近,在 ruby​​-talk 邮件列表上,有人询问编写一个交换函数,其中 swap(a,b) 将交换变量“a”和“b”的值。通常这不能在 Ruby 中完成,因为交换函数不会引用调用函数的绑定。

但是,如果我们显式传递绑定,则可以编写类似交换的函数。这是一个简单的尝试:

 def swap(var_a, var_b, vars)
    old_a = eval var_a, vars
    old_b = eval var_b, vars
    eval "#{var_a} = #{old_b}", vars
    eval "#{var_b} = #{old_a}", vars
  end

  a = 22
  b = 33
  swap ("a", "b", binding)
  p a                          # => 33
  p b                          # => 22

这确实有效!但它有一个很大的缺点。 “a”和“b”的旧值被插入到字符串中。只要旧值是简单的文字(例如整数或字符串),那么最后两个 eval 语句将如下所示: eval "a = 33", vars"。但是如果旧值是复杂对象,则 eval 将如下所示 之间往返的值都会失败。

就像 eval "a = #",哎呀,对于任何无法在字符串 /Tech/Ruby/RubyBindings.rdoc" rel="nofollow">http://onestepback.org/index.cgi/Tech/Ruby/RubyBindings.rdoc

Recently on the ruby-talk mailing list, someone asked about writing a swap function where swap(a,b) would swap the values of the variables "a" and "b". Normally this cannot be done in Ruby because the swap function would have no reference to the binding of the calling function.

However, if we explictly pass in the binding, then it is possible to write a swap-like function. Here is a simple attempt:

 def swap(var_a, var_b, vars)
    old_a = eval var_a, vars
    old_b = eval var_b, vars
    eval "#{var_a} = #{old_b}", vars
    eval "#{var_b} = #{old_a}", vars
  end

  a = 22
  b = 33
  swap ("a", "b", binding)
  p a                          # => 33
  p b                          # => 22

This actually works! But it has one big drawback. The old values of "a" and "b" are interpolated into a string. As long as the old values are simple literals (e.g. integers or strings), then the last two eval statements will look like: eval "a = 33", vars". But if the old values are complex objects, then the eval would look like eval "a = #", vars. Oops, this will fail for any value that can not survive a round trip to a string and back.

Referred from : http://onestepback.org/index.cgi/Tech/Ruby/RubyBindings.rdoc

记忆里有你的影子 2024-11-12 12:24:18

整数是对象,有一个 id,就像 Ruby 中的其他东西一样。它们的实现方式如下:

p (0..10).map{|n| n.object_id}
#=>[1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21]

所有其他对象都有偶数 object_id 编号。无法将 7 (object_id 15) 更改为其他内容。

Integers are objects, with an id, like everything else in Ruby. They are implemented like this:

p (0..10).map{|n| n.object_id}
#=>[1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21]

All other objects have even object_id numbers. There is no way to change 7 (object_id 15) into something else.

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