Class: Interscript::Node::Item::String

Inherits:
Interscript::Node::Item show all
Defined in:
lib/interscript/visualize/nodes.rb,
lib/interscript/node/item/string.rb

Instance Attribute Summary collapse

Attributes inherited from Interscript::Node::Item

#item

Instance Method Summary collapse

Methods inherited from Interscript::Node::Item

try_convert

Constructor Details

#initialize(data) ⇒ String

Returns a new instance of String.



3
4
5
# File 'lib/interscript/node/item/string.rb', line 3

def initialize data
  self.data = data
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



2
3
4
# File 'lib/interscript/node/item/string.rb', line 2

def data
  @data
end

Instance Method Details

#+(other) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/interscript/node/item/string.rb', line 25

def + other
  if self.data == ""
    Interscript::Node::Item.try_convert(other)
  elsif Interscript::Node::Item::String === self &&
    (Interscript::Node::Item::String === other || ::String === other)

    other = Interscript::Node::Item.try_convert(other)

    Interscript::Node::Item::String.new(self.data + other.data)
  else
    super
  end
end

#==(other) ⇒ Object



39
40
41
# File 'lib/interscript/node/item/string.rb', line 39

def ==(other)
  super && self.data == other.data
end

#downcaseObject



20
# File 'lib/interscript/node/item/string.rb', line 20

def downcase; self.dup.tap { |i| i.data = i.data.downcase }; end

#first_stringObject Also known as: nth_string



16
17
18
# File 'lib/interscript/node/item/string.rb', line 16

def first_string
  self.data
end

#inspectObject



43
44
45
# File 'lib/interscript/node/item/string.rb', line 43

def inspect
  @data.inspect
end

#max_lengthObject



12
13
14
# File 'lib/interscript/node/item/string.rb', line 12

def max_length
  self.data.length
end

#to_hashObject



7
8
9
10
# File 'lib/interscript/node/item/string.rb', line 7

def to_hash
  { :class => self.class.to_s,
    :data => self.data }
end

#to_html(_) ⇒ Object



78
79
80
81
82
83
84
85
86
87
# File 'lib/interscript/visualize/nodes.rb', line 78

def to_html(_)
  out = ""
  self.data.each_char do |i|
    out << "<ruby>"
    out << "<kbd>#{h i}</kbd>"
    out << "<rt>#{"%04x" % i.ord}</rt>"
    out << "</ruby>"
  end
  out
end

#upcaseObject



21
# File 'lib/interscript/node/item/string.rb', line 21

def upcase; self.dup.tap { |i| i.data = i.data.upcase }; end