Class: Symbol
- Defined in:
- lib/nano/symbol/to_proc.rb,
lib/nano/symbol/not.rb,
lib/nano/symbol/pad.rb,
lib/nano/symbol/succ.rb,
lib/nano/symbol/to_str.rb,
lib/nano/symbol/camelize.rb,
lib/nano/symbol/to_const.rb,
lib/nano/symbol/camelcase.rb,
lib/nano/symbol/upcase%3F.rb,
lib/nano/symbol/underscore.rb,
lib/nano/symbol/downcase%3F.rb,
lib/nano/symbol/capitalized%3F.rb
Overview
– Credit goes to Florian Gross (flgr). ++
Instance Method Summary collapse
-
#camelcase(*args) ⇒ Object
Converts a symbol to camelcase.
- #camelize(*args) ⇒ Object
- #capitalized? ⇒ Boolean
- #downcase? ⇒ Boolean
- #not? ⇒ Boolean
-
#pad(i = 1) ⇒ Object
Easily manipulate undercores on symbols.
-
#succ ⇒ Object
Successor method for symobol.
-
#to_const ⇒ Object
Get a constant by a given symbol name.
-
#to_proc ⇒ Object
Turn a symbol into a proc calling the method to which it refers.
-
#to_str ⇒ Object
Symbol’s really are just simplified strings.
- #underscore ⇒ Object
- #upcase? ⇒ Boolean
- #~@ ⇒ Object
Instance Method Details
#camelcase(*args) ⇒ Object
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
17 18 19 |
# File 'lib/nano/symbol/camelcase.rb', line 17 def camelcase( *args ) self.to_s.camelcase( *args ).to_sym end |
#camelize(*args) ⇒ Object
6 7 8 |
# File 'lib/nano/symbol/camelize.rb', line 6 def camelize( *args ) self.to_s.camelize( *args ).to_sym end |
#capitalized? ⇒ Boolean
6 7 8 |
# File 'lib/nano/symbol/capitalized%3F.rb', line 6 def capitalized? self.to_s.capitalized? end |
#downcase? ⇒ Boolean
6 7 8 |
# File 'lib/nano/symbol/downcase%3F.rb', line 6 def downcase? self.to_s.downcase? end |
#not? ⇒ Boolean
4 5 6 |
# File 'lib/nano/symbol/not.rb', line 4 def not? self.to_s.slice(0,1) == '~' end |
#pad(i = 1) ⇒ Object
Easily manipulate undercores on symbols.
:a.pad(2) #=> :__a__
:__a__.pad(-1) #=> :_a_
8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/nano/symbol/pad.rb', line 8 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 |
#succ ⇒ Object
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
– In the future I would like this to work more like a simple chracerter dial. ++
14 15 16 |
# File 'lib/nano/symbol/succ.rb', line 14 def succ self.to_s.succ.intern end |
#to_const ⇒ Object
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.
11 12 13 |
# File 'lib/nano/symbol/to_const.rb', line 11 def to_const to_s.to_const end |
#to_proc ⇒ Object
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
17 18 19 |
# File 'lib/nano/symbol/to_proc.rb', line 17 def to_proc lambda { |obj, *args| obj.send(self, *args) } end |
#to_str ⇒ Object
Symbol’s really are just simplified strings. Thus #to_str seems quite reasonable. This uses the Kernal#String method. – Maybe just change to #to_s? BTW: This would be lots faster, I bet, if implemented in core. ++
9 10 11 |
# File 'lib/nano/symbol/to_str.rb', line 9 def to_str String( self ) end |
#underscore ⇒ Object
6 7 8 |
# File 'lib/nano/symbol/underscore.rb', line 6 def underscore self.to_s.underscore.to_sym end |
#upcase? ⇒ Boolean
6 7 8 |
# File 'lib/nano/symbol/upcase%3F.rb', line 6 def upcase? self.to_s.upcase? end |
#~@ ⇒ Object
8 9 10 11 12 13 14 |
# File 'lib/nano/symbol/not.rb', line 8 def ~@ if self.to_s.slice(0,1) == '~' :"#{self.to_s[1..-1]}" else :"~#{self}" end end |