I want to create a parent class (or module) with methods that does something with the method name of child classes where the parent method is called. Example:
class Foo
def foo
puts __method__
end
end
class Bar < Foo
def bar
foo
end
end
When Bar.new.bar
is called,it prints :foo
, but I want to make it print bar
. How should Foo#foo
method be to satisfy that behavior?
Try caller
:
class Foo
def foo
puts caller[0][/`.*'/][1..-2]
end
end
class Bar < Foo
def bar
foo
end
end
Bar.new.bar #=> bar
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.