在Ruby中,如何在哈希而不是数组中获取实例变量?

时间:2022-09-11 15:08:57

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:

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

self.instance_variables

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

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

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?

我如何计算hash_of_instance_variables?

4 个解决方案

#1


18  

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:

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

object.instance_variable_get :@my_instance_var

#2


5  

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

问题很老但是找到了rails解决方案:instance_values

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

这是谷歌的第一个答案,所以它可能会帮助某人。

#3


1  

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

#4


-1  

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

Ruby on Rails有几种内置方法可以满足您的需求。

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

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

user.attributes

user.attributes

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

{“first”=>“brian”,“last”=>“case”}

user.serializeable_hash

user.serializeable_hash

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

{“first”=>“brian”,“last”=>“case”}

#1


18  

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:

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

object.instance_variable_get :@my_instance_var

#2


5  

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

问题很老但是找到了rails解决方案:instance_values

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

这是谷歌的第一个答案,所以它可能会帮助某人。

#3


1  

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

#4


-1  

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

Ruby on Rails有几种内置方法可以满足您的需求。

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

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

user.attributes

user.attributes

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

{“first”=>“brian”,“last”=>“case”}

user.serializeable_hash

user.serializeable_hash

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

{“first”=>“brian”,“last”=>“case”}