Ruby:提取对象的所有成员

时间:2011-08-10 17:52:31

标签: ruby syntax

如何提取任意对象的所有成员(方法,变量,常量,对象等)并将它们传递给块?

def inside(obj)
  #pass all the members of the object to the block
end

inside myLink do
  url = "myurl.com"
end

1 个答案:

答案 0 :(得分:2)

您可以使用instance_eval:

def inside obj, &block
  obj.instance_eval &block
end

你仍然需要使用自己:

inside myLink do
   self.url = "myurl.com"
   # or:
   @url = "myurl.com"
end