Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add comparison methods to Hash #3010

Merged
merged 1 commit into from Nov 14, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
96 changes: 96 additions & 0 deletions mrblib/hash.rb
Expand Up @@ -346,6 +346,102 @@ def __update(h)
h.each_key{|k| self[k] = h[k]}
self
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

##
Expand Down
72 changes: 72 additions & 0 deletions test/t/hash.rb
Expand Up @@ -351,3 +351,75 @@
h.rehash
assert_equal("b", h[[:b]])
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