开发者

Ruby: extract all members of an object

开发者 https://www.devze.com 2023-03-27 08:22 出处:网络
How do I extract all members (methods, variables, constants, objects, etc) of an arbitrary object and pass them to a block?

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

精彩评论

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

关注公众号