代码之家  ›  专栏  ›  技术社区  ›  Omar Ali

使用符号访问对象属性

  •  3
  • Omar Ali  · 技术社区  · 14 年前

    def myhelper(object, attributes = [])
      attributes.each do |attr|
        object.attr
      end
    end
    

    myhelper Person, [:title, :name]
    

    我试图实现的是亲自打印一个属性列表,但是对象.attr在myhelper中,方法不起作用。

    我该怎么做?

    1 回复  |  直到 14 年前
        1
  •  6
  •   jordinl    14 年前

    尝试 object.send(attr)

    另外,你想做一张地图吗?

    推荐文章