Splitting a symbol, in the same manner as one would split a string

谁都会走 提交于 2019-12-04 22:03:22

Since Ruby 1.9 some string's features are added to the Symbol class but not this much.The best you can do, I think is:

:symbol_with_underscores.to_s.split('_').map(&:to_sym)

You could turn this into a Symbol method:

class Symbol
  def split(separator)
    to_s.split(separator).map(&:to_sym)
  end
end

:symbol_with_underscores.split('_')
# => [:symbol, :with, :underscores]
Greg Dan

Think about symbols as numbers. Because symbols are internally stored as int numbers. Therefore they don't have string related methods.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!