Skip to content

Commit ba88208

Browse files
author
Nicolas Laurent
committed
[GR-20117] Implemented Comparable#clamp.
PullRequest: truffleruby/1192
2 parents 610b16e + fa63373 commit ba88208

File tree

4 files changed

+12
-7
lines changed

4 files changed

+12
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ Compatibility:
5555
* Joni has been updated from 2.1.25 to 2.1.30.
5656
* Implemented `Method#<<` and `Method#>>` (#1821).
5757
* The `.bundle` file extension is now used for C extensions on macOS (#1819, #1837).
58+
* Implemented `Comparable#clamp` (#1517).
5859

5960
Performance:
6061

spec/tags/core/comparable/clamp_tags.txt

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/main/ruby/truffleruby/core/comparable.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,14 @@ def between?(min, max)
7979
true
8080
end
8181

82+
def clamp(min, max)
83+
comp = min <=> max
84+
raise ArgumentError, 'min argument must be smaller than max argument' if comp == nil or comp > 0
85+
return min if self < min
86+
return max if self > max
87+
self
88+
end
89+
8290
# A version of MRI's rb_cmpint (sort of)
8391
def self.compare_int(int)
8492
return int if int.kind_of? Integer

src/main/ruby/truffleruby/core/complex.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@
3636

3737
class Complex < Numeric
3838

39-
undef_method :%, :<, :<=, :<=>, :>, :>=, :between?, :div, :divmod, :floor, :ceil, :modulo,
40-
:remainder, :round, :step, :truncate, :i, :negative?, :positive?
39+
undef_method :%, :<, :<=, :<=>, :>, :>=, :between?, :clamp, # comparable
40+
:div, :divmod, :floor, :ceil, :modulo, :remainder,
41+
:round, :step, :truncate, :i, :negative?, :positive?
4142

4243
def self.convert(real, imag = undefined)
4344
if nil.equal?(real) || nil.equal?(imag)

0 commit comments

Comments
 (0)