- camelcase
- camelize
- capitalize
- capitalized?
- downcase
- downcase?
- not?
- pad
- succ
- to_const
- to_proc
- to_str
- underscore
- upcase
- upcase?
- ~
Converts a symbol to camelcase. By default capitalization occurs on whitespace and underscores. By setting the first parameter to true the first character can also be captizlized. The second parameter can be assigned a valid Regualr Expression characeter set to determine which characters to match for capitalizing subsequent parts of the symbol.
:this_is_a_test.camelcase #=> :ThisIsATest :this_is_a_test.camelcase(false) #=> :thisIsATest
[ show source ]
# File lib/facets/core/symbol/camelcase.rb, line 17 def camelcase( *args ) self.to_s.camelcase( *args ).to_sym end
[ show source ]
# File lib/facets/core/symbol/camelize.rb, line 6 def camelize( *args ) self.to_s.camelize( *args ).to_sym end
[ show source ]
# File lib/facets/core/symbol/capitalize.rb, line 6 def capitalize to_s.capitalize.to_sym end
[ show source ]
# File lib/facets/core/symbol/capitalized.rb, line 7 def capitalized? to_s.capitalized? end
[ show source ]
# File lib/facets/core/symbol/downcase.rb, line 6 def downcase to_s.downcase.to_sym end
[ show source ]
# File lib/facets/core/symbol/downcase.rb, line 10 def downcase? to_s.downcase? end
[ show source ]
# File lib/facets/core/symbol/not.rb, line 4 def not? self.to_s.slice(0,1) == '~' end
Easily manipulate undercores on symbols.
:a.pad(2) #=> :__a__ :__a__.pad(-1) #=> :_a_
[ show source ]
# File lib/facets/core/symbol/pad.rb, line 9 def pad(i=1) return self if i == 0 s = self.to_s if i > 0 return ( ('_'*i) + self.to_s + ('_'*i) ).to_sym else i *= -1 return s[i..-i-1].to_sym if s[0..i-1] == ('_'*i) and s[-i..-1] == ('_'*i) return self end end
Successor method for symobol. This simply converts the symbol to a string uses String#succ and then converts it back to a symbol.
:a.succ => :b
[ show source ]
# File lib/facets/core/symbol/succ.rb, line 14 def succ self.to_s.succ.intern end
Get a constant by a given symbol name.
:Class.to_const #=> Class
Note this method is not as verstile as it should be, since it can not access contants relative to the current execution context. But without a binding_of_caller that does not seem possible.
[ show source ]
# File lib/facets/core/symbol/to_const.rb, line 13 def to_const to_s.to_const end
Turn a symbol into a proc calling the method to which it refers.
up = :upcase.to_proc up.call("hello") #=> HELLO
More useful is the fact that this allows & to be used to coerce Symbol into Proc.
%w{foo bar qux}.map(&:upcase) #=> ["FOO","BAR","QUX"] [1, 2, 3].inject(&:+) #=> 6
[ show source ]
# File lib/facets/core/symbol/to_proc.rb, line 21 def to_proc Proc.new{|*args| args.shift.__send__(self, *args)} end
Symbol’s really are just simplified strings. Thus to_str seems quite reasonable. This uses the Kernal#String method.
[ show source ]
# File lib/facets/core/symbol/to_str.rb, line 11 def to_str String( self ) end
[ show source ]
# File lib/facets/core/symbol/underscore.rb, line 6 def underscore self.to_s.underscore.to_sym end
[ show source ]
# File lib/facets/core/symbol/upcase.rb, line 6 def upcase to_s.upcase.to_sym end
[ show source ]
# File lib/facets/core/symbol/upcase.rb, line 10 def upcase? to_s.upcase? end
[ show source ]
# File lib/facets/core/symbol/not.rb, line 8 def ~@ if self.to_s.slice(0,1) == '~' "#{self.to_s[1..-1]}".to_sym else "~#{self}".to_sym end end