Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
M
mruby
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
mruby
Commits
59a76d48
Unverified
Commit
59a76d48
authored
Jul 09, 2019
by
Yukihiro "Matz" Matsumoto
Committed by
GitHub
Jul 09, 2019
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #4560 from lopopolo/range-max-min-hang
Specialize Enumerable#max and Enumerable#min for Range
parents
9add4a66
3b77b5fd
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
167 additions
and
0 deletions
+167
-0
mrbgems/mruby-range-ext/mrblib/range.rb
mrbgems/mruby-range-ext/mrblib/range.rb
+39
-0
mrbgems/mruby-range-ext/test/range.rb
mrbgems/mruby-range-ext/test/range.rb
+102
-0
mrbgems/mruby-string-ext/test/range.rb
mrbgems/mruby-string-ext/test/range.rb
+26
-0
No files found.
mrbgems/mruby-range-ext/mrblib/range.rb
View file @
59a76d48
...
...
@@ -25,4 +25,43 @@ class Range
end
ary
end
def
max
(
&
block
)
val
=
self
.
first
last
=
self
.
last
return
super
if
block
# fast path for numerics
if
(
val
.
kind_of?
(
Fixnum
)
||
val
.
kind_of?
(
Float
))
&&
(
last
.
kind_of?
(
Fixnum
)
||
last
.
kind_of?
(
Float
))
raise
TypeError
if
exclude_end?
&&
!
last
.
kind_of?
(
Fixnum
)
return
nil
if
val
>
last
return
nil
if
val
==
last
&&
exclude_end?
max
=
last
max
-=
1
if
exclude_end?
return
max
end
# delegate to Enumerable
super
end
def
min
(
&
block
)
val
=
self
.
first
last
=
self
.
last
return
super
if
block
# fast path for numerics
if
(
val
.
kind_of?
(
Fixnum
)
||
val
.
kind_of?
(
Float
))
&&
(
last
.
kind_of?
(
Fixnum
)
||
last
.
kind_of?
(
Float
))
raise
TypeError
if
exclude_end?
&&
!
last
.
kind_of?
(
Fixnum
)
return
nil
if
val
>
last
return
nil
if
val
==
last
&&
exclude_end?
min
=
val
return
min
end
# delegate to Enumerable
super
end
end
mrbgems/mruby-range-ext/test/range.rb
View file @
59a76d48
...
...
@@ -30,3 +30,105 @@ assert('Range#size') do
assert_equal
Float
::
INFINITY
,
(
0
..
Float
::
INFINITY
).
size
assert_nil
(
'a'
..
'z'
).
size
end
assert
(
'Range#max'
)
do
# returns the maximum value in the range when called with no arguments
assert_equal
10
,
(
1
..
10
).
max
assert_equal
9
,
(
1
...
10
).
max
assert_equal
4294967295
,
(
0
...
2
**
32
).
max
# returns the maximum value in the Float range when called with no arguments
assert_equal
908.1111
,
(
303.20
..
908.1111
).
max
# raises TypeError when called on an exclusive range and a non Integer value
assert_raise
(
TypeError
)
{
(
303.20
...
908.1111
).
max
}
# returns nil when the endpoint is less than the start point
assert_equal
nil
,
(
100
..
10
).
max
# returns nil when the endpoint equals the start point and the range is exclusive
assert_equal
nil
,
(
5
...
5
).
max
# returns the endpoint when the endpoint equals the start point and the range is inclusive
assert_equal
5
,
(
5
..
5
).
max
# returns nil when the endpoint is less than the start point in a Float range
assert_equal
nil
,
(
3003.20
..
908.1111
).
max
end
assert
(
'Range#max given a block'
)
do
# passes each pair of values in the range to the block
acc
=
[]
(
1
..
10
).
max
do
|
a
,
b
|
acc
<<
a
acc
<<
b
a
end
(
1
..
10
).
each
do
|
value
|
assert_true
acc
.
include?
(
value
)
end
# passes each pair of elements to the block in reversed order
acc
=
[]
(
1
..
5
).
max
do
|
a
,
b
|
acc
<<
[
a
,
b
]
a
end
assert_equal
[[
2
,
1
],
[
3
,
2
],
[
4
,
3
],
[
5
,
4
]],
acc
# returns the element the block determines to be the maximum
assert_equal
1
,
((
1
..
3
).
max
{
|
_a
,
_b
|
-
3
})
# returns nil when the endpoint is less than the start point
assert_equal
nil
,
((
100
..
10
).
max
{
|
x
,
y
|
x
<=>
y
})
assert_equal
nil
,
((
5
...
5
).
max
{
|
x
,
y
|
x
<=>
y
})
end
assert
(
'Range#min'
)
do
# returns the minimum value in the range when called with no arguments
assert_equal
1
,
(
1
..
10
).
min
assert_equal
1
,
(
1
...
10
).
min
# returns the minimum value in the Float range when called with no arguments
assert_equal
303.20
,
(
303.20
..
908.1111
).
min
# returns nil when the start point is greater than the endpoint
assert_equal
nil
,
(
100
..
10
).
min
# returns nil when the endpoint equals the start point and the range is exclusive
assert_equal
nil
,
(
5
...
5
).
max
# returns the endpoint when the endpoint equals the start point and the range is inclusive
assert_equal
5
,
(
5
..
5
).
max
# returns nil when the start point is greater than the endpoint in a Float range
assert_equal
nil
,
(
3003.20
..
908.1111
).
max
end
assert
(
'Range#min given a block'
)
do
# passes each pair of values in the range to the block
acc
=
[]
(
1
..
10
).
min
do
|
a
,
b
|
acc
<<
a
acc
<<
b
a
end
(
1
..
10
).
each
do
|
value
|
assert_true
acc
.
include?
(
value
)
end
# passes each pair of elements to the block in reversed order
acc
=
[]
(
1
..
5
).
min
do
|
a
,
b
|
acc
<<
[
a
,
b
]
a
end
assert_equal
[[
2
,
1
],
[
3
,
1
],
[
4
,
1
],
[
5
,
1
]],
acc
# returns the element the block determines to be the minimum
assert_equal
3
,
((
1
..
3
).
min
{
|
_a
,
_b
|
-
3
})
# returns nil when the start point is greater than the endpoint
assert_equal
nil
,
((
100
..
10
).
min
{
|
x
,
y
|
x
<=>
y
})
assert_equal
nil
,
((
5
...
5
).
min
{
|
x
,
y
|
x
<=>
y
})
end
mrbgems/mruby-string-ext/test/range.rb
0 → 100644
View file @
59a76d48
assert
(
'Range#max'
)
do
# returns the maximum value in the range when called with no arguments
assert_equal
'l'
,
(
'f'
..
'l'
).
max
assert_equal
'e'
,
(
'a'
...
'f'
).
max
# returns nil when the endpoint is less than the start point
assert_equal
nil
,
(
'z'
..
'l'
).
max
end
assert
(
'Range#max given a block'
)
do
# returns nil when the endpoint is less than the start point
assert_equal
nil
,
((
'z'
..
'l'
).
max
{
|
x
,
y
|
x
<=>
y
})
end
assert
(
'Range#min'
)
do
# returns the minimum value in the range when called with no arguments
assert_equal
'f'
,
(
'f'
..
'l'
).
min
# returns nil when the start point is greater than the endpoint
assert_equal
nil
,
(
'z'
..
'l'
).
min
end
assert
(
'Range#min given a block'
)
do
# returns nil when the start point is greater than the endpoint
assert_equal
nil
,
((
'z'
..
'l'
).
min
{
|
x
,
y
|
x
<=>
y
})
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment