Skip to content

Commit 14f9efa

Browse files
authored
Merge pull request #1 from henrikac/gt-lt-operator
added <, >, <=, >= operators
2 parents e1f13e4 + 86b953a commit 14f9efa

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

spec/csuuid_spec.cr

+30
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,34 @@ describe CSUUID do
9999
(uuid2 <=> uuid1).should eq 1
100100
(uuid1 <=> uuid1).should eq 0
101101
end
102+
103+
it "can compare CSUUIDs via <" do
104+
uuid1 = CSUUID.new
105+
uuid2 = CSUUID.new
106+
(uuid1 < uuid2).should be_true
107+
(uuid2 < uuid1).should be_false
108+
end
109+
110+
it "can compare CSUUIDs via >" do
111+
uuid1 = CSUUID.new
112+
uuid2 = CSUUID.new
113+
(uuid1 > uuid2).should be_false
114+
(uuid2 > uuid1).should be_true
115+
end
116+
117+
it "can compare CSUUIDs via <=" do
118+
uuid1 = CSUUID.new
119+
uuid2 = CSUUID.new
120+
(uuid1 <= uuid2).should be_true
121+
(uuid2 <= uuid1).should be_false
122+
(uuid1 <= uuid1).should be_true
123+
end
124+
125+
it "can compare CSUUIDs via >=" do
126+
uuid1 = CSUUID.new
127+
uuid2 = CSUUID.new
128+
(uuid1 >= uuid2).should be_false
129+
(uuid2 >= uuid1).should be_true
130+
(uuid1 >= uuid1).should be_true
131+
end
102132
end

src/csuuid.cr

+20
Original file line numberDiff line numberDiff line change
@@ -180,4 +180,24 @@ struct CSUUID
180180

181181
to_s <=> val.to_s
182182
end
183+
184+
# Returns `true` if `self` is less than *other*.
185+
def <(other : CSUUID) : Bool
186+
(self <=> other) == -1
187+
end
188+
189+
# Returns `true` if `self` is greater than *other*.
190+
def >(other : CSUUID) : Bool
191+
(self <=> other) == 1
192+
end
193+
194+
# Returns `true` if `self` is less than or equal to *other*.
195+
def <=(other : CSUUID) : Bool
196+
self == other || self < other
197+
end
198+
199+
# Returns `true` if `self` is greater than or equal to *other*.
200+
def >=(other : CSUUID) : Bool
201+
self == other || self > other
202+
end
183203
end

0 commit comments

Comments
 (0)