Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
M
mruby
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
mruby
Commits
85b5af61
Commit
85b5af61
authored
Nov 15, 2015
by
Yukihiro "Matz" Matsumoto
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'nobu-feature/hash-cmp'
parents
4e63ea96
fa86026a
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
168 additions
and
0 deletions
+168
-0
mrbgems/mruby-hash-ext/mrblib/hash.rb
mrbgems/mruby-hash-ext/mrblib/hash.rb
+96
-0
mrbgems/mruby-hash-ext/test/hash.rb
mrbgems/mruby-hash-ext/test/hash.rb
+72
-0
No files found.
mrbgems/mruby-hash-ext/mrblib/hash.rb
View file @
85b5af61
...
...
@@ -250,4 +250,100 @@ class Hash
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
mrbgems/mruby-hash-ext/test/hash.rb
View file @
85b5af61
...
...
@@ -158,3 +158,75 @@ assert("Hash#to_h") do
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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment