Class: Code::Object::Range

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Code::Object

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

Constructor Details

#initialize(left, right, exclude_end: false) ⇒ Range

Returns a new instance of Range.



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

def initialize(left, right, exclude_end: false)
  @left = left
  @right = right
  @exclude_end = exclude_end
  @raw = ::Range.new(left, right, exclude_end)
end

Instance Attribute Details

#rawObject (readonly)

Returns the value of attribute raw.



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

def raw
  @raw
end

Instance Method Details

#call(**args) ⇒ Object



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
44
45
46
47
48
49
# File 'lib/code/object/range.rb', line 13

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

  if operator == "any?"
    sig(arguments) { ::Code::Object::Function }
    any?(value, **globals)
  elsif operator == "all?"
    sig(arguments) { ::Code::Object::Function }
    all?(value, **globals)
  elsif operator == "each"
    sig(arguments) { ::Code::Object::Function }
    each(value, **globals)
  elsif operator == "select"
    sig(arguments) { ::Code::Object::Function }
    select(value, **globals)
  elsif operator == "map"
    sig(arguments) { ::Code::Object::Function }
    map(value, **globals)
  elsif operator == "step"
    sig(arguments) { ::Code::Object::Number }
    step(value)
  elsif operator == "to_list"
    sig(arguments)
    to_list
  elsif operator == "first"
    sig(arguments)
    first
  elsif operator == "last"
    sig(arguments)
    last
  else
    super
  end
end

#inspectObject



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

def inspect
  to_s
end

#to_sObject



51
52
53
# File 'lib/code/object/range.rb', line 51

def to_s
  raw.to_s
end