Parent

Symbol

Public Class Methods

generate(key=nil) click to toggle source

Generate a unique symbol.

  Symbol.generate => :-1

If key is given the new symbol will be prefixed with it.

  Symbol.generate(:foo) => :foo-1

  TODO: Is the generated symbol format acceptable?

CREDIT: Trans

    # File lib/core/facets/symbol/generate.rb, line 15
15:   def self.generate(key=nil)
16:     key = key.to_sym if key
17:     @symbol_generate_counter ||= {}
18:     @symbol_generate_counter[key] ||= 0
19:     num = @symbol_generate_counter[key] += 1
20:     ("#{key}-%X" % num).to_sym
21:   end

Public Instance Methods

/(path) click to toggle source

Join with path as a file path.

  :merb / "core_ext"           #=> "merb/core_ext"
  :merb / :core_ext / :string  #=> "merb/core_ext/string"

@param [#] path The path component(s) to append.

@return [String] The receiver (as path string), concatenated with path.

@api public

    # File lib/core/facets/symbol/op_div.rb, line 14
14:   def /(path)
15:     File.join(to_s, path.to_s)
16:   end
chomp(seperator) click to toggle source

Just like String#chomp.

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

CREDIT: Trans

    # File lib/core/facets/symbol/chomp.rb, line 9
 9:   def chomp(seperator)
10:     to_s.chomp(seperator.to_s).to_sym
11:   end
clone?() click to toggle source
    # File lib/core/facets/duplicable.rb, line 25
25:   def clone? ; false ; end
dup?() click to toggle source
    # File lib/core/facets/duplicable.rb, line 24
24:   def dup?   ; false ; end
lchomp(seperator) click to toggle source

Just like String#lchomp.

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

CREDIT: Trans

    # File lib/core/facets/symbol/chomp.rb, line 19
19:   def lchomp(seperator)
20:     to_s.reverse.chomp(seperator.to_s).reverse.to_sym
21:   end
not?() click to toggle source

Does a symbol have a “not” sign?

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

CREDIT: Trans

    # File lib/core/facets/symbol/not.rb, line 10
10:   def not?
11:     self.to_s.slice(0,1) == '~'
12:   end
plain?() click to toggle source
   # File lib/core/facets/symbol/plain.rb, line 3
3:   def plain?
4:     c = to_s[1,1]
5:     !(c == '=' || c == '?')
6:   end
query?() click to toggle source
    # File lib/core/facets/symbol/plain.rb, line 12
12:   def query?
13:     to_s[1,1] == '?'
14:   end
re_s() click to toggle source

Convert symbol to string, apply string method and convert back to symbol via a fluent interface.

  :HELLO.re_s.downcase
    # File lib/core/facets/symbol/re_s.rb, line 11
11:   def re_s
12:     @re_s ||= Functor.new do |op, *a|
13:       to_s.send(op, *a).to_sym
14:     end
15:   end
setter?() click to toggle source
    # File lib/core/facets/symbol/plain.rb, line 8
 8:   def setter?
 9:     to_s[1,1] == '='
10:   end
succ() click to toggle source

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?

    # File lib/core/facets/symbol/succ.rb, line 13
13:     def succ
14:       self.to_s.succ.intern
15:     end
thrown?() click to toggle source

Does the block throw the symbol?

    # File lib/core/facets/symbol/thrown.rb, line 5
 5:   def thrown?
 6:     catch(self) do
 7:       begin
 8:         yield
 9:         true
10:       rescue ArgumentError => err     # 1.9 exception
11:         #msg += ", not #{err.message.split(/ /).last}"
12:         false
13:       rescue NameError => err         # 1.8 exception
14:         #msg += ", not #{err.name.inspect}"
15:         false
16:       end
17:     end
18:   end
to_proc() click to toggle source

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), Nobuhiro Imai (current)

    # File lib/core/facets/symbol/to_proc.rb, line 26
26:     def to_proc
27:       Proc.new{|*args| args.shift.__send__(self, *args)}
28:     end
try_dup() click to toggle source

Since Symbol is immutable it cannot be duplicated. For this reason # returns self.

    # File lib/core/facets/kernel/try_dup.rb, line 51
51:   def try_dup
52:     self
53:   end
variablize() click to toggle source

Prepend an “@” to the beginning of a symbol to make a instance variable name. This also replaces non-valid characters with underscores.

   # File lib/core/facets/symbol/variablize.rb, line 7
7:   def variablize
8:     "@#{self}".gsub(/\W/, '_').to_sym
9:   end
~() click to toggle source

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

  ~:friend    #=> :"~friend"

CREDIT: Trans

    # File lib/core/facets/symbol/not.rb, line 20
20:   def ~
21:     if self.to_s.slice(0,1) == '~'
22:       "#{self.to_s[1..-1]}".to_sym
23:     else
24:       "~#{self}".to_sym
25:     end
26:   end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.