Class: Symbol

Inherits:
Object show all
Defined in:
lib/more/facets/cut.rb,
lib/more/facets/typecast.rb,
lib/core/facets/conversion.rb,
lib/core/facets/symbol/not.rb,
lib/core/facets/symbol/succ.rb,
lib/core/facets/symbol/chomp.rb,
lib/core/facets/symbol/shadow.rb,
lib/core/facets/symbol/to_proc.rb,
lib/core/facets/symbol/generate.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.cast_from(object) ⇒ Object



190
191
192
193
194
195
# File 'lib/more/facets/typecast.rb', line 190

def cast_from(object)
  return super
rescue TypeCastException
  return object.to_sym if object.respond_to? :to_sym
  raise
end

.generate(suffix = nil) ⇒ Object

Generate a unique symbol.

Symbol.generate => :<1>

If suffix is given the new symbol will be suffixed with it.

Symbol.generate(:this) => :<2>this

TODO: Is the generated symbol format acceptable?

CREDIT: Trans


15
16
17
18
# File 'lib/core/facets/symbol/generate.rb', line 15

def self.generate(suffix=nil)
  @symbol_generate_counter ||= 0
  ("<%X>#{suffix}" % @symbol_generate_counter += 1).to_sym
end

Instance Method Details

#<(klass) ⇒ Object

A little tick to simulate subclassing literal syntax.



158
159
160
161
162
163
164
165
# File 'lib/more/facets/cut.rb', line 158

def <(klass)
  if Class === klass
    [self,klass]
  else
    raise NoMethodError, "undefined method `<' for :#{self}:Symbol"
    #_op_lt_without_cuts(cut_class)
  end
end

#chomp(seperator) ⇒ Object

Just like String#chomp.

:ab.chomp(:b)  #=> :a

CREDIT: Trans


9
10
11
# File 'lib/core/facets/symbol/chomp.rb', line 9

def chomp(seperator)
  to_s.chomp(seperator.to_s).to_sym
end

#lchomp(seperator) ⇒ Object

Just like String#lchomp.

:ab.lchomp(:a)  #=> :b

CREDIT: Trans


19
20
21
# File 'lib/core/facets/symbol/chomp.rb', line 19

def lchomp(seperator)
  to_s.reverse.chomp(seperator.to_s).reverse.to_sym
end

#not?Boolean

Does a symbol have a “not” sign?

"friend".to_sym.not?   #=> false
"~friend".to_sym.not?  #=> true

CREDIT: Trans

Returns:

  • (Boolean)


10
11
12
# File 'lib/core/facets/symbol/not.rb', line 10

def not?
  self.to_s.slice(0,1) == '~'
end

#shadow(i = 1) ⇒ Object Also known as: pad

Easily manipulate undercores on symbols.

:a.pad(2)         #=> :__a__
:__a__.pad(-1)    #=> :_a_

CREDIT: Trans


10
11
12
13
14
15
16
17
18
19
20
# File 'lib/core/facets/symbol/shadow.rb', line 10

def shadow(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

#succObject

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

TODO: Make this  work more like a simple character dial.


14
15
16
# File 'lib/core/facets/symbol/succ.rb', line 14

def succ
  self.to_s.succ.intern
end

#to_constObject

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.



449
450
451
# File 'lib/core/facets/conversion.rb', line 449

def to_const
  to_s.split('::').inject(Object){ |namespace,name| namespace.const_get(name) }
end

#to_procObject

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

And other conveniences such as:

%{john terry fiona}.map(&:capitalize)   # -> %{John Terry Fiona}
sum = numbers.inject(&:+)

TODO: This will be deprecated as of Ruby 1.9, since it will become standard Ruby.

CREDIT: Florian Gross (orignal)
CREDIT: Nobuhiro Imai (current)


434
435
436
# File 'lib/core/facets/conversion.rb', line 434

def to_proc
  Proc.new{|*args| args.shift.__send__(self, *args)}
end

#~@Object

Add a “not” sign to the front of a symbol.

~:friend    #=> :"~friend"

CREDIT: Trans


20
21
22
23
24
25
26
# File 'lib/core/facets/symbol/not.rb', line 20

def ~@
  if self.to_s.slice(0,1) == '~'
    "#{self.to_s[1..-1]}".to_sym
  else
    "~#{self}".to_sym
  end
end