Ruby 类名转化为符号最便捷的方法

kevinhua · March 29, 2012 · Last by AlphaLiu replied at March 30, 2012 · 2204 hits

我目前使用的是:

self.to_s.downcase.to_sym

有没有更简捷的?

好吧...

class Class
  def name_to_sym
    self.to_s.downcase.to_sym
  end
end

class Foobar; end

Foobar.name_to_sym

^_^

#1 楼 @lgn21st 这个……破坏性太强

那个没啥破坏性,哈哈

@lgn21st 已经猜中真相了

class Foobar; end
Foobar.name.to_sym

:"#{self}"

#1 楼 @lgn21st monkey patch , yeah!!

You need to Sign in before reply, if you don't have an account, please Sign up first.