Class: String
Instance Method Summary collapse
-
#constantize ⇒ String
Converts string into Ruby constant.
-
#nmtokenize ⇒ String
Does reverse of #constantize e.g.
Instance Method Details
#constantize ⇒ String
Returns converts string into Ruby constant. self must be String with no whitespaces and match Regexp.nmtoken ‘foo-bar’.constantize => ‘Foo_bar’ ‘foo_bar’.constantize => ‘FooBar’.
8 9 10 11 12 13 14 |
# File 'lib/duxml/ruby_ext/string.rb', line 8 def constantize return self if Regexp.constant.match(self) raise Exception unless Regexp.nmtoken.match(self) s = split('_').collect do |word| word.capitalize unless word == '_' end.join.gsub('-', '_') raise Exception unless s.match(Regexp.constant) s end |
#nmtokenize ⇒ String
Returns does reverse of #constantize e.g. ‘Foo_b’.nmtokenize => ‘foo-bar’ ‘FooBar’.nmtokenize => ‘foo_bar’.
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/duxml/ruby_ext/string.rb', line 19 def nmtokenize split('::').collect do |word| word.gsub(/(?!^)[A-Z_]/) do |match| case match when '_' then '-' else "_#{match.downcase}" end end.downcase end.join(':') end |