Class: Code::Object::Range
Instance Attribute Summary collapse
Attributes inherited from Code::Object
#raw
Class Method Summary
collapse
Instance Method Summary
collapse
#<=>, #==, 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_new, code_or_operator, #code_or_operator, code_self, #code_self, #code_to_string, code_to_string, falsy?, #falsy?, #hash, inspect, maybe, multi_fetch, #multi_fetch, repeat, #sig, sig, #to_json, to_s, truthy?, #truthy?, |
Constructor Details
#initialize(*args, **_kargs, &_block) ⇒ Range
Returns a new instance of Range.
8
9
10
11
12
13
14
15
|
# File 'lib/code/object/range.rb', line 8
def initialize(*args, **_kargs, &_block)
@left = args.first.presence || Integer.new(0)
@right = args.second.presence || Integer.new(0)
@options = Dictionary.new(args.third.presence || Dictionary.new)
@exclude_end = Boolean.new(@options.code_get(String.new(:exclude_end)))
@raw = ::Range.new(left, right, exclude_end?)
super
end
|
Instance Attribute Details
#exclude_end ⇒ Object
Returns the value of attribute exclude_end.
6
7
8
|
# File 'lib/code/object/range.rb', line 6
def exclude_end
@exclude_end
end
|
Returns the value of attribute left.
6
7
8
|
# File 'lib/code/object/range.rb', line 6
def left
@left
end
|
Returns the value of attribute options.
6
7
8
|
# File 'lib/code/object/range.rb', line 6
def options
@options
end
|
Returns the value of attribute right.
6
7
8
|
# File 'lib/code/object/range.rb', line 6
def right
@right
end
|
Class Method Details
17
18
19
|
# File 'lib/code/object/range.rb', line 17
def self.name
"Range"
end
|
Instance Method Details
144
145
146
|
# File 'lib/code/object/range.rb', line 144
def as_json(...)
raw.as_json(...)
end
|
#call(**args) ⇒ Object
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
50
51
52
53
54
55
56
57
58
|
# File 'lib/code/object/range.rb', line 21
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 "all?"
sig(args) { Function }
code_all?(value, **globals)
when "any?"
sig(args) { Function }
code_any?(value, **globals)
when "each"
sig(args) { Function }
code_each(value, **globals)
when "first"
sig(args)
code_first
when "last"
sig(args)
code_last
when "map"
sig(args) { Function }
code_map(value, **globals)
when "select"
sig(args) { Function }
code_select(value, **globals)
when "step"
sig(args) { Integer | Decimal }
code_step(value)
when "to_list"
sig(args)
code_to_list
else
super
end
end
|
#code_all?(argument, **globals) ⇒ Boolean
60
61
62
63
64
65
66
|
# File 'lib/code/object/range.rb', line 60
def code_all?(argument, **globals)
Boolean.new(
raw.all? do |element|
argument.call(arguments: [Argument.new(element)], **globals).truthy?
end
)
end
|
#code_any?(argument, **globals) ⇒ Boolean
68
69
70
71
72
73
74
|
# File 'lib/code/object/range.rb', line 68
def code_any?(argument, **globals)
Boolean.new(
raw.any? do |element|
argument.call(arguments: [Argument.new(element)], **globals).truthy?
end
)
end
|
#code_each(argument, **globals) ⇒ Object
76
77
78
79
80
81
|
# File 'lib/code/object/range.rb', line 76
def code_each(argument, **globals)
raw.each do |element|
argument.call(arguments: [Argument.new(element)], **globals)
end
self
end
|
#code_first ⇒ Object
83
84
85
|
# File 'lib/code/object/range.rb', line 83
def code_first
raw.first || Nothing.new
end
|
#code_last ⇒ Object
87
88
89
|
# File 'lib/code/object/range.rb', line 87
def code_last
raw.last || Nothing.new
end
|
#code_map(argument, **globals) ⇒ Object
91
92
93
94
95
96
97
|
# File 'lib/code/object/range.rb', line 91
def code_map(argument, **globals)
List.new(
raw.map do |element|
argument.call(arguments: [Argument.new(element)], **globals)
end
)
end
|
#code_select(argument, **globals) ⇒ Object
99
100
101
102
103
104
105
|
# File 'lib/code/object/range.rb', line 99
def code_select(argument, **globals)
List.new(
raw.select do |element|
argument.call(arguments: [Argument.new(element)], **globals).truthy?
end
)
end
|
#code_step(argument) ⇒ Object
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
# File 'lib/code/object/range.rb', line 111
def code_step(argument)
list = List.new
element = left
list.code_append(element)
element = element.code_plus(argument)
if exclude_end?
while element.code_inferior(right).truthy?
list.code_append(element)
element = element.code_plus(argument)
end
else
while element.code_inferior_or_equal(right).truthy?
list.code_append(element)
element = element.code_plus(argument)
end
end
list
end
|
#code_to_list ⇒ Object
132
133
134
|
# File 'lib/code/object/range.rb', line 132
def code_to_list
List.new(raw.to_a)
end
|
#exclude_end? ⇒ Boolean
107
108
109
|
# File 'lib/code/object/range.rb', line 107
def exclude_end?
exclude_end.truthy?
end
|
136
137
138
|
# File 'lib/code/object/range.rb', line 136
def inspect
to_s
end
|
140
141
142
|
# File 'lib/code/object/range.rb', line 140
def to_s
raw.to_s
end
|