Class: Code::Object::Range
Constant Summary
Constants inherited
from Code::Object
NUMBER_CLASSES
Instance Attribute Summary collapse
#methods, #raw
Instance Method Summary
collapse
code_new, #code_new, maybe, #name, repeat, |
#<=>, #==, #as_json, #code_and, #code_as_json, #code_deep_duplicate, #code_different, #code_duplicate, #code_equal_equal, #code_equal_equal_equal, #code_exclamation_point, #code_exclusive_range, #code_falsy?, #code_fetch, code_fetch, code_get, #code_get, #code_inclusive_range, #code_inspect, #code_methods, #code_name, #code_or, #code_self, #code_set, code_set, #code_to_boolean, #code_to_class, #code_to_date, #code_to_decimal, #code_to_dictionary, #code_to_duration, #code_to_integer, #code_to_json, #code_to_nothing, #code_to_parameter, #code_to_range, #code_to_string, #code_to_time, #code_truthy?, #eql?, #falsy?, #hash, #inspect, #multi_fetch, #nothing?, #sig, #succ, #to_code, #to_i, #to_json, #to_s, #truthy?
Constructor Details
#initialize(*args, **kargs, &_block) ⇒ Range
Returns a new instance of Range.
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
# File 'lib/code/object/range.rb', line 8
def initialize(*args, **kargs, &_block)
if args.first.is_a?(Range)
@code_left = args.first.code_left
@code_right = args.first.code_right
@code_options = args.first.code_options
@code_exclude_end = args.first.code_exclude_end
else
@code_left =
(args.first.to_code.nothing? ? Integer.new(0) : args.first.to_code)
@code_right =
if args.second.to_code.nothing?
Integer.new(0)
else
args.second.to_code
end
@code_options = Dictionary.new(args.third.presence || kargs)
@code_exclude_end = Boolean.new(code_options.code_get(:exclude_end))
end
self.raw = ::Range.new(code_left, code_right, exclude_end?)
end
|
Instance Attribute Details
#code_exclude_end ⇒ Object
Returns the value of attribute code_exclude_end.
6
7
8
|
# File 'lib/code/object/range.rb', line 6
def code_exclude_end
@code_exclude_end
end
|
#code_left ⇒ Object
Returns the value of attribute code_left.
6
7
8
|
# File 'lib/code/object/range.rb', line 6
def code_left
@code_left
end
|
#code_options ⇒ Object
Returns the value of attribute code_options.
6
7
8
|
# File 'lib/code/object/range.rb', line 6
def code_options
@code_options
end
|
#code_right ⇒ Object
Returns the value of attribute code_right.
6
7
8
|
# File 'lib/code/object/range.rb', line 6
def code_right
@code_right
end
|
Instance Method Details
#call(**args) ⇒ Object
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
# File 'lib/code/object/range.rb', line 32
def call(**args)
code_operator = args.fetch(:operator, nil).to_code
code_arguments = args.fetch(:arguments, []).to_code
globals = multi_fetch(args, *GLOBALS)
code_value = code_arguments.code_first
case code_operator.to_s
when "all?"
sig(args) { Function }
code_all?(code_value, **globals)
when "any?"
sig(args) { Function }
code_any?(code_value, **globals)
when "each"
sig(args) { Function }
code_each(code_value, **globals)
when "first"
sig(args)
code_first
when "last"
sig(args)
code_last
when "map"
sig(args) { Function }
code_map(code_value, **globals)
when "select"
sig(args) { Function }
code_select(code_value, **globals)
when "step"
sig(args) { Integer | Decimal }
code_step(code_value)
when "sample"
sig(args)
code_sample
when "to_list"
sig(args)
code_to_list
else
super
end
end
|
#code_all?(argument, **globals) ⇒ Boolean
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
# File 'lib/code/object/range.rb', line 74
def code_all?(argument, **globals)
code_argument = argument.to_code
index = 0
Boolean.new(
raw.all? do |code_element|
code_argument
.call(
arguments: List.new([code_element, Integer.new(index), self]),
**globals
)
.truthy?
.tap { index += 1 }
end
)
end
|
#code_any?(argument, **globals) ⇒ Boolean
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
# File 'lib/code/object/range.rb', line 92
def code_any?(argument, **globals)
code_argument = argument.to_code
index = 0
Boolean.new(
raw.any? do |code_element|
code_argument
.call(
arguments: List.new([code_element, Integer.new(index), self]),
**globals
)
.truthy?
.tap { index += 1 }
end
)
end
|
#code_each(argument, **globals) ⇒ Object
110
111
112
113
114
115
116
117
118
119
120
121
|
# File 'lib/code/object/range.rb', line 110
def code_each(argument, **globals)
code_argument = argument.to_code
raw.each.with_index do |code_element, index|
code_argument.call(
arguments: List.new([code_element, Integer.new(index), self]),
**globals
)
end
self
end
|
#code_first ⇒ Object
123
124
125
|
# File 'lib/code/object/range.rb', line 123
def code_first
raw.first || Nothing.new
end
|
#code_last ⇒ Object
127
128
129
|
# File 'lib/code/object/range.rb', line 127
def code_last
raw.last || Nothing.new
end
|
#code_map(argument, **globals) ⇒ Object
131
132
133
134
135
136
137
138
139
140
141
142
|
# File 'lib/code/object/range.rb', line 131
def code_map(argument, **globals)
code_argument = argument.to_code
List.new(
raw.map.with_index do |code_element, index|
code_argument.call(
arguments: List.new([code_element, Integer.new(index), self]),
**globals
)
end
)
end
|
#code_sample ⇒ Object
189
190
191
|
# File 'lib/code/object/range.rb', line 189
def code_sample
code_to_list.code_sample
end
|
#code_select(argument, **globals) ⇒ Object
144
145
146
147
148
149
150
151
152
153
154
155
|
# File 'lib/code/object/range.rb', line 144
def code_select(argument, **globals)
code_argument = argument.to_code
List.new(
raw.select.with_index do |code_element, index|
code_argument.call(
arguments: List.new([code_element, Integer.new(index), self]),
**globals
).truthy?
end
)
end
|
#code_step(argument) ⇒ Object
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
|
# File 'lib/code/object/range.rb', line 161
def code_step(argument)
code_argument = argument.to_code
code_list = List.new
code_element = code_left
code_list.code_append(code_element)
code_element = code_element.code_plus(code_argument)
if exclude_end?
while code_element.code_inferior(code_right).truthy?
code_list.code_append(code_element)
code_element = code_element.code_plus(code_argument)
end
else
while code_element.code_inferior_or_equal(code_right).truthy?
code_list.code_append(code_element)
code_element = code_element.code_plus(code_argument)
end
end
code_list
end
|
#code_to_list ⇒ Object
185
186
187
|
# File 'lib/code/object/range.rb', line 185
def code_to_list
List.new(raw.to_a)
end
|
#exclude_end? ⇒ Boolean
157
158
159
|
# File 'lib/code/object/range.rb', line 157
def exclude_end?
code_exclude_end.truthy?
end
|