K
Kyung won Cheon
class A
def aaa
puts "called aaa by #{self}"
def bbb
puts "called bbb by #{self}"
end
end
end
a = A.new
p a.respond_to?bbb) # => false
a.aaa
p a.respond_to?bbb) # => true
a.bbb
p A.public_instance_methods.grep(/bbb/) # => ["bbb"]
a2 = A.new
a2.bbb
p a.respond_to?ccc) # => false
a.instance_eval do
def ccc
puts "called ccc by #{self}"
end
end
p a.respond_to?ccc) # => true
a.ccc
p A.public_instance_methods.grep(/ccc/) # => []
a2.ccc rescue puts $! # undefined method
#####################
# What's the diff ??
# Help Me^^
#####################
def aaa
puts "called aaa by #{self}"
def bbb
puts "called bbb by #{self}"
end
end
end
a = A.new
p a.respond_to?bbb) # => false
a.aaa
p a.respond_to?bbb) # => true
a.bbb
p A.public_instance_methods.grep(/bbb/) # => ["bbb"]
a2 = A.new
a2.bbb
p a.respond_to?ccc) # => false
a.instance_eval do
def ccc
puts "called ccc by #{self}"
end
end
p a.respond_to?ccc) # => true
a.ccc
p A.public_instance_methods.grep(/ccc/) # => []
a2.ccc rescue puts $! # undefined method
#####################
# What's the diff ??
# Help Me^^
#####################