Skip to content

Commit

Permalink
move Hash comparison methods to mruby-hash-ext gem
Browse files Browse the repository at this point in the history
  • Loading branch information
matz committed Nov 14, 2015
1 parent 1fc9074 commit fa86026
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 168 deletions.
96 changes: 96 additions & 0 deletions mrbgems/mruby-hash-ext/mrblib/hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -250,4 +250,100 @@ def key(val)
def to_h
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
72 changes: 72 additions & 0 deletions mrbgems/mruby-hash-ext/test/hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,75 @@
assert_equal Hash, h.to_h.class
assert_equal h, h.to_h
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
96 changes: 0 additions & 96 deletions mrblib/hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -346,102 +346,6 @@ 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: 0 additions & 72 deletions test/t/hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -351,75 +351,3 @@
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

0 comments on commit fa86026

Please sign in to comment.