Class: Code::Object::String

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

Constant Summary

Constants inherited from Code::Object

NUMBER_CLASSES

Instance Attribute Summary

Attributes inherited from Code::Object

#raw

Instance Method Summary collapse

Methods inherited from Code::Object

#<=>, #==, as_json, #as_json, call, #code_and_operator, code_and_operator, #code_as_json, code_as_json, 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_new, code_or_operator, #code_or_operator, code_self, #code_self, #code_to_json, code_to_json, falsy?, #falsy?, #hash, inspect, maybe, multi_fetch, #multi_fetch, #nothing?, nothing?, repeat, sig, #sig, #succ, #to_code, to_json, #to_json, to_s, truthy?, #truthy?, |

Constructor Details

#initialize(*args, **_kargs) ⇒ String

Returns a new instance of String.



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

def initialize(*args, **_kargs, &)
  raw = args.first || Nothing.new
  raw = raw.raw if raw.is_a?(Object)
  @raw = raw.to_s
end

Instance Method Details

#call(**args) ⇒ Object



12
13
14
15
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
# File 'lib/code/object/string.rb', line 12

def call(**args)
  code_operator = args.fetch(:operator, nil).to_code
  code_arguments = args.fetch(:arguments, List.new).to_code
  globals = multi_fetch(args, *GLOBALS)
  code_value = code_arguments.code_first

  case code_operator.to_s
  when "&", "to_function"
    sig(args)
    code_to_function(**globals)
  when "*"
    sig(args) { Integer | Decimal }
    code_multiplication(code_value)
  when "+"
    sig(args) { Object }
    code_plus(code_value)
  when "downcase"
    sig(args)
    code_downcase
  when "include?"
    sig(args) { String }
    code_include?(code_value)
  when "first"
    sig(args) { Integer.maybe }
    code_first(code_value)
  when "reverse"
    sig(args)
    code_reverse
  else
    super
  end
end

#code_downcaseObject



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

def code_downcase
  String.new(raw.downcase)
end

#code_first(n = nil) ⇒ Object



72
73
74
75
76
# File 'lib/code/object/string.rb', line 72

def code_first(n = nil)
  code_n = n.to_code
  code_n = Integer.new(1) if code_n.nothing?
  String.new(raw.first(code_n.raw))
end

#code_include?(value) ⇒ Boolean

Returns:



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

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

#code_multiplication(other) ⇒ Object



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

def code_multiplication(other)
  code_other = other.to_code
  String.new(raw * code_other.raw)
end

#code_plus(other) ⇒ Object



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

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

#code_reverseObject



64
65
66
# File 'lib/code/object/string.rb', line 64

def code_reverse
  String.new(raw.reverse)
end

#code_to_function(**_globals) ⇒ Object



68
69
70
# File 'lib/code/object/string.rb', line 68

def code_to_function(**_globals)
  Function.new([{ name: "_" }], "_.#{raw}")
end