Class Symbol
In: lib/facets/core/facets/object/dup.rb
lib/facets/core/facets/symbol/op_div.rb
lib/facets/core/facets/symbol/thrown.rb
lib/facets/core/facets/symbol/chomp.rb
lib/facets/core/facets/symbol/variablize.rb
lib/facets/core/facets/symbol/to_proc.rb
lib/facets/core/facets/symbol/as_s.rb
lib/facets/core/facets/symbol/succ.rb
lib/facets/core/facets/symbol/plain.rb
lib/facets/core/facets/symbol/not.rb
lib/facets/core/facets/symbol/generate.rb
Parent: Object

Methods

/   as_s   bang?   chomp   clone?   dup!   dup?   generate   lchomp   not?   plain?   query?   setter?   succ   thrown?   to_proc   variablize   ~  

Public Class methods

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

[Source]

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

Public Instance methods

Join with path as a file path.

  • path - The path component(s) to append. [to_s]

Examples

  (:merb / "string")   #=> "merb/string"
  (:merb / :symbol)    #=> "merb/symbol"

Returns String of the receiver (as a path string), concatenated with path.

[Source]

# File lib/facets/core/facets/symbol/op_div.rb, line 16
  def /(path)
    File.join(to_s, path.to_s)
  end

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

  :HELLO.as_s.downcase  #=> :hello

[Source]

# File lib/facets/core/facets/symbol/as_s.rb, line 10
  def as_s
    Functor.new do |op, *a|
      to_s.send(op, *a).to_sym
    end
  end

Symbol ends in `!`.

  :a!.bang? #=> true
  :a.bang?  #=> false

[Source]

# File lib/facets/core/facets/symbol/plain.rb, line 38
  def bang?
    to_s[-1,1] == '!'
  end

Just like String#chomp.

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

CREDIT: Trans

[Source]

# File lib/facets/core/facets/symbol/chomp.rb, line 9
  def chomp(seperator)
    to_s.chomp(seperator.to_s).to_sym
  end

[Source]

# File lib/facets/core/facets/object/dup.rb, line 67
  def clone? ; false ; end

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

  :a.dup!  #=> :a

[Source]

# File lib/facets/core/facets/object/dup.rb, line 65
  def dup!   ; self  ; end

[Source]

# File lib/facets/core/facets/object/dup.rb, line 66
  def dup?   ; false ; end

Just like String#lchomp.

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

CREDIT: Trans

[Source]

# File lib/facets/core/facets/symbol/chomp.rb, line 19
  def lchomp(seperator)
    to_s.reverse.chomp(seperator.to_s).reverse.to_sym
  end

Does a symbol have a "not" sign?

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

CREDIT: Trans

[Source]

# File lib/facets/core/facets/symbol/not.rb, line 10
  def not?
    self.to_s.slice(0,1) == '~'
  end

Symbol does not end in `!`, `=`, or `?`.

  :a.plain?   #=> true
  :a?.plain?  #=> false
  :a!.plain?  #=> false
  :a=.plain?  #=> false

[Source]

# File lib/facets/core/facets/symbol/plain.rb, line 10
  def plain?
    c = to_s[-1,1]
    !(c == '=' || c == '?' || c == '!')
  end

Symbol ends in `?`.

  :a?.query? #=> true
  :a.query?  #=> false

[Source]

# File lib/facets/core/facets/symbol/plain.rb, line 29
  def query?
    to_s[-1,1] == '?'
  end

Symbol ends in `=`.

  :a=.setter? #=> true
  :a.setter?  #=> false

[Source]

# File lib/facets/core/facets/symbol/plain.rb, line 20
  def setter?
    to_s[-1,1] == '='
  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

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

[Source]

# File lib/facets/core/facets/symbol/succ.rb, line 13
    def succ(n=1)
      s = self.to_s
      n.times do
        s = s.succ
      end
      s.to_sym
    end

Does the block throw the symbol?

[Source]

# File lib/facets/core/facets/symbol/thrown.rb, line 5
  def thrown?
    catch(self) do
      begin
        yield
        true
      rescue ArgumentError => err     # 1.9 exception
        false  #msg += ", not #{err.message.split(/ /).last}"
      rescue NameError => err         # 1.8 exception
        false  #msg += ", not #{err.name.inspect}"
      end
    end
  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

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

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

[Source]

# File lib/facets/core/facets/symbol/to_proc.rb, line 20
    def to_proc
      Proc.new{|*args| args.shift.__send__(self, *args)}
    end

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

  :a.variablize  #=> :"@a"

[Source]

# File lib/facets/core/facets/symbol/variablize.rb, line 8
  def variablize
    name = to_s.gsub(/\W/, '_')
    "@#{name}".to_sym
  end

Add a "not" sign to the front of a symbol.

  (~:friend)  #=> :"~friend"

CREDIT: Trans

[Source]

# File lib/facets/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

[Validate]