如何将 Ruby 中的字符串转换为变量的引用?

发布于 2024-10-20 22:09:43 字数 641 浏览 0 评论 0原文

我需要能够将变量的名称传递到表达式(以黄瓜形式),并且希望能够将该字符串转换为变量的引用(即不是副本)。

例如,

Given /^I have set an initial value to @my_var$/ do
  @my_var = 10
end

# and now I want to change the value of that variable in a different step
Then /^I update "([^"]*)"$/ do |var_name_string|
  # I know I can get the value of @my_var by doing:
  eval "@my_var_copy = @#{var_name_string}"

  # But then once I update @my_var_copy I have to finish by updating the original
  eval "@#{var_name_string} = @my_var_copy"

  # How do instead I create a reference to the @my_var object?
end

由于 Ruby 是一种反射性语言,我确信我想做的事情是可能的,但我还没有破解它。

I need to be able to pass the name of a variable into an expression (in cucumber) and would like to be able to convert that string into a reference (i.e. not a copy) of the variable.

e.g.

Given /^I have set an initial value to @my_var$/ do
  @my_var = 10
end

# and now I want to change the value of that variable in a different step
Then /^I update "([^"]*)"$/ do |var_name_string|
  # I know I can get the value of @my_var by doing:
  eval "@my_var_copy = @#{var_name_string}"

  # But then once I update @my_var_copy I have to finish by updating the original
  eval "@#{var_name_string} = @my_var_copy"

  # How do instead I create a reference to the @my_var object?
end

Since Ruby is such a reflective language I'm sure what I'm trying to do is possible but I haven't yet cracked it.

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

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

发布评论

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

评论(3

唔猫 2024-10-27 22:09:44

解决方案可能是将事物包装到数组中。这可以很容易地通过引用传递。

irb(main):001:0> my_var = [10]
=> [10]
irb(main):002:0> my_var_copy = my_var
=> [10]
irb(main):003:0> my_var[0] = 55
=> 55
irb(main):004:0> my_var_copy
=> [55]

请参阅此处 - http://www.rubyfleebie.com/understanding-fixnums/

并且(稍微偏离主题,但给了我解决方案的初步想法)在这里 - http:// ruby.about.com/od/advancedruby/a/deepcopy.htm

A solution might be to wrap things into an array. Which can easily be passed around by reference.

irb(main):001:0> my_var = [10]
=> [10]
irb(main):002:0> my_var_copy = my_var
=> [10]
irb(main):003:0> my_var[0] = 55
=> 55
irb(main):004:0> my_var_copy
=> [55]

See here - http://www.rubyfleebie.com/understanding-fixnums/

And (slightly off topic, but gave me the initial idea for a solution) here - http://ruby.about.com/od/advancedruby/a/deepcopy.htm

素衣风尘叹 2024-10-27 22:09:43
  class Reference
    def initialize(var_name, vars)
      @getter = eval "lambda { #{var_name} }", vars
      @setter = eval "lambda { |v| #{var_name} = v }", vars
    end
    def value
      @getter.call
    end
    def value=(new_value)
      @setter.call(new_value)
    end
  end

http://onestepback.org/index.cgi/Tech/Ruby/ 得到这个RubyBindings.rdoc。祝你好运!

  class Reference
    def initialize(var_name, vars)
      @getter = eval "lambda { #{var_name} }", vars
      @setter = eval "lambda { |v| #{var_name} = v }", vars
    end
    def value
      @getter.call
    end
    def value=(new_value)
      @setter.call(new_value)
    end
  end

Got this from http://onestepback.org/index.cgi/Tech/Ruby/RubyBindings.rdoc. Good luck!

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