range_extd 0.4 → 1.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.
- checksums.yaml +4 -4
- data/ChangeLog +21 -0
- data/News +4 -0
- data/README.en.rdoc +39 -22
- data/README.ja.rdoc +39 -22
- data/lib/range_extd/range_extd.rb +50 -19
- data/test/test_range_extd.rb +93 -27
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 32649fb2085566146ed0bd20a5f1198f4030f56477ea64cb4f7822ce6288ff24
|
4
|
+
data.tar.gz: 3fe7fb3ee23f9cd41550445e1482f86292b84dad8b13f0a3984b5e077a16a740
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bc7050082f5af7734ee5946c1f51d21ed633ed275ef93ed9adf9844c8a965f9acab24eef8658cdb06891b852b05ab27bd8a81d0904bce15fbb9a0b0dfb0ba7e1
|
7
|
+
data.tar.gz: 4bf22fb1d18614b5afed4bb8bed88bea3aeb0f90fa8da4c3345be3ee58561313b28453244910c7bac306592945768208dc03cb866c67201eea6c13bc4dbedc34
|
data/ChangeLog
CHANGED
@@ -1,3 +1,24 @@
|
|
1
|
+
-----
|
2
|
+
(Version: 1.0)
|
3
|
+
2019-10-30 Masa Sakano
|
4
|
+
* Release of Ver.1.0.
|
5
|
+
* Endless Range introduced in Ruby 2.6 is now fully supported.
|
6
|
+
* RangeError is raised now when it is appropriate in initialisation, where ArgumentError used to be raised.
|
7
|
+
|
8
|
+
-----
|
9
|
+
(Version: 1.0)
|
10
|
+
2019-10-30 Masa Sakano
|
11
|
+
* Release of Ver.1.0.
|
12
|
+
* Endless Range introduced in Ruby 2.6 is now fully supported.
|
13
|
+
* RangeError is raised now when it is appropriate in initialisation, where ArgumentError used to be raised.
|
14
|
+
|
15
|
+
-----
|
16
|
+
(Version: 1.0)
|
17
|
+
2019-10-30 Masa Sakano
|
18
|
+
* Release of Ver.1.0.
|
19
|
+
* Endless Range introduced in Ruby 2.6 is now fully supported.
|
20
|
+
* RangeError is raised now when it is appropriate in initialisation, where ArgumentError used to be raised.
|
21
|
+
|
1
22
|
-----
|
2
23
|
(Version: 0.4)
|
3
24
|
2019-10-30 Masa Sakano
|
data/News
CHANGED
data/README.en.rdoc
CHANGED
@@ -63,12 +63,16 @@ not 100 per cent trivial. The definition I adopt for the behaviour of
|
|
63
63
|
RangeExtd is probably not the only solution. Personally, I am content
|
64
64
|
with it, and I think it achieves the good logical completeness within the frame.
|
65
65
|
|
66
|
-
I hope you find
|
66
|
+
I hope you find this package to be useful.
|
67
67
|
|
68
|
+
=== News: Endless Range supported
|
69
|
+
|
70
|
+
Now, as of 2019 October, this fully supports {Endless Range}[https://rubyreferences.github.io/rubychanges/2.6.html#endless-range-1]
|
71
|
+
introduced in Ruby 2.6. It is released as Version 1.0 finally!
|
68
72
|
|
69
73
|
==== NOTE: Relationship with Rangesmaller
|
70
74
|
|
71
|
-
This package
|
75
|
+
This package supersedes the obsolete {Rangesmaller}[https://rubygems.org/gems/rangesmaller] package and class,
|
72
76
|
with the added open-ended feature, and a different interface in
|
73
77
|
creating a new instance.
|
74
78
|
https://rubygems.org/gems/rangesmaller
|
@@ -78,7 +82,7 @@ https://rubygems.org/gems/rangesmaller
|
|
78
82
|
The class to handle multiple Ranges with objects of the same class
|
79
83
|
(most typically Float),
|
80
84
|
{Rangeary}[https://rubygems.org/gems/rangeary] uses this library to
|
81
|
-
fullest, because the concept of potentially open-ended Range
|
85
|
+
fullest, because the concept of potentially open-ended Range on both
|
82
86
|
begin and end is essential to realise Rangeary.
|
83
87
|
https://rubygems.org/gems/rangeary
|
84
88
|
|
@@ -176,7 +180,7 @@ All the methods that are in the built-in Range can be used.
|
|
176
180
|
|
177
181
|
== Description
|
178
182
|
|
179
|
-
Once the file range_extd.rb is required, the two classes are defined:
|
183
|
+
Once the file +range_extd/range_extd.rb+ is required, the two classes are defined:
|
180
184
|
|
181
185
|
* RangeExtd
|
182
186
|
* RangeExtd::Infinity
|
@@ -209,8 +213,16 @@ those two constants, as long as the cmp method of the class is written
|
|
209
213
|
politely.
|
210
214
|
|
211
215
|
For more detail, see its documents (YARD or RDoc-style
|
212
|
-
documents embedded in the code, or see RubyGems webpage).
|
216
|
+
documents embedded in the code, or see {RubyGems webpage}[http://rubygems.org/gems/range_extd]).
|
213
217
|
|
218
|
+
**Note** +RangeExtd::Infinity::POSITIVE+ is practically the same as
|
219
|
+
{Endless Range}[https://rubyreferences.github.io/rubychanges/2.6.html#endless-range-1]
|
220
|
+
introduced in Ruby 2.6 released in 2018 December!! In other words,
|
221
|
+
the official Ruby has finally implement a part of this library!
|
222
|
+
However, +RangeExtd::Infinity::NEGATIVE+ is not yet implemented in the
|
223
|
+
official Ruby Range (it has no "boundless begin"), and hence this library still has some use, which
|
224
|
+
supplements the mathematical incompleteness of the standard Range in
|
225
|
+
the official Ruby.
|
214
226
|
|
215
227
|
=== RangeExtd Class
|
216
228
|
|
@@ -308,18 +320,14 @@ existing code in principle.
|
|
308
320
|
|
309
321
|
== Known bugs
|
310
322
|
|
311
|
-
* As of Ruby 2.6, {Endless Range}[https://rubyreferences.github.io/rubychanges/2.6.html#endless-range-1]
|
312
|
-
is introduced. As a result, Range#end sometimes raises RangeError.
|
313
|
-
However, this library does not yet take it into account, and it may
|
314
|
-
raises Exception when encountering an endless Range.
|
315
323
|
* Note this library does not work in Ruby 1.8 or earlier.
|
316
324
|
For Ruby 1.9.3 it is probably all right, though I have never tested it.
|
317
325
|
* Some unusual (rare) boundary conditions are found to vary from
|
318
|
-
version to version in Ruby, such as
|
319
|
-
|
326
|
+
version to version in Ruby, such as an implementation of +Hash#=>+.
|
327
|
+
Though the test scripts are pretty extensive,
|
320
328
|
they have not been performed over many different versions of Ruby.
|
321
|
-
|
322
|
-
|
329
|
+
Hence, some features may not work well in some particular versions, although such
|
330
|
+
cases should be very rare.
|
323
331
|
* {RangeExtd#hash} method does not theoretically guarantee to return a unique
|
324
332
|
number for a {RangeExtd} object, though to encounter a hash number that is
|
325
333
|
used elsewhere is extremely unlikely to happen in reality.
|
@@ -329,7 +337,7 @@ Extensive tests have been performed, as included in the package.
|
|
329
337
|
|
330
338
|
== ToDo
|
331
339
|
|
332
|
-
|
340
|
+
Nothing on the horizon.
|
333
341
|
|
334
342
|
|
335
343
|
== Final notes
|
@@ -369,7 +377,7 @@ License:: MIT.
|
|
369
377
|
Warranty:: No warranty whatsoever.
|
370
378
|
Versions:: The versions of this package follow Semantic Versioning (2.0.0) http://semver.org/
|
371
379
|
|
372
|
-
|
380
|
+
----------
|
373
381
|
|
374
382
|
= RangeExtd - 拡張Rangeクラス - exclude_begin と無限大に開いた範囲と
|
375
383
|
|
@@ -435,6 +443,11 @@ Versions:: The versions of this package follow Semantic Versioning (2.0.0) http:
|
|
435
443
|
このクラスが少なからぬ人に有用なものであることを願ってここにリリースします。
|
436
444
|
|
437
445
|
|
446
|
+
=== News: Endless Range サポートしました
|
447
|
+
|
448
|
+
2019年10月より、本パッケージは、Ruby 2.6 で導入された {Endless Range}[https://rubyreferences.github.io/rubychanges/2.6.html#endless-range-1]
|
449
|
+
(終端のない Range)を正式サポートしました。よって、Version 1.0 をリリースしました!
|
450
|
+
|
438
451
|
==== 注: Rangesmallerとの関係
|
439
452
|
|
440
453
|
このパッケージは、(今やサポートされていない) {Rangesmaller}[https://rubygems.org/gems/rangesmaller] パッケージ及びクラスを
|
@@ -544,7 +557,7 @@ https://rubygems.org/gems/rangeary
|
|
544
557
|
|
545
558
|
== 詳説
|
546
559
|
|
547
|
-
ファイル range_extd.rb が読まれた段階で、次の二つのクラスが定義されます。
|
560
|
+
ファイル +range_extd/range_extd.rb+ が読まれた段階で、次の二つのクラスが定義されます。
|
548
561
|
|
549
562
|
* RangeExtd
|
550
563
|
* RangeExtd::Infinity
|
@@ -575,7 +588,15 @@ https://rubygems.org/gems/rangeary
|
|
575
588
|
る、という前提で。
|
576
589
|
|
577
590
|
さらに詳しくは、マニュアルを参照して下さい(YARD または RDoc形式で書かれた文書が
|
578
|
-
|
591
|
+
コード内部に埋込まれていますし、{RubyGemsのウェブサイト}[http://rubygems.org/gems/range_extd]でも閲覧できます。
|
592
|
+
|
593
|
+
**(注)** +RangeExtd::Infinity::POSITIVE+ は、
|
594
|
+
2018年12月に公式リリースされたRuby 2.6で導入された
|
595
|
+
{Endless Range}[https://rubyreferences.github.io/rubychanges/2.6.html#endless-range-1]
|
596
|
+
(終端のないRange)で実用上同一です!! 言葉を替えれば、公式Rubyがついに本
|
597
|
+
ライブラリの一部をサポートしました! ただし、公式Rubyには、
|
598
|
+
+RangeExtd::Infinity::NEGATIVE+ は依然ありません(始端のないRangeがない)。
|
599
|
+
本ライブラリにより、組込Rangeに欠けている数学的不完全性を補うことができます。
|
579
600
|
|
580
601
|
|
581
602
|
=== RangeExtd クラス
|
@@ -665,10 +686,6 @@ RangeExtd と別の RangeExtd または Rangeの比較 (<tt><=></tt>) におい
|
|
665
686
|
|
666
687
|
== 既知のバグ
|
667
688
|
|
668
|
-
|
669
|
-
* Ruby 2.6 では、後ろに無限に開いたRange({Endless Range}[https://rubyreferences.github.io/rubychanges/2.6.html#endless-range-1])
|
670
|
-
が導入されました。本ライブラリでは、まだそれには対応していません。
|
671
|
-
したがって、Endless Rangeを本クラスに適用した場合、問題が起きるでしょう。
|
672
689
|
* このライブラリは Ruby 1.8 およびそれ以前のバージョンでは動作しません。
|
673
690
|
Ruby 1.9.3 ではおそらく大丈夫でしょうが、私は試したことがありません。
|
674
691
|
* いくつかの極めて稀な境界条件に於ける挙動は、Rubyのバージョンごとにあ
|
@@ -685,7 +702,7 @@ RangeExtd と別の RangeExtd または Rangeの比較 (<tt><=></tt>) におい
|
|
685
702
|
|
686
703
|
== 開発項目
|
687
704
|
|
688
|
-
|
705
|
+
特になし。
|
689
706
|
|
690
707
|
|
691
708
|
== 終わりに
|
data/README.ja.rdoc
CHANGED
@@ -63,12 +63,16 @@ not 100 per cent trivial. The definition I adopt for the behaviour of
|
|
63
63
|
RangeExtd is probably not the only solution. Personally, I am content
|
64
64
|
with it, and I think it achieves the good logical completeness within the frame.
|
65
65
|
|
66
|
-
I hope you find
|
66
|
+
I hope you find this package to be useful.
|
67
67
|
|
68
|
+
=== News: Endless Range supported
|
69
|
+
|
70
|
+
Now, as of 2019 October, this fully supports {Endless Range}[https://rubyreferences.github.io/rubychanges/2.6.html#endless-range-1]
|
71
|
+
introduced in Ruby 2.6. It is released as Version 1.0 finally!
|
68
72
|
|
69
73
|
==== NOTE: Relationship with Rangesmaller
|
70
74
|
|
71
|
-
This package
|
75
|
+
This package supersedes the obsolete {Rangesmaller}[https://rubygems.org/gems/rangesmaller] package and class,
|
72
76
|
with the added open-ended feature, and a different interface in
|
73
77
|
creating a new instance.
|
74
78
|
https://rubygems.org/gems/rangesmaller
|
@@ -78,7 +82,7 @@ https://rubygems.org/gems/rangesmaller
|
|
78
82
|
The class to handle multiple Ranges with objects of the same class
|
79
83
|
(most typically Float),
|
80
84
|
{Rangeary}[https://rubygems.org/gems/rangeary] uses this library to
|
81
|
-
fullest, because the concept of potentially open-ended Range
|
85
|
+
fullest, because the concept of potentially open-ended Range on both
|
82
86
|
begin and end is essential to realise Rangeary.
|
83
87
|
https://rubygems.org/gems/rangeary
|
84
88
|
|
@@ -176,7 +180,7 @@ All the methods that are in the built-in Range can be used.
|
|
176
180
|
|
177
181
|
== Description
|
178
182
|
|
179
|
-
Once the file range_extd.rb is required, the two classes are defined:
|
183
|
+
Once the file +range_extd/range_extd.rb+ is required, the two classes are defined:
|
180
184
|
|
181
185
|
* RangeExtd
|
182
186
|
* RangeExtd::Infinity
|
@@ -209,8 +213,16 @@ those two constants, as long as the cmp method of the class is written
|
|
209
213
|
politely.
|
210
214
|
|
211
215
|
For more detail, see its documents (YARD or RDoc-style
|
212
|
-
documents embedded in the code, or see RubyGems webpage).
|
216
|
+
documents embedded in the code, or see {RubyGems webpage}[http://rubygems.org/gems/range_extd]).
|
213
217
|
|
218
|
+
**Note** +RangeExtd::Infinity::POSITIVE+ is practically the same as
|
219
|
+
{Endless Range}[https://rubyreferences.github.io/rubychanges/2.6.html#endless-range-1]
|
220
|
+
introduced in Ruby 2.6 released in 2018 December!! In other words,
|
221
|
+
the official Ruby has finally implement a part of this library!
|
222
|
+
However, +RangeExtd::Infinity::NEGATIVE+ is not yet implemented in the
|
223
|
+
official Ruby Range (it has no "boundless begin"), and hence this library still has some use, which
|
224
|
+
supplements the mathematical incompleteness of the standard Range in
|
225
|
+
the official Ruby.
|
214
226
|
|
215
227
|
=== RangeExtd Class
|
216
228
|
|
@@ -308,18 +320,14 @@ existing code in principle.
|
|
308
320
|
|
309
321
|
== Known bugs
|
310
322
|
|
311
|
-
* As of Ruby 2.6, {Endless Range}[https://rubyreferences.github.io/rubychanges/2.6.html#endless-range-1]
|
312
|
-
is introduced. As a result, Range#end sometimes raises RangeError.
|
313
|
-
However, this library does not yet take it into account, and it may
|
314
|
-
raises Exception when encountering an endless Range.
|
315
323
|
* Note this library does not work in Ruby 1.8 or earlier.
|
316
324
|
For Ruby 1.9.3 it is probably all right, though I have never tested it.
|
317
325
|
* Some unusual (rare) boundary conditions are found to vary from
|
318
|
-
version to version in Ruby, such as
|
319
|
-
|
326
|
+
version to version in Ruby, such as an implementation of +Hash#=>+.
|
327
|
+
Though the test scripts are pretty extensive,
|
320
328
|
they have not been performed over many different versions of Ruby.
|
321
|
-
|
322
|
-
|
329
|
+
Hence, some features may not work well in some particular versions, although such
|
330
|
+
cases should be very rare.
|
323
331
|
* {RangeExtd#hash} method does not theoretically guarantee to return a unique
|
324
332
|
number for a {RangeExtd} object, though to encounter a hash number that is
|
325
333
|
used elsewhere is extremely unlikely to happen in reality.
|
@@ -329,7 +337,7 @@ Extensive tests have been performed, as included in the package.
|
|
329
337
|
|
330
338
|
== ToDo
|
331
339
|
|
332
|
-
|
340
|
+
Nothing on the horizon.
|
333
341
|
|
334
342
|
|
335
343
|
== Final notes
|
@@ -369,7 +377,7 @@ License:: MIT.
|
|
369
377
|
Warranty:: No warranty whatsoever.
|
370
378
|
Versions:: The versions of this package follow Semantic Versioning (2.0.0) http://semver.org/
|
371
379
|
|
372
|
-
|
380
|
+
----------
|
373
381
|
|
374
382
|
= RangeExtd - 拡張Rangeクラス - exclude_begin と無限大に開いた範囲と
|
375
383
|
|
@@ -435,6 +443,11 @@ Versions:: The versions of this package follow Semantic Versioning (2.0.0) http:
|
|
435
443
|
このクラスが少なからぬ人に有用なものであることを願ってここにリリースします。
|
436
444
|
|
437
445
|
|
446
|
+
=== News: Endless Range サポートしました
|
447
|
+
|
448
|
+
2019年10月より、本パッケージは、Ruby 2.6 で導入された {Endless Range}[https://rubyreferences.github.io/rubychanges/2.6.html#endless-range-1]
|
449
|
+
(終端のない Range)を正式サポートしました。よって、Version 1.0 をリリースしました!
|
450
|
+
|
438
451
|
==== 注: Rangesmallerとの関係
|
439
452
|
|
440
453
|
このパッケージは、(今やサポートされていない) {Rangesmaller}[https://rubygems.org/gems/rangesmaller] パッケージ及びクラスを
|
@@ -544,7 +557,7 @@ https://rubygems.org/gems/rangeary
|
|
544
557
|
|
545
558
|
== 詳説
|
546
559
|
|
547
|
-
ファイル range_extd.rb が読まれた段階で、次の二つのクラスが定義されます。
|
560
|
+
ファイル +range_extd/range_extd.rb+ が読まれた段階で、次の二つのクラスが定義されます。
|
548
561
|
|
549
562
|
* RangeExtd
|
550
563
|
* RangeExtd::Infinity
|
@@ -575,7 +588,15 @@ https://rubygems.org/gems/rangeary
|
|
575
588
|
る、という前提で。
|
576
589
|
|
577
590
|
さらに詳しくは、マニュアルを参照して下さい(YARD または RDoc形式で書かれた文書が
|
578
|
-
|
591
|
+
コード内部に埋込まれていますし、{RubyGemsのウェブサイト}[http://rubygems.org/gems/range_extd]でも閲覧できます。
|
592
|
+
|
593
|
+
**(注)** +RangeExtd::Infinity::POSITIVE+ は、
|
594
|
+
2018年12月に公式リリースされたRuby 2.6で導入された
|
595
|
+
{Endless Range}[https://rubyreferences.github.io/rubychanges/2.6.html#endless-range-1]
|
596
|
+
(終端のないRange)で実用上同一です!! 言葉を替えれば、公式Rubyがついに本
|
597
|
+
ライブラリの一部をサポートしました! ただし、公式Rubyには、
|
598
|
+
+RangeExtd::Infinity::NEGATIVE+ は依然ありません(始端のないRangeがない)。
|
599
|
+
本ライブラリにより、組込Rangeに欠けている数学的不完全性を補うことができます。
|
579
600
|
|
580
601
|
|
581
602
|
=== RangeExtd クラス
|
@@ -665,10 +686,6 @@ RangeExtd と別の RangeExtd または Rangeの比較 (<tt><=></tt>) におい
|
|
665
686
|
|
666
687
|
== 既知のバグ
|
667
688
|
|
668
|
-
|
669
|
-
* Ruby 2.6 では、後ろに無限に開いたRange({Endless Range}[https://rubyreferences.github.io/rubychanges/2.6.html#endless-range-1])
|
670
|
-
が導入されました。本ライブラリでは、まだそれには対応していません。
|
671
|
-
したがって、Endless Rangeを本クラスに適用した場合、問題が起きるでしょう。
|
672
689
|
* このライブラリは Ruby 1.8 およびそれ以前のバージョンでは動作しません。
|
673
690
|
Ruby 1.9.3 ではおそらく大丈夫でしょうが、私は試したことがありません。
|
674
691
|
* いくつかの極めて稀な境界条件に於ける挙動は、Rubyのバージョンごとにあ
|
@@ -685,7 +702,7 @@ RangeExtd と別の RangeExtd または Rangeの比較 (<tt><=></tt>) におい
|
|
685
702
|
|
686
703
|
== 開発項目
|
687
704
|
|
688
|
-
|
705
|
+
特になし。
|
689
706
|
|
690
707
|
|
691
708
|
== 終わりに
|
@@ -62,6 +62,12 @@ class RangeExtd < Range
|
|
62
62
|
|
63
63
|
@@middle_strings = []
|
64
64
|
|
65
|
+
# Error messages
|
66
|
+
ERR_MSGS = {
|
67
|
+
infinity_compare: 'Float::INFINITY is not mathematically comparable with another Infinity.',
|
68
|
+
}
|
69
|
+
private_constant :ERR_MSGS
|
70
|
+
|
65
71
|
# @note The flag of exclude_begin|end can be given in the arguments in a couple of ways.
|
66
72
|
# If there is any duplication, those specified in the optional hash have the highest
|
67
73
|
# priority. Then the two descrete Boolean parameters have the second.
|
@@ -103,9 +109,18 @@ class RangeExtd < Range
|
|
103
109
|
# @option opts [Boolean] :exclude_end If specified, this has the higher priority, or false in default.
|
104
110
|
#
|
105
111
|
# Note if you use the third form with "string_form" with the user-defined string
|
106
|
-
# (via {RangeExtd.middle_strings=}()), make 100 per cent sure
|
107
|
-
#
|
112
|
+
# (via {RangeExtd.middle_strings=}()), make 100 per cent sure you know
|
113
|
+
# what you are doing. If the string is ambiguous, the result may differ
|
108
114
|
# from what you thought you would get! See {RangeExtd.middle_strings=}() for detail.
|
115
|
+
# Below are a couple of examples:
|
116
|
+
#
|
117
|
+
# RangeExtd.new(5, '....', 6) # => RangeError because (5..("....")) is an invalid Range.
|
118
|
+
# RangeExtd.new("%", '....', "y") # => ("%" <.. "....")
|
119
|
+
# # n.b., "y" is interpreted as TRUE for
|
120
|
+
# # the flag for "exclude_begin?"
|
121
|
+
# RangeExtd.new("x", '....', "y") # => RangeError because ("x" <..("....")) is an invalid RangeExte,
|
122
|
+
# # in the sense String "...." is *smaller* than "x"
|
123
|
+
# # in terms of the "<=>" operator comparison.
|
109
124
|
#
|
110
125
|
# @raise [ArgumentError] particularly if the range to be created is not {#valid?}.
|
111
126
|
def initialize(*inar, **hsopt) # **k expression from Ruby 1.9?
|
@@ -118,8 +133,9 @@ class RangeExtd < Range
|
|
118
133
|
return
|
119
134
|
end
|
120
135
|
|
121
|
-
#
|
122
|
-
arout = RangeExtd.
|
136
|
+
# Note: the order of exclude_begin? and end? is reversed from the input!
|
137
|
+
arout = RangeExtd.send(:_get_init_args, *inar, hsopt)
|
138
|
+
# == [RangeBeginValue, RangeEndValue, exclude_end?, exclude_begin?]
|
123
139
|
|
124
140
|
### The following routine is obsolete.
|
125
141
|
### Users, if they wish, should call RangeExtd::Infinity.overwrite_compare() beforehand.
|
@@ -151,7 +167,7 @@ class RangeExtd < Range
|
|
151
167
|
# end
|
152
168
|
|
153
169
|
if ! RangeExtd.valid?(*arout)
|
154
|
-
raise
|
170
|
+
raise RangeError, "the combination of the arguments does not constitute a valid RangeExtd instance."
|
155
171
|
end
|
156
172
|
|
157
173
|
@exclude_begin = arout.pop
|
@@ -820,6 +836,8 @@ class RangeExtd < Range
|
|
820
836
|
##################################################
|
821
837
|
|
822
838
|
# Private class method to evaluate the arguments.
|
839
|
+
#
|
840
|
+
# @raise [ArgumentError] if the input format is invalid (otherwise the caller may raise RangeError (it depends))
|
823
841
|
def self._get_init_args(*inar, **hsopt)
|
824
842
|
nMin = 1; nMax = 5
|
825
843
|
if inar.size < nMin || nMax < inar.size
|
@@ -875,11 +893,11 @@ class RangeExtd < Range
|
|
875
893
|
# Now, checking if the form is the String one, and if so, process it.
|
876
894
|
arMid = @@middle_strings.map{|i| Regexp.quote(i)} # See self.middle_strings=(ary) for description.
|
877
895
|
|
878
|
-
# Originally, defined?(inar[1].=~) seemed enough. But as of Ruby 2.6,
|
896
|
+
# Originally, defined?(inar[1].=~) seemed enough. But as of Ruby 2.6 (maybe even before),
|
879
897
|
# Numeric has :=~ method as well!
|
880
|
-
if inar.size > 2 &&
|
881
|
-
|
882
|
-
|
898
|
+
if (inar.size > 2 &&
|
899
|
+
inar[1].class.method_defined?(:=~) &&
|
900
|
+
inar[1].class.method_defined?(:to_str))
|
883
901
|
begin
|
884
902
|
cmp = (inar[0] <=> inar[2]).abs
|
885
903
|
rescue
|
@@ -934,7 +952,6 @@ class RangeExtd < Range
|
|
934
952
|
if hsopt.has_key?(:exclude_end)
|
935
953
|
exclude_end = (hsopt[:exclude_end] && true)
|
936
954
|
end
|
937
|
-
|
938
955
|
# [RangeBeginValue, RangeEndValue, exclude_end?, exclude_begin?]
|
939
956
|
beginend + [exclude_end, exclude_begin]
|
940
957
|
|
@@ -954,13 +971,17 @@ class RangeExtd < Range
|
|
954
971
|
#
|
955
972
|
# What is valid is defined as follows:
|
956
973
|
#
|
957
|
-
# 1.
|
974
|
+
# 1. The {#begin} and {#end} elements must be Comparable to each other,
|
958
975
|
# and the comparison results must be consistent betwen the two.
|
959
|
-
# The sole
|
976
|
+
# The two sole exceptions are {RangeExtd::NONE} and Endless Range
|
977
|
+
# introduced in Ruby 2.6 (see below for the exceptions), both of which are valid.
|
960
978
|
# For example, (nil..nil) is NOT valid (nb., it raised Exception in Ruby 1.8).
|
961
|
-
# 2. {#begin} must
|
979
|
+
# 2. Except for {RangeExtd::NONE}, {#begin} must have the method +<=+.
|
980
|
+
# Therefore, some Endless Ranges (Ruby 2.6 and upwards) like (true..) are *not* valid.
|
981
|
+
# Note even "true" has the method +<=>+ and hence checking +<=+ is essential.
|
982
|
+
# 3. {#begin} must be smaller than or equal to {#end},
|
962
983
|
# that is, ({#begin} <=> {#end}) must be either -1 or 0.
|
963
|
-
#
|
984
|
+
# 4. If {#begin} is equal to {#end}, namely, ({#begin} <=> {#end}) == 0,
|
964
985
|
# the exclude status of the both ends must agree.
|
965
986
|
# That is, if the {#begin} is excluded, {#end} must be also excluded,
|
966
987
|
# and vice versa.
|
@@ -981,6 +1002,8 @@ class RangeExtd < Range
|
|
981
1002
|
# RangeExtd.valid?(nil...nil) # => false
|
982
1003
|
# RangeExtd.valid?(0..0) # => true
|
983
1004
|
# RangeExtd.valid?(0...0) # => false
|
1005
|
+
# RangeExtd.valid?(0...) # => true
|
1006
|
+
# RangeExtd.valid?(true..) # => false
|
984
1007
|
# RangeExtd.valid?(0..0, true) # => false
|
985
1008
|
# RangeExtd.valid?(0...0, true) # => true
|
986
1009
|
# RangeExtd.valid?(2..-1) # => false
|
@@ -998,13 +1021,13 @@ class RangeExtd < Range
|
|
998
1021
|
# in the parameter are used. In default, both of them are false.
|
999
1022
|
#
|
1000
1023
|
# @overload new(range, [exclude_begin=false, [exclude_end=false]])
|
1001
|
-
# @param [Object]
|
1024
|
+
# @param range [Object] Instance of Range or its subclasses, including RangeExtd
|
1002
1025
|
# @param exclude_begin [Boolean] If specified, this has the higher priority, or false in default.
|
1003
1026
|
# @param exclude_end [Boolean] If specified, this has the higher priority, or false in default.
|
1004
1027
|
#
|
1005
1028
|
# @overload new(obj_begin, obj_end, [exclude_begin=false, [exclude_end=false]])
|
1006
1029
|
# @param obj_begin [Object] Any object that is {Comparable} with end
|
1007
|
-
# @param obj_end [Object] Any object that is Comparable with begin
|
1030
|
+
# @param obj_end [Object] Any object that is Comparable with begin (or nil, for Ruby 2.6 onwards)
|
1008
1031
|
# @param exclude_begin [Boolean] If specified, this has the lower priority, or false in default.
|
1009
1032
|
# @param exclude_end [Boolean] If specified, this has the lower priority, or false in default.
|
1010
1033
|
#
|
@@ -1014,15 +1037,23 @@ class RangeExtd < Range
|
|
1014
1037
|
if defined?(inar[0].is_none?) && inar[0].is_none? && exc_beg && exc_end
|
1015
1038
|
return true
|
1016
1039
|
end
|
1017
|
-
|
1018
1040
|
begin
|
1019
1041
|
t = (vbeg <=> vend)
|
1020
1042
|
begin
|
1043
|
+
if vend.nil?
|
1044
|
+
begin
|
1045
|
+
_ = (vbeg..nil) # Endless Range introduced in Ruby 2.6
|
1046
|
+
return vbeg.class.method_defined?(:<=)
|
1047
|
+
rescue ArgumentError
|
1048
|
+
# Before Ruby 2.6
|
1049
|
+
return false
|
1050
|
+
end
|
1051
|
+
end
|
1021
1052
|
return false if t != -1*(vend <=> vbeg) # false if not commutative, or possibly exception (such as -1*nil).
|
1022
1053
|
rescue NoMethodError, TypeError
|
1023
1054
|
if (Float === vend && defined?(vbeg.infinity?) && vbeg.infinity?) ||
|
1024
1055
|
(Float === vbeg && defined?(vend.infinity?) && vend.infinity?)
|
1025
|
-
warn
|
1056
|
+
warn self.const_get(:ERR_MSGS)[:infinity_compare] # one of the tests comes here.
|
1026
1057
|
end
|
1027
1058
|
return false # return
|
1028
1059
|
end
|
@@ -1043,7 +1074,7 @@ class RangeExtd < Range
|
|
1043
1074
|
else
|
1044
1075
|
if (Float === vend && defined?(vbeg.infinity?) && vbeg.infinity?) ||
|
1045
1076
|
(Float === vbeg && defined?(vend.infinity?) && vend.infinity?)
|
1046
|
-
warn
|
1077
|
+
warn self.const_get(:ERR_MSGS)[:infinity_compare] # not tested so far?
|
1047
1078
|
end
|
1048
1079
|
false # Not Comparable.
|
1049
1080
|
end # case t
|
data/test/test_range_extd.rb
CHANGED
@@ -250,6 +250,17 @@ gem "minitest"
|
|
250
250
|
assert_equal RangeExtd::Infinity::NEGATIVE, r.begin
|
251
251
|
end # def test_rangeextd_new_infinity_c3
|
252
252
|
|
253
|
+
def test_get_init_args
|
254
|
+
begin
|
255
|
+
_ = (0..nil)
|
256
|
+
rescue ArgumentError
|
257
|
+
return # Before Ruby 2.6
|
258
|
+
end
|
259
|
+
arout = RangeExtd.class_eval{ _get_init_args(3, nil) } # class_eval from Ruby 1.8.7 (?)
|
260
|
+
assert_equal [3, nil, F, F], arout
|
261
|
+
arout = RangeExtd.class_eval{ _get_init_args(3, nil, F, T) } # true == exclude_end?
|
262
|
+
assert_equal [3, nil, T, F], arout
|
263
|
+
end
|
253
264
|
|
254
265
|
def test_new
|
255
266
|
e22 = RangeExtd.new(@s22)
|
@@ -283,6 +294,28 @@ gem "minitest"
|
|
283
294
|
assert_equal(-@ib, j22.end)
|
284
295
|
end
|
285
296
|
|
297
|
+
def test_new_endless_range01
|
298
|
+
ra00 = (-2..)
|
299
|
+
rae0 = RangeExtd.new(ra00)
|
300
|
+
assert rae0.valid?
|
301
|
+
assert_equal(-2, rae0.begin)
|
302
|
+
assert_nil rae0.end
|
303
|
+
assert_equal Float::INFINITY, rae0.size
|
304
|
+
assert_equal ra00.exclude_end?, rae0.exclude_end?
|
305
|
+
refute rae0.exclude_begin?
|
306
|
+
end
|
307
|
+
|
308
|
+
def test_new_endless_range02
|
309
|
+
ra00 = (Float::INFINITY...)
|
310
|
+
rae0 = RangeExtd.new(ra00)
|
311
|
+
assert rae0.valid?
|
312
|
+
assert_equal Float::INFINITY, rae0.begin
|
313
|
+
assert_nil rae0.end
|
314
|
+
assert_equal Float::INFINITY, rae0.size
|
315
|
+
assert_equal ra00.exclude_end?, rae0.exclude_end?
|
316
|
+
refute rae0.exclude_begin?
|
317
|
+
end
|
318
|
+
|
286
319
|
def test_new_middle_strings
|
287
320
|
aru = ['[','(in)','(ex)',', ','(ex)','(in)',']']
|
288
321
|
ark = [nil, :math, aru, :default]
|
@@ -339,9 +372,21 @@ gem "minitest"
|
|
339
372
|
assert_equal(-@ie, m22.begin)
|
340
373
|
assert_equal(-@ib, m22.end)
|
341
374
|
|
342
|
-
assert_raises(
|
375
|
+
assert_raises(RangeError){ RangeExtd.new(@ib, '....', @ie) } # => the combination of the arguments does not constitute a valid RangeExtd instance.
|
376
|
+
# Basically, for @ib, @ie == [1, 6],
|
377
|
+
# the user may intend to write "1....6", but it is interpreted as
|
378
|
+
# ((1..("....")), exclude_begin: (!!6))
|
379
|
+
# hence it is RangeError (as opposed to ArgumentError)
|
380
|
+
|
381
|
+
rt0 = RangeExtd.new("%", '....', "y")
|
382
|
+
assert_equal "%", rt0.begin
|
383
|
+
assert_equal '....', rt0.end
|
384
|
+
assert rt0.exclude_begin?
|
385
|
+
refute rt0.exclude_end?
|
386
|
+
|
343
387
|
RangeExtd.middle_strings = :math
|
344
|
-
assert_raises(
|
388
|
+
assert_raises(RangeError){ RangeExtd.new(@ib, '..', @ie) } # => the combination of the arguments does not constitute a valid RangeExtd instance.
|
389
|
+
|
345
390
|
RangeExtd.middle_strings = :default
|
346
391
|
assert_raises(ArgumentError){ RangeExtd.new(@ib, '..', @ie, T, nil, T) } # => wrong number of arguments (6 for 1..5)
|
347
392
|
assert_raises(ArgumentError){ RangeExtd.new(@ib, @ie, T, nil, T) } # => wrong number of arguments (5 for 2..4)
|
@@ -354,6 +399,7 @@ gem "minitest"
|
|
354
399
|
|
355
400
|
def test_new_invalid
|
356
401
|
ae = ArgumentError
|
402
|
+
re = RangeError
|
357
403
|
# Wrong number of parameters
|
358
404
|
assert_raises(ae){ RangeExtd() }
|
359
405
|
assert_raises(ae){ RangeExtd(1,2,3,4,5) }
|
@@ -362,39 +408,39 @@ gem "minitest"
|
|
362
408
|
assert_raises(ae){ RangeExtd(1..1,2,3,{},5) }
|
363
409
|
|
364
410
|
# Wrong range (Object input)
|
365
|
-
assert_raises(
|
366
|
-
assert_raises(
|
367
|
-
assert_raises(
|
368
|
-
assert_raises(
|
369
|
-
assert_raises(
|
370
|
-
assert_raises(
|
371
|
-
assert_raises(
|
411
|
+
assert_raises(re){ RaE(2, -1) }
|
412
|
+
assert_raises(re){ RaE(nil, nil) }
|
413
|
+
assert_raises(re){ RaE(nil, false) }
|
414
|
+
assert_raises(re){ RaE(?d..?a) }
|
415
|
+
assert_raises(re){ RaE(?a, 5) }
|
416
|
+
assert_raises(re){ RaE(0, 0, true, false) }
|
417
|
+
assert_raises(re){ RaE(0, 0, nil, 1) }
|
372
418
|
assert_equal RangeExtd::NONE, RaE(0, 0, true, true)
|
373
419
|
assert_equal RangeExtd::NONE, RaE(?a, ?a, true, true)
|
374
420
|
assert_equal (0..0), RaE(0, 0, false, false)
|
375
421
|
|
376
422
|
# Wrong range (Infinity input)
|
377
|
-
assert_raises(
|
423
|
+
assert_raises(re){ RaE(?a, RangeExtd::Infinity::NEGATIVE) }
|
378
424
|
assert_equal (RangeExtd::Infinity::NEGATIVE..?a), RaE(RangeExtd::Infinity::NEGATIVE, ?a)
|
379
425
|
assert_equal (RangeExtd::Infinity::NEGATIVE...?a), RaE(RangeExtd::Infinity::NEGATIVE, ?a, nil, 3)
|
380
426
|
assert_equal (?a..RangeExtd::Infinity::POSITIVE), RaE(?a, RangeExtd::Infinity::POSITIVE)
|
381
427
|
assert_equal RangeExtd, RaE(?a, RangeExtd::Infinity::POSITIVE, 1).class
|
382
|
-
assert_raises(
|
383
|
-
assert_raises(
|
384
|
-
assert_raises(
|
385
|
-
assert_raises(
|
428
|
+
assert_raises(re){ RaE(RangeExtd::Infinity::NEGATIVE, Float::INFINITY) } # Float::INFINITY is an exception - you should not mix it up.
|
429
|
+
assert_raises(re){ RaE(-Float::INFINITY, RangeExtd::Infinity::POSITIVE) } # Float::INFINITY is an exception - you should not mix it up.
|
430
|
+
assert_raises(re){ RaE(RangeExtd::Infinity::POSITIVE, ?a) }
|
431
|
+
assert_raises(re){ RaE(RangeExtd::Infinity::POSITIVE, RangeExtd::Infinity::NEGATIVE) }
|
386
432
|
assert_equal RangeExtd, RaE(RangeExtd::Infinity::NEGATIVE, RangeExtd::Infinity::POSITIVE).class
|
387
|
-
assert_raises(
|
388
|
-
assert_raises(
|
433
|
+
assert_raises(re){ RaE(RangeExtd::Infinity::NEGATIVE, 0, false, false) } # For Numeric, you should use -Float::INFINITY
|
434
|
+
assert_raises(re){ RaE(0, RangeExtd::Infinity::POSITIVE, false, false) } # For Numeric, you should use Float::INFINITY
|
389
435
|
assert_equal RangeExtd, RaE(RangeExtd::Infinity::NEGATIVE, ?a, false, false).class
|
390
436
|
assert_equal RangeExtd, RaE(?a, RangeExtd::Infinity::POSITIVE, false, false).class
|
391
437
|
# assert_raises(ae){ RaE(RangeExtd::Infinity::NEGATIVE, ?a, true) } #### No exception. Is it OK???
|
392
438
|
# assert_raises(ae){ RaE(?a, RangeExtd::Infinity::POSITIVE, nil, 1) } #### No exception. Is it OK???
|
393
439
|
|
394
440
|
# Wrong range (Range input)
|
395
|
-
assert_raises(
|
396
|
-
assert_raises(
|
397
|
-
assert_raises(
|
441
|
+
assert_raises(re){ RangeExtd(2..-1) }
|
442
|
+
assert_raises(re){ RangeExtd(nil..nil) }
|
443
|
+
assert_raises(re){ RangeExtd(?d..?a) }
|
398
444
|
|
399
445
|
# Range with contradictory boundary
|
400
446
|
assert_equal ?a..?e, RaE(?a...?e, nil, nil)
|
@@ -402,10 +448,19 @@ gem "minitest"
|
|
402
448
|
assert_equal ?a..?a, RaE(?a...?a, nil, nil)
|
403
449
|
assert_equal RangeExtd::NONE, RaE(?a...?a, 1, 1)
|
404
450
|
assert_equal RangeExtd::NONE, RaE(?a..?b, 1, 1)
|
405
|
-
assert_raises(
|
406
|
-
assert_raises(
|
451
|
+
assert_raises(re){ RaE(?a..?a, true, nil) }
|
452
|
+
assert_raises(re){ RaE(?a..?a, nil, 1) }
|
407
453
|
end
|
408
454
|
|
455
|
+
def test_new_invalid_endless_range01
|
456
|
+
ra00 = (nil..)
|
457
|
+
refute RangeExtd.valid?(ra00)
|
458
|
+
assert_raises(RangeError){ RangeExtd.new(ra00) }
|
459
|
+
|
460
|
+
ra01 = (true..)
|
461
|
+
refute RangeExtd.valid?(ra01)
|
462
|
+
assert_raises(RangeError){ RangeExtd.new(ra01) }
|
463
|
+
end
|
409
464
|
|
410
465
|
def test_new_exclude_begin_end
|
411
466
|
# Form 1: 1 parameters
|
@@ -502,10 +557,10 @@ gem "minitest"
|
|
502
557
|
assert_raises ArgumentError do
|
503
558
|
RangeExtd.new(5)
|
504
559
|
end
|
505
|
-
assert_raises
|
560
|
+
assert_raises RangeError do
|
506
561
|
RangeExtd.new(nil,5)
|
507
562
|
end
|
508
|
-
assert_raises
|
563
|
+
assert_raises RangeError do
|
509
564
|
RangeExtd.new(nil,5,true)
|
510
565
|
end
|
511
566
|
end # def test_exclude_begin
|
@@ -638,7 +693,7 @@ gem "minitest"
|
|
638
693
|
assert_equal(@ib+1, @s21.first(1)[0])
|
639
694
|
assert_equal(@ib+1, @s22.first(1)[0])
|
640
695
|
|
641
|
-
assert_raises
|
696
|
+
assert_raises RangeError do
|
642
697
|
RangeExtd(9, 3, true)
|
643
698
|
end
|
644
699
|
assert_raises ArgumentError do
|
@@ -670,7 +725,7 @@ gem "minitest"
|
|
670
725
|
end
|
671
726
|
|
672
727
|
## Else
|
673
|
-
assert_raises
|
728
|
+
assert_raises RangeError do
|
674
729
|
RangeExtd.new(nil..nil, true)
|
675
730
|
end
|
676
731
|
|
@@ -898,11 +953,11 @@ gem "minitest"
|
|
898
953
|
inf = Float::INFINITY
|
899
954
|
excl_ini = true
|
900
955
|
assert_equal inf, RangeExtd(-inf, 1).size
|
901
|
-
assert_raises
|
956
|
+
assert_raises RangeError do
|
902
957
|
RangeExtd(-inf, -inf, excl_ini) # exclde_begin yet !exclude_end
|
903
958
|
end
|
904
959
|
assert_equal inf, RangeExtd(-inf, 1, excl_ini).size
|
905
|
-
assert_raises
|
960
|
+
assert_raises RangeError do
|
906
961
|
RangeExtd( inf, inf, excl_ini) # exclde_begin yet !exclude_end
|
907
962
|
end
|
908
963
|
assert_equal inf, RangeExtd( 1, inf, excl_ini).size
|
@@ -1058,6 +1113,17 @@ gem "minitest"
|
|
1058
1113
|
assert RangeExtd(RangeExtd::Infinity::NEGATIVE..?b).valid?
|
1059
1114
|
end # def test_Range_valid
|
1060
1115
|
|
1116
|
+
# For Ruby 2.6 and onwards
|
1117
|
+
def test_endlessRange_valid
|
1118
|
+
assert (0..).valid?
|
1119
|
+
assert (0...).valid?
|
1120
|
+
refute (true..).valid?
|
1121
|
+
refute (nil...).valid?
|
1122
|
+
refute (nil..nil).valid?
|
1123
|
+
assert (Float::INFINITY..Float::INFINITY).valid?
|
1124
|
+
assert (-Float::INFINITY..Float::INFINITY).valid?
|
1125
|
+
assert (-Float::INFINITY...Float::INFINITY).valid?
|
1126
|
+
end
|
1061
1127
|
|
1062
1128
|
def test_Range_empty
|
1063
1129
|
assert RangeExtd::NONE.empty?
|