在 Ruby 中,如何获取散列而不是数组中的实例变量?

发布于 2024-09-02 00:17:29 字数 375 浏览 7 评论 0原文

我有一个 Ruby 课程。我想从该类中的方法的参数获取实例变量。我可以将所有实例变量作为数组获取:

self.instance_variables

但是,我想获取名为 arg 的实例变量,具体来说:

class MyClass
  def get_instance_variable(arg)
    hash_of_instance_variables[arg]
  end
end

object.get_instance_variable('my_instance_var')

如何计算 hash_of_instance_variables

I have a Ruby class. I want to get an instance variable from an argument to a method in that class. I can do get all of the instance variables as an array:

self.instance_variables

However, I want to get the instance variable named arg, specifically:

class MyClass
  def get_instance_variable(arg)
    hash_of_instance_variables[arg]
  end
end

object.get_instance_variable('my_instance_var')

How do I compute hash_of_instance_variables?

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

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

发布评论

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

评论(5

冧九 2024-09-09 00:17:29

要创建所有实例变量的哈希,您可以使用以下代码:

class Object
  def instance_variables_hash
    Hash[instance_variables.map { |name| [name, instance_variable_get(name)] } ]
  end
end

但正如 cam 在他的评论中提到的,您应该使用 instance_variable_get 方法:

object.instance_variable_get :@my_instance_var

To create a hash of all instance variables you can use the following code:

class Object
  def instance_variables_hash
    Hash[instance_variables.map { |name| [name, instance_variable_get(name)] } ]
  end
end

But as cam mentioned in his comment, you should use instance_variable_get method instead:

object.instance_variable_get :@my_instance_var
揪着可爱 2024-09-09 00:17:29

问题很老,但找到了 Rails 解决方案: instance_values

这是第一个答案谷歌所以也许它会对某人有所帮助。

Question is quite old but found rails solution for this: instance_values

This is first answer in google so maybe it will help someone.

小姐丶请自重 2024-09-09 00:17:29
class MyClass    
def variables_to_hash
      h = {}
      instance_variables.each{|a|
        s = a.to_s
        n = s[1..s.size]
        v = instance_variable_get a
        h[n] = v
      }
      h
    end
end
class MyClass    
def variables_to_hash
      h = {}
      instance_variables.each{|a|
        s = a.to_s
        n = s[1..s.size]
        v = instance_variable_get a
        h[n] = v
      }
      h
    end
end
难忘№最初的完美 2024-09-09 00:17:29

对于 Ruby 2.6+,您可以将块传递给to_h 方法,导致语法非常 DRY:

# Some instance variables
instance_variable_set(:@a, 'dog')
#=> "dog"
instance_variable_set(:@b, 'cat')
#=> "cat"

# Array of instance variable names
instance_variables
#=> [:@a, :@b]

# Hash of instance variable names and values, including leading @
instance_variables.to_h { |k| [k, instance_variable_get(k)] }
#=> { :@a => "dog", :@b => "cat" }

# Hash of instance variable names and values, excluding leading @
instance_variables.to_h { |k| [k[1..-1].to_sym, instance_variable_get(k)] }
#=> { :a => "dog", :b => "cat" }

For Ruby 2.6+, you can pass a block to the to_h method, leading to very DRY syntax:

# Some instance variables
instance_variable_set(:@a, 'dog')
#=> "dog"
instance_variable_set(:@b, 'cat')
#=> "cat"

# Array of instance variable names
instance_variables
#=> [:@a, :@b]

# Hash of instance variable names and values, including leading @
instance_variables.to_h { |k| [k, instance_variable_get(k)] }
#=> { :@a => "dog", :@b => "cat" }

# Hash of instance variable names and values, excluding leading @
instance_variables.to_h { |k| [k[1..-1].to_sym, instance_variable_get(k)] }
#=> { :a => "dog", :b => "cat" }
怎言笑 2024-09-09 00:17:29

Ruby on Rails 有几种内置方法可以实现此目的,您可能会发现它们可以满足您的需求。

user = User.new(第一个: 'brian', 最后一个: 'case')

用户.属性

{“第一个”=>“布莱恩”,“最后一个”=>“案例”}

用户.serializeable_hash

{“第一个”=>“布莱恩”,“最后一个”=>“案例”}

Ruby on Rails has a couple of built-in ways to do this that you might find meet your needs.

user = User.new(first: 'brian', last: 'case')

user.attributes

{"first"=>"brian", "last"=>"case"}

user.serializeable_hash

{"first"=>"brian", "last"=>"case"}

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