Class: Code::Object::String

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Code::Object

#<=>, #==, #falsy?, #hash, #truthy?

Constructor Details

#initialize(string) ⇒ String

Returns a new instance of String.



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

def initialize(string)
  @raw = string
end

Instance Attribute Details

#rawObject (readonly)

Returns the value of attribute raw.



4
5
6
# File 'lib/code/object/string.rb', line 4

def raw
  @raw
end

Instance Method Details

#call(**args) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/code/object/string.rb', line 10

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

  if operator == "&" || operator == "to_function"
    sig(arguments)
    to_function(**globals)
  elsif operator == "+"
    sig(arguments) { ::Code::Object }
    plus(value)
  elsif operator == "*"
    sig(arguments) { ::Code::Object::Number }
    multiplication(value)
  elsif operator == "reverse"
    sig(arguments)
    reverse
  elsif operator == "include?"
    sig(arguments) { ::Code::Object::String }
    include?(value)
  else
    super
  end
end

#inspectObject



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

def inspect
  raw.inspect
end

#succObject



36
37
38
# File 'lib/code/object/string.rb', line 36

def succ
  ::Code::Object::String.new(raw.succ)
end

#to_sObject



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

def to_s
  raw
end

#to_symObject



40
41
42
# File 'lib/code/object/string.rb', line 40

def to_sym
  raw.to_sym
end