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_compare, #code_deep_duplicate, #code_different, #code_duplicate, #code_equal, #code_exclamation_mark, #code_exclusive_range, #code_falsy?, code_fetch, #code_fetch, code_get, #code_get, #code_greater, #code_greater_or_equal, #code_inclusive_range, #code_inspect, #code_less, #code_less_or_equal, #code_methods, #code_name, #code_nothing?, #code_or, #code_self, #code_set, code_set, #code_something?, #code_strict_different, #code_strict_equal, #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, #something?, #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
31
32
33
34
35
|
# 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
elsif args.first.is_a?(List)
@code_left = args.first.code_first
@code_right = args.first.code_last
@code_options = Dictionary.new(args.second.presence || kargs)
@code_exclude_end = Boolean.new(code_options.code_get(: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
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
73
74
75
76
77
|
# File 'lib/code/object/range.rb', line 37
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
# File 'lib/code/object/range.rb', line 79
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
# File 'lib/code/object/range.rb', line 97
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
115
116
117
118
119
120
121
122
123
124
125
126
|
# File 'lib/code/object/range.rb', line 115
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
128
129
130
|
# File 'lib/code/object/range.rb', line 128
def code_first
raw.first || Nothing.new
end
|
#code_last ⇒ Object
132
133
134
|
# File 'lib/code/object/range.rb', line 132
def code_last
raw.last || Nothing.new
end
|
#code_map(argument, **globals) ⇒ Object
136
137
138
139
140
141
142
143
144
145
146
147
|
# File 'lib/code/object/range.rb', line 136
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
194
195
196
|
# File 'lib/code/object/range.rb', line 194
def code_sample
code_to_list.code_sample
end
|
#code_select(argument, **globals) ⇒ Object
149
150
151
152
153
154
155
156
157
158
159
160
|
# File 'lib/code/object/range.rb', line 149
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
# File 'lib/code/object/range.rb', line 166
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_less(code_right).truthy?
code_list.code_append(code_element)
code_element = code_element.code_plus(code_argument)
end
else
while code_element.code_less_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
190
191
192
|
# File 'lib/code/object/range.rb', line 190
def code_to_list
List.new(raw.to_a)
end
|
#exclude_end? ⇒ Boolean
162
163
164
|
# File 'lib/code/object/range.rb', line 162
def exclude_end?
code_exclude_end.truthy?
end
|