Class: Rouge::Symbol

Inherits:
Object
  • Object
show all
Includes:
Metadata
Defined in:
lib/rouge/symbol.rb

Constant Summary collapse

LOOKUP =

The symbols for t/f/n are the Ruby objects themselves.

{
  :true => true,
  :false => false,
  :nil => nil,
}
KNOWNS =
{
  :/ => [nil, :/],
  :"./" => [nil, :"./"],
  :"rouge.core//" => [:"rouge.core", :/],
  :"rouge.core/./" => [:"rouge.core", :"./"]
}
CACHE =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Metadata

#meta, #meta=

Constructor Details

#initialize(sym) ⇒ Symbol

Returns a new instance of Symbol.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rouge/symbol.rb', line 25

def initialize(sym)
  if r = KNOWNS[sym]
    @ns = r[0]
    @name = r[1]
  else
    str = sym.to_s
    solidus = str.rindex('/')
    if solidus
      @ns = str[0...solidus].intern
      @name = str[solidus + 1..-1].intern
    else
      @ns = nil
      @name = sym
    end
  end

  @ns_s = @ns.to_s unless @ns.nil?
  @name_s = @name.to_s

  # split(sep, 0) means a trailing '.' won't become an empty component.  (0
  # is default)  Contrast with split(sep, -1).
  @name_parts =
    @name_s.length > 1 ? @name_s.split('.', 0).map(&:intern) : [@name]

  @new_sym = (@name_s[-1] == ?. and @name_s.length > 1)
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/rouge/symbol.rb', line 7

def name
  @name
end

#name_partsObject (readonly)

Returns the value of attribute name_parts.



7
8
9
# File 'lib/rouge/symbol.rb', line 7

def name_parts
  @name_parts
end

#name_sObject (readonly)

Returns the value of attribute name_s.



7
8
9
# File 'lib/rouge/symbol.rb', line 7

def name_s
  @name_s
end

#new_symObject (readonly)

Returns the value of attribute new_sym.



7
8
9
# File 'lib/rouge/symbol.rb', line 7

def new_sym
  @new_sym
end

#nsObject (readonly)

Returns the value of attribute ns.



7
8
9
# File 'lib/rouge/symbol.rb', line 7

def ns
  @ns
end

#ns_sObject (readonly)

Returns the value of attribute ns_s.



7
8
9
# File 'lib/rouge/symbol.rb', line 7

def ns_s
  @ns_s
end

Class Method Details

.[](inner) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'lib/rouge/symbol.rb', line 52

def self.[](inner)
  return LOOKUP[inner] if LOOKUP.include? inner
  c = CACHE[inner]
  return c.dup if c
  # Note: don't cache symbols themselves, they may have metadata.
  c = new inner
  CACHE[inner] = c.dup.freeze
  c
end

Instance Method Details

#==(right) ⇒ Object



74
75
76
# File 'lib/rouge/symbol.rb', line 74

def ==(right)
  right.is_a?(Rouge::Symbol) and right.ns == @ns and right.name == @name
end

#inspectObject



66
67
68
# File 'lib/rouge/symbol.rb', line 66

def inspect
  "Rouge::Symbol[#{to_sym.inspect}]"
end

#to_sObject



70
71
72
# File 'lib/rouge/symbol.rb', line 70

def to_s
  "#{@ns ? "#@ns/" : ""}#@name"
end

#to_symObject



62
63
64
# File 'lib/rouge/symbol.rb', line 62

def to_sym
  :"#{to_s}"
end