动态获取变量值

发布于 2024-10-29 05:06:01 字数 547 浏览 1 评论 0原文

我有以下类,

class User
  attr_accessor :name, :age, :address

  def initialize()
    self.name = "new user"
    self.age = 19
    self.address = "address" 
  end  


end

如果有一个方法来获取属性的指定值,我想要什么。意味着,我有另一种方法来获取上述类中的所有方法名称

methods = self.public_methods(all = false)

,并且我可以从中获取方法名称(我的意思是名称的 getter 等..),并且我想要一种传递该方法名称的方法(我有它作为字符串)并获取方法的返回值

例如:

当我传递“名称”作为方法名称时,我应该能够获取“新用户”作为值

希望我清楚地表达了我的问题,提前感谢

**请注意,我必须使用这种方式,因为我的班级有很多属性并且它有很多继承的类。因此单独访问每个属性是不可能的:D

I have the following class

class User
  attr_accessor :name, :age, :address

  def initialize()
    self.name = "new user"
    self.age = 19
    self.address = "address" 
  end  


end

What I want if to have a method to get the assigned values to the attributes. Means, I have another method to get all the method names inside the above class

methods = self.public_methods(all = false)

and I can get the method name from that (I mean the getter of name etc..) and I want a way to pass that method name (which I have it as string) and get the return value of the method

Ex:

when I pass 'name' as the method name I should be able to get 'new user' as the value

hope I made my question clear, thanks in advance

** Please note, i have to use this way as my class has so many attributes and it has so many inherited classes. So accessing each and every attr individually is not possible :D

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

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

发布评论

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

评论(1

猫九 2024-11-05 05:06:01

这就是 send 的用途:

user = User.new
user.name        # => "new user"
user.send(:name) # => "new user"

getters = user.public_methods(false).reject { |m| m =~ /=$/ }
getters.each { |m| puts user.send(m) }

如果您没有访问器方法,则使用 instance_variable_get 是另一种选择:

user.instance_variable_get(:@name) # => "new user"

使用 public_methods 获取属性列表可能很危险,具体取决于您如何确定要调用哪些方法。相反,您可以创建自己的类方法,该方法既定义访问器又存储属性名称以供将来使用:

class User
  class << self
    attr_reader :fields

    def field (*names)
      names.flatten.each do |name|
        attr_accessor name
        (@fields ||= []) << name
      end
    end
  end

  field :name
  field :age
  field :address
end

user = User.new
user.name    = "Me"
user.age     = 22
user.address = "1234"

user.class.fields.each do |field|
  puts user.send(field)
end

That’s what send is for:

user = User.new
user.name        # => "new user"
user.send(:name) # => "new user"

getters = user.public_methods(false).reject { |m| m =~ /=$/ }
getters.each { |m| puts user.send(m) }

Using instance_variable_get is another option if you don’t have an accessor method:

user.instance_variable_get(:@name) # => "new user"

Using public_methods to get a list of attributes could be dangerous, depending on how you determine which methods to call. Instead, you could create your own class method which both defines the accessor and stores the attribute name for future use:

class User
  class << self
    attr_reader :fields

    def field (*names)
      names.flatten.each do |name|
        attr_accessor name
        (@fields ||= []) << name
      end
    end
  end

  field :name
  field :age
  field :address
end

user = User.new
user.name    = "Me"
user.age     = 22
user.address = "1234"

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