有一门课是这样的.
module Foo
class Bar
end
end
我想得到Bar
而不是Foo
的类名.
bar = Foo::Bar.new
bar.class.to_s.match('::(.+)$'){ $1 }
我可以通过这个代码得到类名,但我认为这不是最好的方法.
有没有更好的方法来获取没有名称空间的类名?
有一门课是这样的.
module Foo
class Bar
end
end
我想得到Bar
而不是Foo
的类名.
bar = Foo::Bar.new
bar.class.to_s.match('::(.+)$'){ $1 }
我可以通过这个代码得到类名,但我认为这不是最好的方法.
有没有更好的方法来获取没有名称空间的类名?
实现这一点的标准方法是调用Object#class和Module#name.例如:
bar.class.name.split('::').last
#=> "Bar"