开发者

Ruby: extract all members of an object

How do I extract all members (methods, variables, constants, objects, etc) of an arbitrary object and pass them to a block?

def 开发者_StackOverflow中文版inside(obj)
  #pass all the members of the object to the block
end

inside myLink do
  url = "myurl.com"
end


You can use instance_eval:

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

You'll still need to use self though:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜