When I use define_methodfrom a block passed to an iterator, for some reason it defined?(super)never evaluates true.
See the example below. Note that this super(value)is a valid challenge, although it defined?thinks differently.
class A
def message=(val)
puts 'A says ' + val
end
end
class B < A
['message', 'warning'].each do |method|
define_method(method + '=') do |val|
puts 'B says ' + val
super(val) if defined?(super)
end
end
end
a = A.new
a.message = 'hello!'
b = B.new
b.message = 'hello!'
class B < A
['message', 'warning'].each do |method|
define_method(method + '=') do |val|
puts 'B says ' + val
super(val) rescue nil
end
end
end
b = B.new
b.message = 'hello!'
source
share