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
4e365156
Commit
4e365156
authored
Oct 17, 2017
by
Yukihiro "Matz" Matsumoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add `Comparable#clamp`; CRuby2.4
parent
061d804a
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
39 additions
and
0 deletions
+39
-0
mrbgems/default.gembox
mrbgems/default.gembox
+3
-0
mrbgems/mruby-compar-ext/mrbgem.rake
mrbgems/mruby-compar-ext/mrbgem.rake
+5
-0
mrbgems/mruby-compar-ext/mrblib/compar.rb
mrbgems/mruby-compar-ext/mrblib/compar.rb
+31
-0
No files found.
mrbgems/default.gembox
View file @
4e365156
...
...
@@ -14,6 +14,9 @@ MRuby::GemBox.new do |conf|
# Use standard Struct class
conf.gem :core => "mruby-struct"
# Use Comparable module extension
conf.gem :core => "mruby-compar-ext"
# Use Enumerable module extension
conf.gem :core => "mruby-enum-ext"
...
...
mrbgems/mruby-compar-ext/mrbgem.rake
0 → 100644
View file @
4e365156
MRuby
::
Gem
::
Specification
.
new
(
'mruby-compar-ext'
)
do
|
spec
|
spec
.
license
=
'MIT'
spec
.
author
=
'mruby developers'
spec
.
summary
=
'Enumerable module extension'
end
mrbgems/mruby-compar-ext/mrblib/compar.rb
0 → 100644
View file @
4e365156
module
Comparable
##
# Returns <i>min</i> if <i>obj</i> <code><=></code> <i>min</i> is less
# than zero, <i>max</i> if <i>obj</i> <code><=></code> <i>max</i> is
# greater than zero and <i>obj</i> otherwise.
#
# 12.clamp(0, 100) #=> 12
# 523.clamp(0, 100) #=> 100
# -3.123.clamp(0, 100) #=> 0
#
# 'd'.clamp('a', 'f') #=> 'd'
# 'z'.clamp('a', 'f') #=> 'f'
#
def
clamp
(
min
,
max
)
if
(
min
<=>
max
)
>
0
raise
ArgumentError
,
"min argument must be smaller than max argument"
end
c
=
self
<=>
min
if
c
==
0
return
self
elsif
c
<
0
return
min
end
c
=
self
<=>
max
if
c
>
0
return
max
else
return
self
end
end
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