This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
## Comparable#at_least
|
||||
|
||||
require 'facets/comparable/at_least'
|
||||
|
||||
Limits a number to at least a given amount.
|
||||
|
||||
3.at_least(4).assert == 4
|
||||
4.at_least(4).assert == 4
|
||||
5.at_least(4).assert == 5
|
||||
|
||||
This is similar to `#clip`.
|
||||
@@ -0,0 +1,11 @@
|
||||
## Comparable#at_most
|
||||
|
||||
require 'facets/comparable/at_most'
|
||||
|
||||
Limits a number to at most a given amount.
|
||||
|
||||
3.at_most(4).assert == 3
|
||||
4.at_most(4).assert == 4
|
||||
5.at_most(4).assert == 4
|
||||
|
||||
This is similar to `#cap`.
|
||||
@@ -0,0 +1,13 @@
|
||||
## Comparable#bound
|
||||
|
||||
require 'facets/comparable/bound'
|
||||
|
||||
The `#bound` method is an alias for `#clip`. It is the original name
|
||||
of the method before the `clip`/`cap` duo was decided upon.
|
||||
|
||||
3.bound(4).assert == 4
|
||||
|
||||
4.bound(3,5).assert == 4
|
||||
|
||||
'd'.bound('c','e').assert == 'd'
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
## Comparable#cap
|
||||
|
||||
require 'facets/comparable/cap'
|
||||
|
||||
Put a cap on what a number can be.
|
||||
|
||||
3.cap(4).assert == 3
|
||||
4.cap(4).assert == 4
|
||||
5.cap(4).assert == 4
|
||||
|
||||
This is very similar to `#at_most`.
|
||||
@@ -0,0 +1,29 @@
|
||||
## Comparable#clip
|
||||
|
||||
require 'facets/comparable/clip'
|
||||
|
||||
Given a single argument `#clip` behaves like `#at_least`.
|
||||
|
||||
3.clip(4).assert == 4
|
||||
4.clip(4).assert == 4
|
||||
5.clip(4).assert == 5
|
||||
|
||||
With two arguments `#clip` puts both a lower and an upper limit
|
||||
on the return value.
|
||||
|
||||
4.clip(3,5).assert == 4
|
||||
3.clip(3,5).assert == 3
|
||||
5.clip(3,5).assert == 5
|
||||
2.clip(3,5).assert == 3
|
||||
6.clip(3,5).assert == 5
|
||||
|
||||
This almost works on characters (using ASCII order).
|
||||
|
||||
'd'.clip('c','e').assert == 'd'
|
||||
'c'.clip('c','e').assert == 'c'
|
||||
'e'.clip('c','e').assert == 'e'
|
||||
'b'.clip('c','e').assert == 'c'
|
||||
'f'.clip('c','e').assert == 'e'
|
||||
|
||||
This method is aliases as `bound`.
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
## Comparable#cmp
|
||||
|
||||
require 'facets/comparable/cmp'
|
||||
|
||||
On integers
|
||||
|
||||
3.cmp(4).assert == -1
|
||||
3.cmp(3).assert == 0
|
||||
3.cmp(2).assert == 1
|
||||
|
||||
On strings
|
||||
|
||||
"abc".cmp("abc").assert == 0
|
||||
"abc".cmp("abcd").assert == -1
|
||||
"abcd".cmp("abc").assert == 1
|
||||
"abc".cmp("bcd").assert == -1
|
||||
"bcd".cmp("abc").assert == 1
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
## Comparable.[]
|
||||
|
||||
require 'facets/comparable/op_get'
|
||||
|
||||
Allows `Comparable` to be included as a *parmetric mixin*. This makes it easy to define
|
||||
a class as comparable on a limited set of attributes.
|
||||
|
||||
c = Class.new do
|
||||
include Comparable[:a,:b]
|
||||
|
||||
attr_accessor :a, :b
|
||||
|
||||
def initialize(a,b)
|
||||
@a=a; @b=b
|
||||
end
|
||||
end
|
||||
|
||||
a = [c.new(10,20),c.new(10,30)]
|
||||
a.sort.assert == a
|
||||
|
||||
a = [c.new(10,30),c.new(10,20)]
|
||||
a.sort.assert == a.reverse
|
||||
|
||||
a = [c.new(10,10),c.new(20,10)]
|
||||
a.sort.assert == a
|
||||
|
||||
a = [c.new(20,10),c.new(10,10)]
|
||||
a.sort.assert == a.reverse
|
||||
|
||||
a = [c.new(10,30),c.new(20,10)]
|
||||
a.sort.assert == a
|
||||
|
||||
Reference in New Issue
Block a user