Class: Code::Object::String

Inherits:
Code::Object show all
Defined in:
lib/code/object/string.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Code::Object

#<=>, #==, call, #code_and_operator, code_and_operator, #code_different, code_different, code_equal_equal, #code_equal_equal, #code_equal_equal_equal, code_equal_equal_equal, code_exclamation_point, #code_exclamation_point, code_exclusive_range, #code_exclusive_range, code_inclusive_range, #code_inclusive_range, code_or_operator, #code_or_operator, code_self, #code_self, code_to_string, #code_to_string, falsy?, #falsy?, hash, #hash, maybe, multi_fetch, #multi_fetch, repeat, sig, #sig, to_s, truthy?, #truthy?, |

Constructor Details

#initialize(string) ⇒ String

Returns a new instance of String.



8
9
10
# File 'lib/code/object/string.rb', line 8

def initialize(string)
  @raw = string.to_s
end

Instance Attribute Details

#rawObject (readonly)

Returns the value of attribute raw.



6
7
8
# File 'lib/code/object/string.rb', line 6

def raw
  @raw
end

Class Method Details

.nameObject



12
13
14
# File 'lib/code/object/string.rb', line 12

def self.name
  "String"
end

Instance Method Details

#call(**args) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/code/object/string.rb', line 16

def call(**args)
  operator = args.fetch(:operator, nil)
  arguments = args.fetch(:arguments, [])
  globals = multi_fetch(args, *GLOBALS)
  value = arguments.first&.value

  case operator.to_s
  when "&", "to_function"
    sig(args)
    code_to_function(**globals)
  when "*"
    sig(args) { Number }
    code_multiplication(value)
  when "+"
    sig(args) { Object }
    code_plus(value)
  when "downcase"
    sig(args)
    code_downcase
  when "include?"
    sig(args) { String }
    code_include?(value)
  when "reverse"
    sig(args)
    code_reverse
  else
    super
  end
end

#code_downcaseObject



46
47
48
# File 'lib/code/object/string.rb', line 46

def code_downcase
  String.new(raw.downcase)
end

#code_include?(value) ⇒ Boolean

Returns:



50
51
52
# File 'lib/code/object/string.rb', line 50

def code_include?(value)
  Boolean.new(raw.include?(value.raw))
end

#code_multiplication(other) ⇒ Object



54
55
56
# File 'lib/code/object/string.rb', line 54

def code_multiplication(other)
  String.new(raw * other.raw)
end

#code_plus(other) ⇒ Object



58
59
60
# File 'lib/code/object/string.rb', line 58

def code_plus(other)
  String.new(raw + other.to_s)
end

#code_reverseObject



62
63
64
# File 'lib/code/object/string.rb', line 62

def code_reverse
  String.new(raw.reverse)
end

#code_to_function(**globals) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/code/object/string.rb', line 66

def code_to_function(**globals)
  Code::Node::Code.new(
    [
      {
        function: {
          parameters: [{ name: "_" }],
          body: [
            {
              left_operation: {
                first: {
                  call: {
                    name: "_"
                  }
                },
                others: [
                  { operator: ".", statement: { call: { name: raw } } }
                ]
              }
            }
          ]
        }
      }
    ]
  ).evaluate(**globals)
end

#inspectObject



92
93
94
# File 'lib/code/object/string.rb', line 92

def inspect
  raw.inspect
end

#succObject



96
97
98
# File 'lib/code/object/string.rb', line 96

def succ
  String.new(raw.succ)
end

#to_sObject



100
101
102
# File 'lib/code/object/string.rb', line 100

def to_s
  raw
end