Commit bf5f83ff authored by Nobuyoshi Nakada's avatar Nobuyoshi Nakada

Add comparison methods to Hash

parent 9c108002
...@@ -346,6 +346,102 @@ class Hash ...@@ -346,6 +346,102 @@ class Hash
h.each_key{|k| self[k] = h[k]} h.each_key{|k| self[k] = h[k]}
self self
end end
##
# call-seq:
# hash < other -> true or false
#
# Returns <code>true</code> if <i>hash</i> is subset of
# <i>other</i>.
#
# h1 = {a:1, b:2}
# h2 = {a:1, b:2, c:3}
# h1 < h2 #=> true
# h2 < h1 #=> false
# h1 < h1 #=> false
#
def <(hash)
begin
hash = hash.to_hash
rescue NoMethodError
raise TypeError, "can't convert #{hash.class} to Hash"
end
size < hash.size and all? {|key, val|
hash.key?(key) and hash[key] == val
}
end
##
# call-seq:
# hash <= other -> true or false
#
# Returns <code>true</code> if <i>hash</i> is subset of
# <i>other</i> or equals to <i>other</i>.
#
# h1 = {a:1, b:2}
# h2 = {a:1, b:2, c:3}
# h1 <= h2 #=> true
# h2 <= h1 #=> false
# h1 <= h1 #=> true
#
def <=(hash)
begin
hash = hash.to_hash
rescue NoMethodError
raise TypeError, "can't convert #{hash.class} to Hash"
end
size <= hash.size and all? {|key, val|
hash.key?(key) and hash[key] == val
}
end
##
# call-seq:
# hash > other -> true or false
#
# Returns <code>true</code> if <i>other</i> is subset of
# <i>hash</i>.
#
# h1 = {a:1, b:2}
# h2 = {a:1, b:2, c:3}
# h1 > h2 #=> false
# h2 > h1 #=> true
# h1 > h1 #=> false
#
def >(hash)
begin
hash = hash.to_hash
rescue NoMethodError
raise TypeError, "can't convert #{hash.class} to Hash"
end
size > hash.size and hash.all? {|key, val|
key?(key) and self[key] == val
}
end
##
# call-seq:
# hash >= other -> true or false
#
# Returns <code>true</code> if <i>other</i> is subset of
# <i>hash</i> or equals to <i>hash</i>.
#
# h1 = {a:1, b:2}
# h2 = {a:1, b:2, c:3}
# h1 >= h2 #=> false
# h2 >= h1 #=> true
# h1 >= h1 #=> true
#
def >=(hash)
begin
hash = hash.to_hash
rescue NoMethodError
raise TypeError, "can't convert #{hash.class} to Hash"
end
size >= hash.size and hash.all? {|key, val|
key?(key) and self[key] == val
}
end
end end
## ##
......
...@@ -351,3 +351,75 @@ assert('Hash#rehash') do ...@@ -351,3 +351,75 @@ assert('Hash#rehash') do
h.rehash h.rehash
assert_equal("b", h[[:b]]) assert_equal("b", h[[:b]])
end end
assert('Hash#<') do
h1 = {a:1, b:2}
h2 = {a:1, b:2, c:3}
assert_false(h1 < h1)
assert_true(h1 < h2)
assert_false(h2 < h1)
assert_false(h2 < h2)
h1 = {a:1}
h2 = {a:2}
assert_false(h1 < h1)
assert_false(h1 < h2)
assert_false(h2 < h1)
assert_false(h2 < h2)
end
assert('Hash#<=') do
h1 = {a:1, b:2}
h2 = {a:1, b:2, c:3}
assert_true(h1 <= h1)
assert_true(h1 <= h2)
assert_false(h2 <= h1)
assert_true(h2 <= h2)
h1 = {a:1}
h2 = {a:2}
assert_true(h1 <= h1)
assert_false(h1 <= h2)
assert_false(h2 <= h1)
assert_true(h2 <= h2)
end
assert('Hash#>=') do
h1 = {a:1, b:2}
h2 = {a:1, b:2, c:3}
assert_true(h1 >= h1)
assert_false(h1 >= h2)
assert_true(h2 >= h1)
assert_true(h2 >= h2)
h1 = {a:1}
h2 = {a:2}
assert_true(h1 >= h1)
assert_false(h1 >= h2)
assert_false(h2 >= h1)
assert_true(h2 >= h2)
end
assert('Hash#>') do
h1 = {a:1, b:2}
h2 = {a:1, b:2, c:3}
assert_false(h1 > h1)
assert_false(h1 > h2)
assert_true(h2 > h1)
assert_false(h2 > h2)
h1 = {a:1}
h2 = {a:2}
assert_false(h1 > h1)
assert_false(h1 > h2)
assert_false(h2 > h1)
assert_false(h2 > h2)
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment