rangeary 0.3.0 → 2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.ja.rdoc CHANGED
@@ -1,31 +1,38 @@
1
1
 
2
2
  = Rangeary - Multiple Range(Extd) class
3
3
 
4
- This package defines Rangeary class, which contains any multiple
5
- 1-dimensional ranges ({RangeExtd} objects). For example, a multiple
4
+ This package defines Rangeary class, which contains multiple
5
+ 1-dimensional ranges ({RangeExtd}[http://rubygems.org/gems/range_extd] objects). For example, a multiple
6
6
  range of
7
+
7
8
  0.5 < x <= 2.5, 6.0 <= x < 8.0, 10.0 <= x (<= Infinity)
9
+
8
10
  for <tt>x</tt> can be defined in this class.
9
11
 
10
- The element objects for {Rangeary} can be anthing that can form
11
- {RangeExtd} objects; not only Numeric (or Real) but anything
12
- Comparable. {Rangeary} accepts the built-in Range-class objects for
13
- the elements in initialisation, too.
12
+ The element objects for +Rangeary+ can be anything that can form
13
+ RangeExtd objects, not only Numeric (or Real) but anything
14
+ Comparable. +Rangeary+ accepts the built-in Range-class objects for
15
+ the elements in initialization, too, providing it is a valid
16
+ (<tt>Range#valid? == true</tt>) ones for the
17
+ {RangeExtd}[https://rubygems.org/gems/range_extd] class (n.b., most Range
18
+ objects are valid, but +(true..true)+ is not, for example, which would
19
+ not make sense to constitute multiple ranges).
14
20
 
15
21
  All the four standard logical operations, that is, negation
16
22
  (<tt>~</tt>), conjunction (<tt>&</tt> or <tt>*</tt>), disjunction
17
23
  (<tt>|</tt> or <tt>+</tt>) and exclusive disjunction (<tt>^</tt> or
18
24
  <tt>xor</tt>) are defined, as well as subtraction (<tt>-</tt>).
19
25
 
20
- {Rangeary} objects are immutable - once it is created, you can not
26
+ {Rangeary} objects are immutable - once it is created, you cannot
21
27
  alter the contents.
22
28
 
23
- Practically, {Rangeary} is a sub-class of Array, works as an array of
24
- {RangeExtd}, and inherits most of the methods, hence you can apply most
25
- operations to {Rangeary} that Array accpets, within the restriction of
26
- immutability of {Rangeary}. In addition, {Rangeary} offers several
27
- methods that directly work on its element as a range. In the above
28
- example, <tt>#cover?(1.0)</tt> would return true, whereas
29
+ {Rangeary} is implemented as a sub-class of Array, works as an array of
30
+ RangeExtd, and inherits most of the methods. Thus, you can apply most
31
+ operations to {Rangeary} that Array accepts, within the restriction of
32
+ immutability of {Rangeary}; for example, +Array#push+ is disabled for
33
+ {Rangeary}. In addition, {Rangeary} offers several
34
+ methods that directly work on its element as a range. In the
35
+ example above, <tt>#cover?(1.0)</tt> (like +Range#cover?+) returns true, whereas
29
36
  <tt>#cover?(9.0)</tt>, false.
30
37
 
31
38
 
@@ -33,35 +40,62 @@ With this class, logical operations of 1-dimensional range objects are
33
40
  now possible and easy.
34
41
  I hope you find it to be useful.
35
42
 
43
+ === News: Library locations and support for Beginless Range
44
+
45
+ **IMPORTANT**: The path for the library is moved up by one
46
+ directory in {Rangeary} Ver.2 from Ver.1 (same as the change in +RangeExtd+) in order that the
47
+ location follows the Ruby Gems convention. In short, the standard way
48
+ to require is +require "rangeary"+, the path of which used to be "rangeary/rangeary"
49
+
50
+ Version of {Rangeary} is now 2.0.
51
+
52
+ Ruby 2.7 and later supports {Beginless range}[https://rubyreferences.github.io/rubychanges/2.7.html#beginless-range].
53
+
54
+ +Rangeary+ (Ver.2) also supports it now, requiring
55
+ {RangeExtd}[https://rubygems.org/gems/range_extd] Ver.2 (or later).
56
+
57
+ ==== News: Endless Range supported
58
+
59
+ Now, as of 2019 October, this fully supports {Endless Range}[https://rubyreferences.github.io/rubychanges/2.6.html#endless-range-1]
60
+ introduced in Ruby 2.6. It is released as Version 1.* finally!
61
+
36
62
 
37
63
  == Install
38
64
 
39
- First, you need {RangeExtd} class library (basically, two files of pure ruby code), which is in gem:
65
+ First, you need {RangeExtd}[https://rubygems.org/gems/range_extd] class library Ver.2 or later, which is in gem:
66
+
40
67
  gem install range_extd
68
+
41
69
  Or, get it from
42
70
  {https://rubygems.org/gems/range_extd}
43
- if you have not yet installed it.
44
71
 
45
- Then, install this library.
72
+ If you choose manual installation for some reason,
73
+ follow the INSTALL section in the manual of
74
+ {RangeExtd}[https://rubygems.org/gems/range_extd]
75
+
76
+ Then, install this library with
77
+
46
78
  gem install rangeary
47
79
 
48
- A file
49
- rangeary/rangeary.rb
80
+ Files of
81
+
82
+ rangeary.rb
83
+ rangeary/util.rb
84
+ rangeary/util/hash_inf.rb
85
+
50
86
  should be installed in one of your <tt>$LOAD_PATH</tt>.
51
87
  Alternatively, get it from
52
88
  {http://rubygems.org/gems/rangeary}
53
89
 
54
90
  Then all you need to do is
55
- require 'rangeary/rangeary'
56
- or, possibly as follows, if you manually install it
91
+
57
92
  require 'rangeary'
58
- in your Ruby script (or irb). The library files like
59
- <tt>range_extd/range_extd.rb</tt> for {RangeExtd} is automatically
60
- loaded.
61
93
 
62
- {Rangeary} itself may work in Ruby 1.8, but {RangeExtd} works in only
63
- Ruby 2.0 (or maybe 1.9.3) and above, hence {Rangeary} does not work in
64
- Ruby 1.8 or earlier.
94
+ in your Ruby script (or irb)
95
+ (Note the path used to be, in Rangeary Ver.1 or earlier), "rangeary/rangeary"),
96
+ in which all the files are automatically loaded.
97
+
98
+ {Rangeary} Ver.2, along with RangeExtd, works in only Ruby 2.7 or later.
65
99
 
66
100
  Have fun!
67
101
 
@@ -79,11 +113,11 @@ Here are some simple examples.
79
113
  Rangeary(true..true) # => ArgumentError
80
114
 
81
115
  Basically, <tt>Rangeary()</tt> or <tt>Rangeary.new()</tt> accepts
82
- an arbitrary number of either {Range}, {RangeExtd}, {Rangeary}, or a
116
+ an arbitrary number of either Range, RangeExtd, {Rangeary}, or a
83
117
  combination of them. Note Range objects that return false in
84
- {Range#valid?} will raise an exception.
118
+ +Range#valid?+ raise an exception.
85
119
 
86
- For more detail and examples, see {Rangeary.new}.
120
+ For more detail and examples, see {Rangeary.initialize}.
87
121
 
88
122
 
89
123
  === Practical application examples
@@ -103,97 +137,388 @@ For more detail and examples, see {Rangeary.new}.
103
137
  rb + Rangeary(3..7) # => [2...9]
104
138
  rb - Rangeary(3..7) # => [2...3, RangeExtd(7,'<...',9)]
105
139
  rb * Rangeary(4..5, 8..10) # => [4..4, 8...9]
106
- rb.negation # => [-Float::INFINITY...2, RangeExtd(4,'<...',6), 9..Float::INFINITY]
140
+ ~rb # => [-Float::INFINITY...2, RangeExtd(4,'<...',6), 9..Float::INFINITY]
141
+
142
+ rc = Rangeary(?d...?x, negative: ?a, positive: ?z)
143
+ ~rc # => [?a...?d, ?x..?z]
144
+
145
+ where +~rb+ is equivalent to +rb.negation+. In the last example, it
146
+ provides the user-defined *infinities*.
107
147
 
108
148
  Most of the methods that are in the built-in Array can be used, as long as
109
149
  it does not violate the immutability of {Rangeary} objects, such as
110
- {Array#push}.
150
+ +Array#push+.
111
151
 
112
152
 
113
153
  == Description
114
154
 
115
- Once the file <tt>rangeary.rb</tt> is required, a class is defined, in
116
- addtion to the two defined in the RangeExtd library
117
- (<tt>RangeExtd</tt> and <tt>RangeExtd::Infinity</tt>).
118
-
119
- * Rangeary
120
-
155
+ Once the file <tt>rangeary.rb</tt> is required, the class +Rangeary+ is defined, in
156
+ addition to those defined in the {RangeExtd}[https://rubygems.org/gems/range_extd] library
157
+ (<tt>RangeExtd</tt>, <tt>RangeExtd::Infinity</tt>, and <tt>RangeExtd::Nowhere</tt>).
158
+ <tt>RangeExtd</tt> adds some methods to the built-in <tt>Range</tt> class.
121
159
 
122
160
  === Rangeary Class
123
161
 
124
162
  {Rangeary} objects are immutable, the same as Range and RangeExtd.
125
163
  Hence once an instance is created, it would not change.
126
164
 
127
- How to create an instance is explained above (in the Examples
165
+ The ways to create an instance are explained above (in the Example
128
166
  sections). Any attempt to try to create an instance with one of
129
- elements being not "valid" as a range, that is, {Range#valid?} returns
167
+ elements being not "valid" as a range, that is, of which +Range#valid?+ returns
130
168
  false, raises an exception (<tt>ArgumentError</tt>), and fails.
131
169
 
132
- Note users can supply negative and/or positive infinity objects in
133
- initialising {Rangeary} object, corresponding to the range objects
134
- they give. In default they are the type of either {Float::INFINITY} or
135
- {RangeExtd::Infinity::POSITIVE}. See {Rangeary.new} for detail.
136
-
137
- When multiple ranges are given in initialising, they are sorted in
138
- storing, and if there is any overlap among any of the elements, they
139
- are treated as disjunction (that is, simple summation). That means
170
+ +RangeExtd+ (or +Range+) objects given as arguments to initialize +Rangeary+
171
+ instances may contain negative and/or positive infinity objects.
172
+ Since Ruby 2.7 and 2.6,
173
+ {Beginless range}[https://rubyreferences.github.io/rubychanges/2.7.html#beginless-range]
174
+ and {Endless Range}[https://rubyreferences.github.io/rubychanges/2.6.html#endless-range-1]
175
+ are respectively introduced. They correspond to more conventional
176
+ {RangeExtd::Infinity}[https://www.rubydoc.info/gems/range_extd/RangeExtd/Infinity]
177
+ objects, which Rangeary has supported since its first release.
178
+
179
+ The built-in borderless Ranges and +RangeExtd::Infinity+ objects are
180
+ similar but are conceptually slightly different; the difference is
181
+ similar to that between borderless Ranges and +Float::INFINITY+.
182
+ See the reference document of {RangeExtd}[http://rubygems.org/gems/range_extd]
183
+ for detail.
184
+
185
+ In default, when Ranges that contain infinities, be it built-in borderless Ranges
186
+ or +Float::INFINITY+ or +RangeExtd::Infinity+ are given to {Rangeary},
187
+ +Rangeary+ use them. Alternatively, a user can specify their own
188
+ infinity objects; for example, you may provide +"a"+ as the negative
189
+ infinity for +Rangeary+ with String Ranges. If nothing is provided
190
+ and yet if +Rangeary+ requires one(s), which may happen, for example,
191
+ in an operation of negation, +Rangeary+ uses +Float::INFINITY+ for
192
+ Numeric (Real numbers) and +nil+ for anything else in default.
193
+ See {Rangeary} and {Rangeary.initialize} for detail.
194
+
195
+ When multiple ranges are given in initializing, they are internally sorted in
196
+ storing, and if there are any overlaps among any of the elements, they
197
+ are treated as disjunction (that is, simple summation). This means that
140
198
  the objects a {Rangeary} instance holds internally can be different from
141
- the objects given in initialisation, that is, their {#object_id} may be
199
+ the objects given in initialization, namely, their <tt>#object_id</tt> may be
142
200
  different. In particular, if built-in Range objects are given,
143
- they are always converted into {RangeExtd} objects internally.
201
+ they are always converted into RangeExtd objects internally.
144
202
 
145
- If any of the given range in the intialisation is empty, that is,
146
- {Range#empty?} returns true, they are ignored, unless all of the
203
+ If any of the given range in initialization is "empty", that is,
204
+ +Range#empty?+ returns true, they are ignored, unless all of the
147
205
  ranges given are empty ranges, in which case the "smallest" one will be
148
206
  preserved.
149
207
 
150
208
  If the result of the operation is empty, only the element of the
151
- resultant {Rangeary} is {RangeExtd::NONE}, and hence
209
+ resultant {Rangeary} is +RangeExtd::NONE+, and hence
152
210
  {Rangeary#empty_element?} will return true.
153
211
 
154
- For any Rangeary objects, {Rangeary#to_a}.size is always
155
- positive and not zero for that reason, or {Rangeary#empty?} returns
156
- always false, as {Rangeary#empty?} is a method inherited from Array
157
- (Use {Rangeary#empty_element?} instead to check if the instance is
158
- empty or not as a range).
212
+ For any Rangeary objects, <tt>(Rangeary#to_a).size</tt> returns always
213
+ positive and not zero for this reason, or +Rangeary#empty?+ returns
214
+ always false, as +Rangeary#empty?+ is a method inherited from Array.
215
+ Use {Rangeary#empty_element?} instead to check whether the instance is
216
+ *practically* empty, or in other words, an empty range.
217
+
159
218
  Rangeary(RangeExtd::NONE).empty? # => false
160
219
  Rangeary(RangeExtd::NONE).empty_element? # => true
161
220
 
162
- As mentioned above, all the methods of Array but a few are inherited
163
- to this {Rangeary} class, and they work based on each element
164
- {RangeExtd}. Four methods work differently: {Rangeary#+} and
221
+ As mentioned, all the methods of Array but a few are inherited
222
+ to this {Rangeary} class, and they in principle work as if +Rangeary+ is an Array of
223
+ +RangeExtd+ (which is indeed the case!). Four methods work differently: {Rangeary#+} and
165
224
  {Rangeary#*} are the alias to {Rangeary#disjunction} and
166
- {Rangeary#conjunction}, repectively. {Rangeary#===} performs
167
- {Range#===} for all the Rangeary element ranges and return true if any
225
+ {Rangeary#conjunction}, respectively. {Rangeary#===} performs
226
+ +Range#===+ for all the Rangeary element ranges and return true if any
168
227
  of them returns true. Therefore, {Rangeary#===}(RangeExtd(**))
169
- returns always false. {Rangeary#flatten} returns the concatnated
170
- array of {Rangeary#to_a} for all the range elements. Also, [#length]
171
- and [#reverse] are undefined.
228
+ returns always false. Also, +#length+
229
+ and +#reverse+ are undefined. Finally, {Array#==} is modified (see below).
172
230
 
173
- {Rangeary#==} and {Rangeary#eql?} work the same as Array, hence
231
+ {Rangeary#==} and +Rangeary#eql?+ work in the same way as of Array. Therefore,
174
232
  [2..4, 6..8] == Rangeary(2..4, 6..8) # => true
175
- But please note
233
+
234
+ However, you should note the following:
235
+
176
236
  [2..4, 6..8] == Rangeary(6..8, 2..4) # => true
177
237
  [6..8, 2..4] == Rangeary(6..8, 2..4) # => false
178
- In short the direct comparison with a standard Array is not recommended.
179
- Instead compare with other {Rangeary} objects.
180
238
 
181
- All the other methods operating on the element of ranges, rather than
239
+ because Rangeary always sort its contents at the initialization.
240
+ In short, it is really not recommended to compare Array and Rangeary
241
+ directly. Instead, you should compare two {Rangeary} objects.
242
+
243
+ All the other methods operating on the element ranges, rather than
182
244
  on the ranges themselves, have a suffix of <tt>_element</tt>, if there
183
245
  is the same method name in the built-in Array. For example,
184
- {Rangeary#size} returns the number of {RangeExtd} objects it holds,
185
- and {Rangeary#size_element} returns the total {Range#size} of all
186
- the {RangeExtd} objects it holds.
246
+ +Rangeary#size+ returns the number of +RangeExtd+ objects it holds as an Array,
247
+ and {Rangeary#size_element} returns the total +Range#size+ of all
248
+ the RangeExtd objects it holds.
249
+
187
250
  Rangeary(1..3, 5..8).size # => 2
188
251
  Rangeary(1..3, 5..8).size_element # => 7
189
252
 
253
+ Or, {Rangeary#flatten_element} returns the concatenated
254
+ single array of +Rangeary#to_a+ for all the discrete range elements
255
+ (the element ranges have to be discrete like Integer).
256
+
257
+ To flatten an Array containing both Arrays and Rangeary while you do
258
+ not want to flatten each Rangeary, use {Rangeary.flatten_no_rangeary}.
259
+
260
+ The complete reference of the class and methods is available at
261
+ {Rubygems website}[http://rubygems.org/gems/rangeary], or you can compile the
262
+ reference with +yard+ from the source package (+make doc+ at the
263
+ package root directory would do).
264
+
265
+ === Infinities
266
+
267
+ The infinities are vital in the logical operation of Rangeary.
268
+ Without it, negation could not be defined, and other logical
269
+ operations are also closely related to it; for example, *subtraction*
270
+ is basically a combination of *negation* and *conjunction*.
271
+
272
+ To determine what the positive and negative infinities for the given
273
+ elements is not a trivial task. In default, +nil+ is used except for
274
+ +Numerics+ (Integer, Rational, Float etc), for which +Float::INFINITY+
275
+ is used. Note that the default used to be
276
+ <tt>RangeExtd::Infinity::POSITIVE</tt> and <tt>RangeExtd::Infinity::NEGATIVE</tt>
277
+ defined in {RangeExtd}[http://rubygems.org/gems/range_extd]
278
+ up to Rangeary Ver.1, where both beginless and endless Ranges were not
279
+ been introduced or supported. Rangeary Ver.2 changes the specification
280
+ to be in line with the latest Ruby Range.
281
+
282
+ Alternatively, a user can specify their own infinities in
283
+ initialization of {Rangeary} with options of +positive:+ and
284
+ +negative:+. The boundaries at the opposite polarities usually should
285
+ match unless they are comparable or either of them is +nil+.
286
+
287
+ Here are examples of how infinities work with {Rangeary}. In the first
288
+ example, infinities are implicitly contained in the specified Range.
289
+ Then, the infinities are internally preserved throughout operations.
290
+ Note that the first set of 2 operations and the second set of a single operation
291
+ means the same.
292
+
293
+ r1 = Rangeary(nil..Float::INFINITY).conjunction( RangeExtd::NONE )
294
+ # => Rangeary(RangeExtd::NONE)
295
+ r2 = r1.negation
296
+ # => Rangeary(nil..Float::INFINITY)
297
+
298
+ ~(Rangeary(nil..Float::INFINITY) * RangeExtd::NONE)
299
+ # => Rangeary(nil..Float::INFINITY)
300
+
301
+ In the second example below, a negative infinity of "+d+" is explicitly specified
302
+ for a Range of single alphabet String.
303
+
304
+ Rangeary("f".."k", negative: "d").negation
305
+ # => Rangeary("d"..."f", "k"<..nil)
306
+
307
+ where +"k"<..nil+ means a begin-exclude Range or +RangeExtd("k"..nil, true)+.
308
+
309
+ A note of caution is that once an infinity is defined for a Rangeary object, any other
310
+ Rangeary objects with which operations are performed should be in line with the
311
+ same infinities. If you specify your own infinities, it is advised to
312
+ do so at the beginning. And once the infinities have been manually
313
+ set, it is advised not to modify them (although this library should
314
+ handle such changes appropriately -- see the method document for detail)
315
+ because unexpected errors may occur. Here is a set of examples.
316
+
317
+ r3 = Rangeary("f".."k", negative: "d")
318
+ r4 = ~r3
319
+ # => Rangeary("d"..."f", "k"<..nil)
320
+ _ = Rangeary(r4, positive: "t") # raises ArgumentError(!):
321
+ # because "t" is smaller than end of Endless Range
322
+ r6 = Rangeary(r3, positive: "t") # OK: because end of r3 is only "k"
323
+ r7 = ~r6
324
+ # => Rangeary("a"..."d", "f".."k") # differs from r4 in the second Range
325
+
326
+ In the example above, +r4+, which is the negation of +r3+ is
327
+ "Endless", i.e., the last Range in +r3+ is an endless Range.
328
+ So, attempting to set a positive infinity of "+t+" raises an Exception (+ArgumentError+).
329
+
330
+ If the new infinity(ies) does not contradict the current contents (like +r6+), it
331
+ is set accordingly and the subsequent operations (e.g., +r7+) adopt the value
332
+ (though you should make sure it is exactly what you want).
333
+
334
+
335
+ ==== Algorithm of determining default infinities
336
+
337
+ Callers can supply user-defined infinity objects for both or either
338
+ positive and negative infinity and in that case they are accepted
339
+ as the infinities with the highest priority, though ArgumentError might be
340
+ issued if they contradict the elements; for example, if a {Rangeary}
341
+ instance consists of an array of Integer Ranges (RangeExtd) like +(3..8)+,
342
+ and yet if String "abc" is specified as an infinity, it *contradicts*
343
+ the elements in the sense they are not comparable.
344
+
345
+ Internally, the {Rangeary} instance has a Hash extended with {Rangeary::Util::HashInf},
346
+ which can be obtained with {Rangeary#infinities}.
347
+ It has only 2 keys of +:negative+ and +:positive+, the values of which
348
+ are the current best-guessed or definite infinities. The Hash also
349
+ holds status information for each polarity with 3 levels of
350
+
351
+ 1. <tt>false</tt>
352
+ 2. <tt>:guessed</tt>
353
+ 3. <tt>:definite</tt>
354
+
355
+ It is +false+ only when the Rangeary is absolutely void with no
356
+ information about the contents: +Rangeary(RangeExtd::NONE)+.
357
+
358
+ If the user explicitly specifies a boundary in the optional arguments in
359
+ initialization of {Rangeary}, it is accepted in principle with an associated status of <tt>:definite</tt>.
360
+
361
+ If the user-specified main arguments in initialization contain
362
+ a (potentially multiple) {Rangeary}, their defined infinities are
363
+ inherited with their associated statuses.
364
+
365
+ Also, user-supplied Range-s or RangeExtd-s to the arguments in
366
+ initialization of {Rangeary} always have, except for
367
+ +RangeExtd::NONE+, concrete boundary values, which can be +nil+.
368
+
369
+ If one of the boundaries of a Range (n.b., it is *not* Rangeary) contains either +nil+ or one of infinite values
370
+ (which is checked with +RangeExtd::Infinity.infinite?+, where in practice
371
+ a duck-typing check is performed, using the method +infinite?+), then
372
+ it is accepted as an infinite value with an associated status of <tt>:definite</tt>.
373
+
374
+ Otherwise,
375
+
376
+ 1. if a boundary value is a (real-type) Numeric, +Float::INFINITY+ (or
377
+ its negative,
378
+ 2. or otherwise, +nil+
379
+
380
+ is set as an infinity of the boundary with an associated status of +:guessed+.
381
+
382
+ Note that the priority used to be different up to Rangeary Ver.1; +nil+ was not used and
383
+ instead the +RangeExtd::Infinity+ objects were used. It was because
384
+ the beginless Range (and endless Range before Ruby-2.6) has not been defined before
385
+ Ruby 2.7. Now they are defined, it is only natural to use +nil+ as the
386
+ default infinities in both ends, hence the change in specification in
387
+ {Rangeary} Ver.2.
388
+
389
+ Usually the arguments given in initialization of a {Rangeary} contain
390
+ more than one set of infinities candidate, unless only a single
391
+ argument of either Range (or its subclass instance) with no optional
392
+ arguments is given. The priority is judged in the following order:
393
+
394
+ 1. the optional arguments
395
+ 2. an associated status of <tt>:definite</tt>, <tt>:guessed</tt>, and
396
+ <tt>false</tt> in this order
397
+
398
+ If the associated statuses are equal for two or more inputs, the most
399
+ extreme one among them for each polarity is chosen. For example, suppose
400
+ two instances of {Rangeary} are given in initialization of another
401
+ {Rangeary} and their negative infinities are "+b+" and "+c+". Then,
402
+ because of
403
+
404
+ "b" < "c"
405
+
406
+ the former ("+b+") is adopted as the new negative infinity. Note that
407
+ the parameters given in the optional arguments have always higher
408
+ priority regardless.
409
+
410
+
411
+ The following examples demonstrate the specification.
412
+
413
+ Rangeary(7..).negation
414
+ # => Rangeary(-Float::INFINITY...7)
415
+ Rangeary(7..).negation.negation
416
+ # => Rangeary(7..)
417
+
418
+ Remember the default infinity for Float is +Float::INFINITY+. In this
419
+ case, however, the positive infinity was in practice specified by the
420
+ user to be +nil+ in the form of argument of +(7..)+ If you want to
421
+ specify the negative infinity instead, you must do it explicitly:
422
+
423
+ Rangeary(7.., negative: nil).negation
424
+ # => Rangeary(...7)
425
+
426
+ Alternatively, you can always use conjunction like (the following two mean the same):
427
+
428
+ Rangeary(..nil).conjunction(Rangeary(7..)).negation
429
+ # => Rangeary(...7)
430
+ ~(Rangeary(..nil) * Rangeary(7..))
431
+ # => Rangeary(...7)
432
+
433
+ The registered infinities for each instance is obtained (Hash extended with
434
+ HashInf), which has
435
+ two keys of +:positive+ and +negative+, with the method {#infinities};
436
+ for example,
437
+
438
+ ran.infinities
439
+ # => <Hash(Inf): {:negative=>"a", :positive=>nil},
440
+ # status: {:negative=>:definite, :positive=>:guessed}>
441
+
442
+ Note that the values of the returned Hash (+HashInf) may be +false+;
443
+ if it is not convenient, call it as +#instances(convert: true)+
444
+ with which +false+ in the returned value, if there is any, is converted
445
+ to +nil+ and the standard Hash as opposed to
446
+ Hash extended with {Rangeary::Util::HashInf} is returned:
447
+
448
+ ran.infinities(convert: true) # => { :negative => "a"
449
+ # :positive => nil, }
450
+
451
+ Consult the manuals of the methods for detail.
452
+
453
+
454
+ === Array#==
455
+
456
+ Equal method of +Array#==+ (and thus its child-class +Rangeary#==+) is
457
+ modified slightly so that the behaviour when both are *practically*
458
+ empty.
459
+
460
+ Rangeary objects are never empty in the sense of Array; it contains at
461
+ least +RangeExtd::NONE+, which is *empty*. Therefore, if either or
462
+ both the Array/Rangeary return true with {Rangeary#empty_element?},
463
+ then it is regarded as equivalent to +Array#empty?+.
464
+
465
+ Up to Ver.1 (or more precisely, in Ver.1), the equality behaviour was
466
+ far more complicated. The complexity originated in the incompleteness
467
+ of the Ruby built-in borderless Range;
468
+ while Ruby-2.6 introduced the endless Range, which Rangeary Ver.1 supported,
469
+ it lacked the beginless Range till the release of Ruby-2.7.
470
+
471
+ Since Rangeary Ver.1 implemented only the endless Range, logical
472
+ operations, especially those that involve negation either explicitly
473
+ or implicitly (like subtraction), were self-incomplete. To mitigate
474
+ the problem, the equality method was designed to handle the
475
+ incompleteness instead in Rangeary Ver.1.0.
476
+
477
+ Now that both beginless and endless Ranges are supported by Ruby and
478
+ Rangeary Ver.2, such an ad hoc fix is no longer necessary or
479
+ desirable. The difference from the Ruby default is now minimum.
480
+
481
+ Note that this library loads the utility library associated with
482
+ +RangeExtd+, which modifies some behaviours of the equality method
483
+ (operator) of all Object, if in a backward-compatible way, i.e., users
484
+ do not have to worry about it for the use outside RangeExtd. For
485
+ logical operations implemented in this library, commutative behaviours
486
+ of the operators are essential and they would be only achieved by
487
+ modifying +Object#==+.
488
+
489
+ Also note that +Rangeary#equiv+ method may behave differently from the equal operator.
490
+ For example,
491
+
492
+ Rangeary(RangeExtd(1,"<...",4), 5...8).equiv?(Rangeary(2..3, 5..7))
493
+
494
+ returns true. To describe this, the left and right Rangearies are arrays of
495
+ ranges of Integers which consist of
496
+
497
+ * (Left)
498
+ 1. Range that starts at, but excluding, 1, ending at, but excluding 4
499
+ 2. Range that starts at 5 (inclusive), ending at 8 (exclusive).
500
+ * (Right)
501
+ 1. Range that starts and ending at 2 and 3, respectively, both inclusive.
502
+ 2. Range that starts and ending at 5 and 7, respectively, both inclusive.
503
+
504
+ According to +Integer#succ+ (+Range#each+) method, the left and right ones are equivalent.
505
+ Therefore +Rangeary#equiv+ returns true, whereas the equal operator
506
+ returns false for this.
507
+
190
508
 
191
509
  == Known bugs
192
510
 
193
- * None is known.
511
+ * Rangeary Ver.2, which supports both beginless and endless Ranges (of Ruby 2.7 and later), requires {RangeExtd}[https://rubygems.org/gems/range_extd] Ver.2 or later.
512
+ * To suppress warnings in Ruby-2.7 (in Rangeary Ver.1), {RangeExtd}[https://rubygems.org/gems/range_extd] must be Ver.1.1.1 or later.
513
+ * +Rangeary#last_element+ includes a monkey patch (which used to raise
514
+ an error before Rangeary Ver.2) to handle a bug in +Range#last+ in Ruby-2.7
515
+ and above (at least up to 3.1.2). See
516
+ {Bug #18994}[https://bugs.ruby-lang.org/issues/18994] for detail of the bug,
517
+ which was in no time resolved with ({patch #6324}[https://github.com/ruby/ruby/pull/6324],
518
+ applied at commit {bbe5ec7}[https://github.com/ruby/ruby/commit/bbe5ec78463f8d6ef2e1a3571f17357a3d9ec8e4]).
519
+ It happens only in very limited conditions.
194
520
 
195
- This library requires Ruby 2.0 or above (it may work all right with
196
- Ruby 1.9.3, however I have never tested it).
521
+ This library (for Ver.2 and later) requires Ruby 2.7 or later.
197
522
 
198
523
  Extensive tests have been performed, as included in the package.
199
524
 
@@ -203,46 +528,52 @@ Extensive tests have been performed, as included in the package.
203
528
  Nothing planned.
204
529
 
205
530
 
206
- == Final notes
531
+ == Final comment
207
532
 
208
- This work is inspired by Ian Stewart, who developped a (numeric)
209
- multiple-range library in Fortran90 for the SAS software package for
533
+ This work is inspired by Ian Stewart, who developed a (numeric)
534
+ multiple-range library in FORTRAN90 for the SAS software package for
210
535
  XMM-Newton telescope. I appreciate his work.
211
536
 
212
537
  The implementation to Ruby was not straightforward, though, partly
213
538
  because the built-in Range does not allow to exclude the begin
214
539
  boundary, and partly because the infinity is not defined for general
215
- objects but Numeric (Real) in Ruby. {RangeExtd} class I have developed
540
+ objects but Numeric (Real) in Ruby (which later changed partially with
541
+ the introduction of Endless Range in Ruby 2.6 released in 2018
542
+ December and further and completely with the introduction of Beginless Range in Ruby
543
+ 2.7 released in 2019 December). +RangeExtd+ class I have developed
216
544
  makes this library possible. I am glad the completeness of ranges in
217
545
  the 1-dimensional space for arbitrary Comparable object is achieved now.
218
546
 
219
547
  Enjoy.
220
548
 
221
549
 
222
-
223
550
  == Miscellaneous
224
551
 
225
552
  == Copyright etc
226
553
 
227
- Author:: Masa Sakano < imagine a_t sakano dot co dot uk >
554
+ Author:: Masa Sakano < info a_t wisebabel dot com >
228
555
  License:: MIT.
229
556
  Warranty:: No warranty whatsoever.
230
557
  Versions:: The versions of this package follow Semantic Versioning (2.0.0) http://semver.org/
231
558
 
232
-
559
+ -------------------------------------------
233
560
 
234
561
  = Rangeary - 複数Range(Extd)クラス
235
562
 
236
- このパッケージは、1次元の任意の数のレンジ({RangeExtd} オブジェクト)を
563
+ このパッケージは、1次元の任意の数のレンジ({RangeExtd}[http://rubygems.org/gems/range_extd] オブジェクト)を
237
564
  保持する Rangeary クラスを定義しています。たとえば、<tt>x</tt> に
238
565
  対する複数レンジの
239
566
  0.5 < x <= 2.5, 6.0 <= x < 8.0, 10.0 <= x (<= 無限大)
240
567
  がこのクラスで定義できます。
241
568
 
242
- {RangeExtd} の要素たり得るオブジェクトは全て {Rangeary} の要素となり
243
- 得ます。Numeric(の実数)に限らず、Comparable であるオブジェクトは全て
244
- 可能です。また、{Rangeary} は、組込Rangeクラスのオブジェクトも初期化に
245
- 使えます。
569
+ RangeExtd の要素たり得るオブジェクトは全て +Rangeary+ の要素となり得ます。
570
+ Numeric(の実数)に限らず、Comparable であるオブジェクトは全て
571
+ 可能です。また、+Rangeary+ は、組込Rangeクラスのオブジェクトも、それが
572
+ {RangeExtd}[https://rubygems.org/gems/range_extd] クラスの
573
+ (<tt>Range#valid? == true</tt>) を満たす限り初期化に使えます
574
+ (注: ほとんどの Rangeオブジェクトはこの条件を満たすものの、
575
+ +(true..true)+ のように満たさないものもあって、実際、それらは複数
576
+ Rangeの構成要素としては意味をなさないものです)。
246
577
 
247
578
  四つの標準論理演算全て、すなわち否定(negation; <tt>~</tt>)、論理積
248
579
  (conjunction; <tt>&</tt> または <tt>*</tt>)、論理和(disjunction;
@@ -253,45 +584,70 @@ Versions:: The versions of this package follow Semantic Versioning (2.0.0) http:
253
584
  {Rangeary} オブジェクトはイミュータブルです。すなわち、
254
585
  一度インスタンスが生成されると、要素を書換えることはできません。
255
586
 
256
- 実用的には、{Rangeary} は、Arrayのサブクラスとなっていて、{RangeExtd}
587
+ 実用的には、{Rangeary} は、Arrayのサブクラスとなっていて、RangeExtd
257
588
  の配列として振舞います。また、Arrayのほとんどのメソッドを継承していま
258
589
  す。したがって、イミュータブルな{Rangeary}に許される限りにおいて、
259
590
  Arrayに使えるほとんどの操作を{Rangeary}に適用できます。加えて、
260
591
  {Rangeary}には、そのレンジの要素に直接働きかけるメソッドも幾つかあり
261
- ます。上に挙げた例ならば、<tt>#cover?(1.0)</tt> は真を返し、
262
- <tt>#cover?(9.0)</tt> は偽を返します。
592
+ ます。上に挙げた例ならば、<tt>Range#cover?(1.0)</tt> は真を返し、
593
+ <tt>Range#cover?(9.0)</tt> は偽を返します。
263
594
 
264
595
  このクラスにより、1次元レンジへの論理演算が可能かつ容易になりました。
265
596
  これが有用なものであることを願ってここにリリースします。
266
597
 
598
+ === News: Libraryの場所、Beginless Rangeのサポート他
599
+
600
+ **重要**: ライブラリのパスが{Rangeary} Ver.1 から Ver.2 で、
601
+ ディレクトリの階層一つ上がりました。これは、Ruby Gemの慣用にそうように
602
+ するためです。端的には、標準的方法は、+require "rangeary"+ です。
603
+ 以前のパスは、"rangeary/rangeary" でした。
604
+
605
+ それに伴い、{Rangeary} のバージョンを2.0にあげました。
606
+
607
+ Ruby 2.7以上では、 {Beginless range}[https://rubyreferences.github.io/rubychanges/2.7.html#beginless-range]
608
+ がサポートされます。
609
+
610
+ +Rangeary+ (Ver.2) もそれをサポートします。ただし、
611
+ {RangeExtd}[https://rubygems.org/gems/range_extd] Ver.2 (以上)
612
+ が必要です。
613
+
614
+ ==== News: Endless Range サポートしました
615
+
616
+ 2019年10月(Rangeary Ver.1)より、Ruby-2.6 で導入された
617
+ {Endless Range}[https://rubyreferences.github.io/rubychanges/2.6.html#endless-range-1]
618
+ をついにサポートしました!
619
+
267
620
 
268
621
  == インストール
269
622
 
270
- まず、{RangeExtd} クラスのライブラリ(純粋に rubyで書かれたファイル 2
271
- です)が必要です。gem を使ってインストールできます。
623
+ まず、 {RangeExtd}[https://rubygems.org/gems/range_extd] クラス(Ver.2
624
+ 以上)のライブラリが必要です。gem を使ってインストールできます。
272
625
  gem install range_extd
273
626
  もしくは以下から入手して下さい。
274
- https://rubygems.org/gems/range_extd
627
+ {https://rubygems.org/gems/range_extd}
628
+
629
+ (同ライブラリを手動でインストールする場合は、同ライブラリのマニュアルを参照。)
275
630
 
276
631
  次いで、このライブラリをインストールします。
277
632
  gem install rangeary
278
633
 
279
- ファイルが一つ、
634
+ ファイル
635
+ rangeary.rb
636
+ rangeary/util.rb
637
+ rangeary/util/hash_inf.rb
280
638
  rangeary/rangeary.rb
281
- <tt>$LOAD_PATH</tt>上のどこかにインストールされるはずです。
639
+ が<tt>$LOAD_PATH</tt>上のどこかにインストールされるはずです。
282
640
  あるいは以下から入手可能です。
283
- http://rubygems.org/gems/rangeary
641
+ {http://rubygems.org/gems/rangeary}
284
642
 
285
643
  後は、Ruby のコード(又は irb)から
286
- require 'rangeary/rangeary'
287
- とするだけです。もしくは、特に手でインストールした場合は、
288
644
  require 'rangeary'
289
- とする必要があるかも知れません。他のファイル({RangeExtd} 用の
290
- <tt>range_extd/range_extd.rb</tt>)は、自動的に読込まれます。
645
+ とするだけです。
646
+ (このパスは、Rangeary Ver.1 では "rangeary/rangeary" でした!)
291
647
 
292
- {Rangeary} 自体は Ruby 1.8でも動作するかも知れません。しかし、
293
- {RangeExtd} が Ruby 2.0 (ひょっとしたら 1.9.3)以上でしか動かないため、
294
- 実質的には {Rangeary} は Ruby 1.8 以前のバージョンでは動作しません。
648
+ 他のファイル(特に<tt>range_extd.rb</tt>)は、自動的に読込まれます。
649
+
650
+ {Rangeary} Ver.2 は Ruby 2.7 以上で動きます。
295
651
 
296
652
  お楽しみあれ!
297
653
 
@@ -309,11 +665,11 @@ http://rubygems.org/gems/rangeary
309
665
  Rangeary(true..true) # => ArgumentError
310
666
 
311
667
  基本的に、<tt>Rangeary()</tt> または <tt>Rangeary.new()</tt> は、任意
312
- の数の {Range}{RangeExtd}、{Rangeary} あるいはその組合わせを引数とし
313
- て取ります。ただし、{Range}クラスのオブジェクトで {Range#valid?} が偽
668
+ の数の Range、RangeExtd、{Rangeary} あるいはその組合わせを引数とし
669
+ て取ります。ただし、Rangeクラスのオブジェクトで +Range#valid?+ が偽
314
670
  を返すものは、例外が発生します。
315
671
 
316
- さらなる解説及び例は、{Rangeary.new}を参照して下さい。
672
+ さらなる解説及び例は、{Rangeary.initialize}を参照して下さい。
317
673
 
318
674
 
319
675
  === 実践例
@@ -333,70 +689,95 @@ http://rubygems.org/gems/rangeary
333
689
  rb + Rangeary(3..7) # => [2...9]
334
690
  rb - Rangeary(3..7) # => [2...3, RangeExtd(7,'<...',9)]
335
691
  rb * Rangeary(4..5, 8..10) # => [4..4, 8...9]
336
- rb.negation # => [-Float::INFINITY...2, RangeExtd(4,'<...',6), 9..Float::INFINITY]
692
+ ~rb # => [-Float::INFINITY...2, RangeExtd(4,'<...',6), 9..Float::INFINITY]
337
693
 
338
- 組込Arrayクラスに含まれるほとんどのメソッドが、たとえば{Array#push}のように
339
- {Rangeary} がイミュータブルであることに抵触しない限りにおいて、使用可能です。
694
+ rc = Rangeary(?d...?x, negative: ?a, positive: ?z)
695
+ ~rc # => [?a...?d, ?x..?z]
340
696
 
697
+ ここで、+~rb+ は +rb.negation+ と等価で論理否定を意味します。最後の例
698
+ では、ユーザー指定の*無限大/無限小*値を定義しています。
341
699
 
342
- == 詳説
343
700
 
344
- ファイル <tt>rangeary.rb</tt> が読まれた段階で、RangeExtd ライブラリで
345
- 定義される二つのクラス(<tt>RangeExtd</tt>
346
- <tt>RangeExtd::Infinity</tt>)に加えて、クラス一つが定義されます。
701
+ 組込Arrayクラスに含まれるほとんどのメソッドが、たとえば+Array#push+のように
702
+ {Rangeary} がイミュータブルであることに抵触しない限りにおいて、使用可能です。
703
+
347
704
 
348
- * Rangeary
705
+ == 詳説
349
706
 
707
+ ファイル <tt>rangeary.rb</tt> が読まれた段階で、
708
+ {RangeExtd}[https://rubygems.org/gems/range_extd] ライブラリで定義されるクラス
709
+ (<tt>RangeExtd</tt>, <tt>RangeExtd::Infinity</tt>, <tt>RangeExtd::Nowhere</tt>)
710
+ が読み込まれます。
711
+ <tt>RangeExtd</tt> は、組込み built-in <tt>Range</tt> クラスにいくつかのメソッドを追加します。
350
712
 
351
713
  === Rangeary クラス
352
714
 
353
715
  {Rangeary} オブジェクトは、Range や RangeExtd と同様、イミュータブルで
354
716
  す。だから、一度インスタンスが生成されると、変化しません。
355
717
 
356
- インスタンスの生成方法は上述の通りです(「使用例」の章)。レンジとして"valid"と見
357
- なされないインスタンスを生成しようとすると、すなわち {Range#valid?}
718
+ インスタンスの生成方法は上述の通りです(「使用例」の章)。レンジとして"valid"
719
+ と見なされないインスタンスを生成しようとすると、すなわち +Range#valid?+
358
720
  偽を返すレンジを使おうとすると、例外(<tt>ArgumentError</tt>)が発生し、
359
721
  失敗します。
360
722
 
361
- なお、ユーザーは、{Rangeary} インスタンス生成の時、レンジ要素に対応し
362
- た正負の無限大オブジェクトを付加することができます。デフォルトでは、
363
- {Float::INFINITY} または {RangeExtd::Infinity::POSITIVE} の類に
364
- なります。詳しくは {Rangeary.new} を参照下さい。
723
+ +Rangeary+初期化引数の +RangeExtd+ (または +Range+) オブジェクトは
724
+ (どちらかあるいは両方の境界に)無限大オブジェクトを含むかも知れません。
725
+ Ruby 2.7 と2.6にて、
726
+ {Beginless range}[https://rubyreferences.github.io/rubychanges/2.7.html#beginless-range],
727
+ {Endless Range}[https://rubyreferences.github.io/rubychanges/2.6.html#endless-range-1]
728
+ がそれぞれ導入されました。これらは、本ライブラリに当初から定義されていた
729
+ {RangeExtd::Infinity}[https://www.rubydoc.info/gems/range_extd/RangeExtd/Infinity]
730
+ に対応すると言えます。
731
+
732
+ 組込みのこれら境界のない Ranges と +RangeExtd::Infinity+ オブジェクトは
733
+ 類似しているものの概念としては少し異なります。この違いは、
734
+ 境界のない Ranges と+Float::INFINITY+ との違いに似ています。
735
+ {RangeExtd}[http://rubygems.org/gems/range_extd]
736
+ のマニュアルに詳細に解説されています。
737
+
738
+ デフォルトでは、引数に与えられたRangeがこれら無限大オブジェクト
739
+ (+nil+であれ+Float::INFINITY+であれ+RangeExtd::Infinity+であれ)
740
+ を含む場合、+Rangeary+ はそれをそのまま使います。あるいは、
741
+ ユーザーは、{Rangeary} インスタンス生成の時、
742
+ レンジ要素に対応した正負の無限大オブジェクトを指定することもできます。
743
+ たとえば、1文字のStringのRangeに対して、+"a"+ を負の無限大オブジェクト
744
+ として指定することができます。もし何も指定がない場合は、Rangearyは、
745
+ デフォルトで、 Numeric (実数) に対しては+Float::INFINITY+、
746
+ それ以外には +nil+ を無限大とします。
365
747
 
366
748
  生成時に複数のレンジが引数として与えられた時、ソートされて保持されます。
367
749
  その時、要素のどこかに重複する部分があった時は、論理和として扱われます
368
750
  (つまり、単純に足し合わされます)。これはつまり、{Rangeary} が内部的に
369
751
  保持するオブジェクトは生成時に与えられたものとは異なる、すなわち
370
- {#object_id} が異なるかも知れないことを意味します。特に、組込Rangeが引
371
- 数として与えられた時は、常に {RangeExtd} オブジェクトに内部で変換されます。
752
+ <tt>#object_id</tt> が異なるかも知れないことを意味します。特に、組込Rangeが引
753
+ 数として与えられた時は、常に RangeExtd オブジェクトに内部で変換されます。
372
754
 
373
- もし生成時に与えられたレンジのどれかが空、すなわち {Range#empty?} が真
755
+ もし生成時に与えられたレンジのどれかが空、すなわち +Range#empty?+ が真
374
756
  を返す場合、それらは無視されます。ただし、引数の全てのレンジが空であっ
375
757
  た場合は、「最小」のものが残されます。
376
758
 
377
759
  もし演算の結果として空の{Rangeary}が返される場合、その唯一の要素は
378
- {RangeExtd::NONE}となり、したがって
760
+ +RangeExtd::NONE+となり、したがって
379
761
  {Rangeary#empty_element?} が真を返します。
380
762
 
381
- そのため、どの Rangeary オブジェクトも、{Rangeary#to_a}.size は常に正
763
+ そのため、どの Rangeary オブジェクトも、+Rangeary#to_a.size+ は常に正
382
764
  の値を返し、零を返すことはありません。あるいは、Array から継承した
383
- {Rangeary#empty?} は常に偽を返します(オブジェクトがレンジとして空かど
765
+ +Rangeary#empty?+ は常に偽を返します(オブジェクトがレンジとして空かど
384
766
  うかをチェックするには、{Rangeary#empty_element?} を使って下さい)。
385
767
  Rangeary(RangeExtd::NONE).empty? # => false
386
768
  Rangeary(RangeExtd::NONE).empty_element? # => true
387
769
 
388
770
  前述のように、ごく一部を除いた全ての Arrayクラスのメソッドがこの
389
- {Rangeary} クラスに継承されていて、それらは各{RangeExtd}要素に対して
771
+ {Rangeary} クラスに継承されていて、それらは各RangeExtd要素に対して
390
772
  動作します。ただし、4個のメソッドの挙動が異なります。
391
773
  {Rangeary#+} と {Rangeary#*} とは、それぞれ {Rangeary#disjunction} と
392
774
  {Rangeary#conjunction} とへのエイリアスとなっています。{Rangeary#===}
393
- は、全ての{RangeExtd}要素に対して{Range#===}を実行し、それらの一つでも
775
+ は、全てのRangeExtd要素に対して+Range#===+を実行し、それらの一つでも
394
776
  真を返せば真を返します。したがって、{Rangeary#===}(RangeExtd(**))は常
395
- に偽を返します。{Rangeary#flatten} は、全ての{RangeExtd}要素に対して
396
- {Rangeary#to_a} を実行して、その結果を結合した配列を返します。
777
+ に偽を返します。
397
778
  また、[#length] と [#reverse] とは未定義化されています。
398
779
 
399
- {Rangeary#==} と {Rangeary#eql?} は、Arrayと同様に動作します。だから
780
+ {Rangeary#==} と +Rangeary#eql?+ は、Arrayと同様に動作します。だから
400
781
  [2..4, 6..8] == Rangeary(2..4, 6..8) # => true
401
782
  も成り立ちます。しかし注意すべきは以下です。
402
783
  [2..4, 6..8] == Rangeary(6..8, 2..4) # => true
@@ -406,20 +787,89 @@ http://rubygems.org/gems/rangeary
406
787
 
407
788
  レンジ要素に対してではなく、レンジを構成する要素に対して動作する他の
408
789
  全てのメソッドは、組込Arrayクラスに同名のメソッドが存在する場合、接尾辞
409
- <tt>_element</tt> がつきます。たとえば、{Rangeary#size} は、保持する
410
- {RangeExtd} オブジェクトの数を返し、一方、{Rangeary#size_element}は、
411
- 保持するすべての {RangeExtd}オブジェクトに対して {Range#size} を行った
790
+ <tt>_element</tt> がつきます。たとえば、+Rangeary#size+ は、保持する
791
+ RangeExtd オブジェクトの数を返し、一方、{Rangeary#size_element}は、
792
+ 保持するすべての RangeExtdオブジェクトに対して +Range#size+ を行った
412
793
  その総和を返します。
413
794
  Rangeary(1..3, 5..8).size # => 2
414
795
  Rangeary(1..3, 5..8).size_element # => 7
415
796
 
797
+ {Rangeary#flatten_element} は、全てのRangeExtd要素に対して
798
+ +Rangeary#to_a+ を実行して、その結果を結合した配列を返します。
416
799
 
417
- == 既知のバグ
800
+ もしArrayをflattenしたいけれど各Rangeary は保持したい場合は、
801
+ {Rangeary.flatten_no_rangeary} を使えます。
802
+
803
+ クラスとメソッドの完全なマニュアルは、
804
+ {Rubygems のウェブサイト}[http://rubygems.org/gems/rangeary]上にあります。
805
+ あるいは、ソースパッケージを展開して、ルートディレクトリで +make doc+
806
+ することで、手元でコンパイルすることもできます(RubyGems の +yard+
807
+ がインストールされている必要があります)。
808
+
809
+ === 無限大
810
+
811
+ 無限大の扱いは単純ではありません。
812
+
813
+ 最大の注意点として、一旦、無限大が定義されると、そのオブジェクトと演算を行う全ての
814
+ Rangearyオブジェクトも同じ無限大をもつべきです。
815
+
816
+ 取扱いと挙動の詳細は英語版マニュアルの「Infinities」章を参照してください。
817
+
818
+ === Array#==
418
819
 
419
- * 無し。
820
+ 等号メソッド +#==+ は、Array とはほんの少し異なった挙動をします。
821
+ というのも、*empty?* の意味が、異なるからです。なぜならば、
822
+ Rangeary オブジェクトは、Array的な意味では、empty になることは決してありません。
823
+ したがって、{Rangeary#empty_element?} または通常の *empty?* が双方真の時は+Rangeary#==+
824
+ は真を返すように +Array#==+ (したがって +Rangeary#==+)が変更されています。
825
+
826
+ Rangeary Ver.1.0 では、
827
+ Ruby 2.6で導入された
828
+ {Endless Range}[https://rubyreferences.github.io/rubychanges/2.6.html#endless-range-1]
829
+ を概念的に素直に組み込むのが困難であり、Rangeary 内部で矛盾ない扱いを保証するために、
830
+ +Range#==+ に複雑な変更を含みました。Ruby 2.7 で Beginless Range
831
+ が導入されたことでその矛盾が自然に解消されたため、Rangeary Ver.2.0 では、その複雑な判断ルーチンは撤廃されました。
832
+
833
+ 本ライブラリは +RangeExtd+ 付属ライブラリを読み込み、そこでは Object
834
+ の等号が少し変更されています(Ruby標準と互換性はあるので心配無用)。
835
+ これは、Rangeary内部で演算の双方向性を保証するために必要な措置です。
836
+
837
+ なお、+Rangeary#equiv+ メソッドは、等号とは異なる挙動をすることがあり
838
+ ます。例えば、
839
+
840
+ Rangeary(RangeExtd(1,"<...",4), 5...8).equiv?(Rangeary(2..3, 5..7))
841
+
842
+ は真になります。以下のような説明になります。左右の Rangearyは、整数の
843
+ Rangeからなる配列で、
844
+
845
+ * (左側の Rangeary)
846
+ 1. 始端が1 (但し始端自体は含まない)で、終端が4 (但し終端自体は含まない)
847
+ 2. 始端が5 (始端を含む)で、終端が8 (但し終端自体は含まない)
848
+ * (右側の Rangeary)
849
+ 1. 始端が2、終端が3 (両端を含む)
850
+ 2. 始端が5、終端が7 (両端を含む)
851
+
852
+ +Integer#succ+ (あるいは、+Range#each+)メソッド的には、左側と右側とは等価です。したがって、
853
+ +Rangeary#equiv+ でこの両者を比較すると真を返します。一方、等号は負を
854
+ 返します。
855
+
856
+
857
+ == 既知のバグ
420
858
 
421
- このライブラリは Ruby 2.0 以上を必要とします(Ruby 1.9.3 では動くかも
422
- 知れませんが、私は試したことがありません)。
859
+ * Rangeary Ver.2 は Beginless/Endless Ranges (Ruby 2.7以降)をサポートしますが、そのために、
860
+ {RangeExtd}[https://rubygems.org/gems/range_extd] Ver.2 以降が必須。
861
+ * Ruby-2.7 で警告を出さないためには、{RangeExtd}[https://rubygems.org/gems/range_extd] は、Ver.1.1.1 以上であること。
862
+ * +Rangeary#last_element+ はちょっと汚いパッチが入っています(Rangeary
863
+ Ver.1 では例外が発生していた)が、これは、Ruby-2.7以降(3.1.2でもまだ未修正)
864
+ のバグに対処するためです。バグ詳細は、
865
+ {Bug #18994}[https://bugs.ruby-lang.org/issues/18994]。
866
+ すぐに対処されました({patch #6324}[https://github.com/ruby/ruby/pull/6324],
867
+ コミット {bbe5ec7}[https://github.com/ruby/ruby/commit/bbe5ec78463f8d6ef2e1a3571f17357a3d9ec8e4])。
868
+ そもそもごくごく限られたケースでしか問題になることはありませんが。
869
+ * <tt>Rangeary.new(-6..-5, 2..5, 8..8).last_element(3)</tt> は、
870
+ Ruby-2.6.x 以前は正しい値を返すが、Ruby-2.7 ではなぜか <tt>[3, 4, 5]</tt> と誤った値を返す。
871
+
872
+ このライブラリは Ruby 2.7 以上を必要とします。
423
873
 
424
874
  パッケージに含まれている通り、網羅的なテストが実行されています。
425
875
 
@@ -433,14 +883,15 @@ http://rubygems.org/gems/rangeary
433
883
 
434
884
  このライブラリは、イアン・スチュワート氏が開発した、XMM-Newton望遠鏡用の
435
885
  SAS解析ソフトウェア・パッケージに含まれる(数値)複数レンジの
436
- Fortran90ライブラリにアイデアを得たものです。彼の仕事に感謝します。
886
+ FORTRAN90ライブラリにアイデアを得たものです。彼の仕事に感謝します。
437
887
 
438
888
  しかし、Rubyへの実装は、一筋縄ではいきませんでした。一つには組込Range
439
- クラスでは始点を除外することができないこと、また一つには Rubyでは数値
440
- を除いて一般オブジェクトに対しての無限大が定義されていないからです。
441
- 小生の開発した {RangeExtd} によって初めてこのライブラリが可能となりま
442
- した。これにより、今、比較可能な任意のオブジェクトについて、1次元上の
443
- レンジの完全性が実現できたことを嬉しく思います。
889
+ クラスでは始点を除外することができないこと、また一つには Ruby
890
+ では数値を除いて一般オブジェクトに対しての無限大が定義されていないからです
891
+ (後者は、Ruby-2.6と2.7とで一定の解決を見たため、本ライブラリ(Ver.2)でも採り入れました!)。
892
+ 小生の開発した RangeExtd によって初めてこのライブラリが可能となりました。
893
+ これにより、今、比較可能な任意のオブジェクトについて、
894
+ 1次元上のレンジの完全性が実現できたことを嬉しく思います。
444
895
 
445
896
  お楽しみ下さい。
446
897
 
@@ -449,7 +900,7 @@ Fortran90ライブラリにアイデアを得たものです。彼の仕事に
449
900
 
450
901
  == 著作権他情報
451
902
 
452
- 著者:: Masa Sakano < imagine a_t sakano dot co dot uk >
903
+ 著者:: Masa Sakano < info a_t wisebabel dot com >
453
904
  利用許諾条項:: MIT.
454
905
  保証:: 一切無し。
455
906
  バージョン:: Semantic Versioning (2.0.0) http://semver.org/