Class: Keisan::AST::String

Inherits:
ConstantLiteral show all
Defined in:
lib/keisan/ast/string.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ConstantLiteral

#==, #evaluate

Methods inherited from Node

#!, #%, #&, #*, #**, #+@, #-, #-@, #/, #<, #<<, #<=, #>, #>=, #>>, #^, #and, #coerce, #contains_a?, #deep_dup, #differentiate, #differentiated, #evaluate, #evaluate_assignments, #evaluated, #false?, #or, #replace, #replaced, #simplified, #simplify, #to_cell, #to_node, #traverse, #true?, #unbound_functions, #unbound_variables, #well_defined?, #|, #~

Constructor Details

#initialize(content) ⇒ String

Returns a new instance of String.



6
7
8
# File 'lib/keisan/ast/string.rb', line 6

def initialize(content)
  @content = content
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



4
5
6
# File 'lib/keisan/ast/string.rb', line 4

def content
  @content
end

Instance Method Details

#+(other) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/keisan/ast/string.rb', line 14

def +(other)
  case other
  when String
    String.new(value + other.value)
  else
    raise Exceptions::TypeError.new("#{other}'s type is invalid, #{other.class}")
  end
end

#equal(other) ⇒ Object



31
32
33
34
# File 'lib/keisan/ast/string.rb', line 31

def equal(other)
  other = other.to_node
  other.is_a?(AST::String) ? Boolean.new(value == other.value) : super
end

#not_equal(other) ⇒ Object



36
37
38
39
# File 'lib/keisan/ast/string.rb', line 36

def not_equal(other)
  other = other.to_node
  other.is_a?(AST::String) ? Boolean.new(value != other.value) : super
end

#to_sObject



23
24
25
26
27
28
29
# File 'lib/keisan/ast/string.rb', line 23

def to_s
  if value =~ /\"/
    "\"#{value.gsub("\"", "\\\"")}\""
  else
    "\"#{value}\""
  end
end

#value(context = nil) ⇒ Object



10
11
12
# File 'lib/keisan/ast/string.rb', line 10

def value(context = nil)
  content
end