Skip to content

Instantly share code, notes, and snippets.

@takaheraw
Created January 31, 2012 10:15
Show Gist options
  • Save takaheraw/1709759 to your computer and use it in GitHub Desktop.
Save takaheraw/1709759 to your computer and use it in GitHub Desktop.
class A
instance_methods.each do |m|
undef_method m unless m.to_s =~ /^__|object_id|method_missing|respond_to?/
end
def initialize(o)
@o = o
end
def method_missing(name, *args)
super if !respond_to?(name)
@o.send("get_#{name}_info", args[0])
end
def respond_to?(method)
@o.respond_to?("get_#{method}_info") || super
end
end
class B
def get_foo_info(id)
p "#{__method__}: #{id}"
end
def get_bar_info(id)
p "#{__method__}: #{id}"
end
end
obj = A.new(B.new)
obj.foo(100) # => "get_foo_info: 100"
obj.bar(100) # => "get_bar_info: 100"
obj.baz(100) # => `method_missing': undefined method `baz' for #<A:0x00000013eac550> (NoMethodError)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment