What is the name of a class, in a class method?
What is the name of a class, in a class method?
1 2 3 4 5 6 7 |
# bad class Foo def self.hello puts class_name # is a monkey patch from yard so probably won't work in production puts class.name # is 'Module' :( end end |
1 2 3 4 5 6 7 8 9 |
# good class Foo def self.hello puts name # built in to Ruby end end [4] pry(main)> Foo.hello Foo |
See Module#name in the Ruby 2.7 docs
Thanks to a Stack Overflow answer
In an instance method, use class.name