378 Commits

Author SHA1 Message Date
flavio-b
80ab42462e [DOC] beginless and endless ranges inspect returns "nil..nil" always
Small correction to the Range docs. All beginless (nil..) and endless (..nil) ranges, as well as fully open ranges (nil..nil), display "nil..nil" when inspected, regardless of their original syntax.
2025-03-21 10:30:40 +09:00
Jérôme Parent-Lévesque
b5cdbadeed [Bug #21185] Fix Range#overlap? with infinite range
Infinite ranges, i.e. unbounded ranges, should overlap with any other range
which wasn't the case in the following example: (0..3).overlap?(nil..nil)
2025-03-17 16:26:23 +09:00
Nobuyoshi Nakada
cbe3156f82
[Bug #21174] [Bug #21175] Fix Range#max on beginless integer range 2025-03-07 17:23:33 +09:00
Nobuyoshi Nakada
f56f3eaae5
[Bug #21030] Fix step for non-numeric range
When the end points of an inclusive range equal, `Range#step` should
yields the element once.
2025-01-13 02:07:05 +09:00
Nobuyoshi Nakada
e433e6515e
[DOC] Exclude 'Class' and 'Module' from RDoc's autolinking 2025-01-02 12:36:06 +09:00
Nobuyoshi Nakada
b4ec22fe6c
[DOC] Exclude 'Method' from RDoc's autolinking 2025-01-02 12:23:49 +09:00
Kouhei Yanagita
3422bfcab6 [Bug #20936] Fix #size for Range#reverse_each 2024-12-10 23:12:27 +09:00
John Bampton
3fc1495c30 Fix spelling 2024-10-09 07:14:44 +09:00
Peter Zhu
dc83de4928 [DOC] Add missing word 2024-10-03 09:28:13 -04:00
Peter Zhu
f56be4286f [DOC] Add pound sign before all <=> methods 2024-10-03 09:28:13 -04:00
Peter Zhu
e358104e6e [DOC] Fix formatting for ways to create Range objects 2024-09-18 15:18:07 -04:00
Nobuyoshi Nakada
24493779b7
[Bug #20725] Should not call compare on nil-endpoint
It means unbounded, always inclusive of other ranges.
2024-09-13 18:23:28 +09:00
zverok
d7b0f26963 Return back legacy Range#step behavior for symbol ranges 2024-09-09 17:46:13 +09:00
zverok
245ed2fc89 Range#step: restore legacy behavior for String ranges 2024-09-03 16:21:42 +09:00
Nobuyoshi Nakada
cd0d4ec059
Single letter ASCII operator is same as the corresponding ID 2024-08-19 19:50:08 +09:00
Victor Shepelev
d450f9d6a2
Make Range#step to consistently use + for iteration (#7444)
Make Range#step to consistently use + for iteration [Feature #18368]

Previously, non-numerics expected step to be integer,
and iterated with begin#succ, skipping over step value
steps. Since this commit, numeric and non-numeric iteration
behaves the same way, by using + operator.
2024-08-18 13:15:18 +03:00
Iskren
371790165f
[DOC] Fix and improve array slicing example in range.c
* [DOC] Fix typo in range.c

  In the example of the beginless range used for array slicing,
  '..' range literal was used while the '...' literal was expected

* [DOC] Add example for array slicing in range.c

  Add an example for the array slice with the beginless range
  using the '..' range literal

* [DOC] Add comments for array slicing in range.c

  Add comments to make crystal clear what the '..' and '...'
  range literals do when used for array slicing as beginless range
2024-07-17 11:42:29 +09:00
Kouhei Yanagita
9f6deaa688 [Misc #18984] Raise TypeError from Range#size if the range is not iterable 2024-04-10 07:28:07 -07:00
Nobuyoshi Nakada
b176315827
[Bug #20324] Uncomparable ranges are not overlapping 2024-03-04 21:02:08 +09:00
Yusuke Endoh
25d74b9527 Do not include a backtick in error messages and backtraces
[Feature #16495]
2024-02-15 18:42:31 +09:00
Peter Zhu
3ed5962755 Replace assert with RUBY_ASSERT in range.c
assert does not print the bug report, only the file and line number of
the assertion that failed. RUBY_ASSERT prints the full bug report, which
makes it much easier to debug.
2024-02-12 15:07:47 -05:00
Peter Zhu
9ec4eae0b1 [DOC] Fix invalid syntax in Range#eql? 2023-12-27 19:31:56 -05:00
Kouhei Yanagita
e8639098ed [Bug #19977] Fix (nil..nil) === x not to raise TypeError 2023-12-22 16:15:33 +09:00
Victor Shepelev
570d7b2c3e
[DOC] Adjust some new features wording/examples. (#9183)
* Reword Range#overlap? docs last paragraph.

* Docs: add explanation about Queue#freeze

* Docs: Add :rescue event docs for TracePoint

* Docs: Enhance Module#set_temporary_name documentation

* Docs: Slightly expand Process::Status deprecations

* Fix MatchData#named_captures rendering glitch

* Improve Dir.fchdir examples

* Adjust Refinement#target docs
2023-12-14 23:01:48 +02:00
Victor Shepelev
07734b51c6
[DOC] Small fixes for documentation rendering
Mostly just fixing RDoc's incorrect treatment of `+`
2023-12-09 13:54:33 +09:00
Kouhei Yanagita
1a16b6ffc2 Make Range#reverse_each raise TypeError if endless 2023-11-28 15:10:42 +09:00
Burdette Lamar
b92a92a3ff
[DOC] More on JSON extensions (#8898) 2023-11-16 08:28:07 -08:00
Tanaka Akira
c23b25f75f describe the assumption for Range#overlap?.
Range#overlap? assumes that there is no minimum value.
This assumption makes
+(...-Float::INFINITY).overlap?((...-Float::INFINITY))+ returns true
while +(...-Float::INFINITY)+ is empty.
2023-10-13 00:00:58 +09:00
Kouhei Yanagita
66fabefa03 Add Range#reverse_each implementation for performance 2023-10-12 17:34:49 +09:00
Kouhei Yanagita
6ae2996e29
Optimize Range#count by using range_size if possible 2023-10-05 00:19:55 +09:00
Nobuyoshi Nakada
50520cc193
[DOC] Missing comment markers 2023-09-27 16:18:05 +09:00
Kouhei Yanagita
4199e49cad Optimize Range#bsearch by reducing the number of Integer#+ calls 2023-09-26 17:31:10 +09:00
Kouhei Yanagita
7e350f5310 Optimize Range#bsearch for beginless/endless ranges within Fixnum 2023-09-21 10:30:58 +09:00
Jeremy Evans
25711683e8 Fix regression when testing inclusion in unbounded ranges
Caused by 04a92a6764bf678919cf4b68a27496a39d6b886a.  This treats
unbounded ranges of arbitrary objects the same as how unbounded
string ranges are treated:

  (..x)  === y  # (y <=> x) <= 0
  (...x) === y  # (y <=> x) <  0
  (x..)  === y  # (x <=> y) <= 0

Fixes [Bug #19864]
2023-09-16 16:02:31 +01:00
Nobuyoshi Nakada
e779465f19
Add comment markers in empty lines [ci skip] 2023-09-16 22:13:19 +09:00
Nobuyoshi Nakada
b4213a73b8 [Feature #19839] Fix Range#overlap? for empty ranges
Empty ranges do not overlap with any range.

Regarding benchmarks, PR#8242 is significantly faster in some cases,
but one of these two cases is a wrong result.

|                           |ActiveSupport| PR#8242|built-ruby|
|:--------------------------|------------:|-------:|---------:|
|(2..3).overlap?(1..1)      |       7.761M| 15.053M|   32.368M|
|                           |            -|   1.94x|     4.17x|
|(2..3).overlap?(2..4)      |      25.720M| 55.070M|   21.981M|
|                           |        1.17x|   2.51x|         -|
|(2..3).overlap?(4..5)      |       7.616M| 15.048M|   21.730M|
|                           |            -|   1.98x|     2.85x|
|(2..3).overlap?(2..1)      |      25.585M| 56.545M|   32.786M|
|                           |            -|   2.21x|     1.28x|
|(2..3).overlap?(0..1)      |       7.554M| 14.755M|   32.545M|
|                           |            -|   1.95x|     4.31x|
|(2..3).overlap?(...1)      |       6.681M|  5.843M|   32.255M|
|                           |        1.14x|       -|     5.52x|
|(2...3).overlap?(..2)      |       6.676M|  5.817M|   21.572M|
|                           |        1.15x|       -|     3.71x|
|(2...3).overlap?(3...)     |       7.392M| 14.755M|   31.805M|
|                           |            -|   2.00x|     4.30x|
|(2..3).overlap?('a'..'d')  |       3.675M|  3.482M|   17.009M|
|                           |        1.06x|       -|     4.89x|
2023-09-16 17:24:21 +09:00
Shouichi Kamiya
e9b503f1bb [Feature #19839] Add Range#overlap?
Add a method that returns true if two range overlap, otherwise false.

```
(0..10).overlap?(5..15) #=> true
(0..10).overlap?(20..30) #=> false
```
2023-09-16 14:57:19 +09:00
Kouhei Yanagita
7d08dbd015
Optimize Range#bsearch for beginless/endless ranges
On Range#bsearch for endless ranges, we try positions at `begin + 2**i` (i = 0, 1, 2, ...)
to find a point that satisfies a given condition.
Subsequently, we perform binary searching with the interval `[begin, begin + 2**n]`.

However, the interval `[begin + 2**(n-1), begin + 2**n]` is sufficient for binary search
because `begin + 2**(n-1)` does not satisfy the condition.

The same applies to beginless ranges.
2023-09-16 12:10:09 +09:00
Nobuyoshi Nakada
fb17c833f5 [Bug #19533] Fix infinite range inclusion with numeric value 2023-04-14 10:22:09 +09:00
Nobuyoshi Nakada
1a149aab77 Extract range type check functions 2023-04-14 10:22:09 +09:00
BurdetteLamar
3b239d2480 Remove (newly unneeded) remarks about aliases 2023-02-19 14:26:34 -08:00
Nobuyoshi Nakada
da4464b824
[Bug #19426] Fix endless Range#step with #succ method 2023-02-09 20:37:34 +09:00
Marco Costa
d2847009db
[DOC] Fix most of Range#cover? marked as verbatim 2022-12-23 11:12:16 +09:00
Daniel Colson
e69b91fae4 Introduce BOP_CMP for optimized comparison
Prior to this commit the `OPTIMIZED_CMP` macro relied on a method lookup
to determine whether `<=>` was overridden. The result of the lookup was
cached, but only for the duration of the specific method that
initialized the cmp_opt_data cache structure.

With this method lookup, `[x,y].max` is slower than doing `x > y ?
x : y` even though there's an optimized instruction for "new array max".
(John noticed somebody a proposed micro-optimization based on this fact
in https://github.com/mastodon/mastodon/pull/19903.)

```rb
a, b = 1, 2
Benchmark.ips do |bm|
  bm.report('conditional') { a > b ? a : b }
  bm.report('method') { [a, b].max }
  bm.compare!
end
```

Before:

```
Comparison:
         conditional: 22603733.2 i/s
              method: 19820412.7 i/s - 1.14x  (± 0.00) slower
```

This commit replaces the method lookup with a new CMP basic op, which
gives the examples above equivalent performance.

After:

```
Comparison:
              method: 24022466.5 i/s
         conditional: 23851094.2 i/s - same-ish: difference falls within
error
```

Relevant benchmarks show an improvement to Array#max and Array#min when
not using the optimized newarray_max instruction as well. They are
noticeably faster for small arrays with the relevant types, and the same
or maybe a touch faster on larger arrays.

```
$ make benchmark COMPARE_RUBY=<master@5958c305> ITEM=array_min
$ make benchmark COMPARE_RUBY=<master@5958c305> ITEM=array_max
```

The benchmarks added in this commit also look generally improved.

Co-authored-by: John Hawthorn <jhawthorn@github.com>
2022-12-06 12:37:23 -08:00
Jeremy Evans
04a92a6764 Raise TypeError for endless non-numeric range include?
Beginless ranges previously raised TypeError for this case,
except for string ranges, which had unexpected behavior:

  ('a'..'z').include?('ww') # false
  (..'z').include?('ww') # previously true, now TypeError

Use of include? with endless ranges could previously result
in an infinite loop.

This splits off a range_string_cover_internal function from
range_include_internal.

Fixes [Bug #18580]
2022-11-24 15:18:44 -08:00
S-H-GAMELINKS
1f4f6c9832 Using UNDEF_P macro 2022-11-16 18:58:33 +09:00
Yusuke Endoh
e026368061 Range#size returns nil for (.."a") and (nil..)
Fixes [Bug #18983]
2022-10-21 16:35:46 +09:00
Jean Boussier
bbe5ec7846 rb_int_range_last: properly handle non-exclusive range
[Bug #18994]
2022-09-04 11:16:11 +02:00
Takashi Kokubun
5b21e94beb Expand tabs [ci skip]
[Misc #18891]
2022-07-21 09:42:04 -07:00
Jeremy Evans
c5475f4269 Fix Range#cover? returning true for beginless ranges of different types
Previously `(2..).cover?("2"..)` was false, but
`(..2).cover?(.."2")` was true.  This changes it so both are false,
treating beginless ranges the same as endless ranges in regards to
type checks.

This also adds documentation to #cover? to describe behavior with
beginless and endless ranges, testing each documentation example,
which is how this bug was found.

Fixes [Bug #18155]
2022-06-06 09:59:22 -07:00