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
8e456308
Commit
8e456308
authored
May 08, 2013
by
Yukihiro "Matz" Matsumoto
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'master' of github.com:mruby/mruby
parents
595b1990
c80103e8
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
126 additions
and
94 deletions
+126
-94
mrbgems/mruby-hash-ext/src/hash-ext.c
mrbgems/mruby-hash-ext/src/hash-ext.c
+62
-0
mrbgems/mruby-hash-ext/test/hash.rb
mrbgems/mruby-hash-ext/test/hash.rb
+6
-0
mrblib/hash.rb
mrblib/hash.rb
+45
-0
src/hash.c
src/hash.c
+0
-92
test/t/hash.rb
test/t/hash.rb
+13
-2
No files found.
mrbgems/mruby-hash-ext/src/hash-ext.c
0 → 100644
View file @
8e456308
/*
** hash.c - Hash class
**
** See Copyright Notice in mruby.h
*/
#include "mruby.h"
#include "mruby/array.h"
#include "mruby/class.h"
#include "mruby/hash.h"
#include "mruby/khash.h"
#include "mruby/string.h"
#include "mruby/variable.h"
/*
* call-seq:
* hsh.values_at(key, ...) -> array
*
* Return an array containing the values associated with the given keys.
* Also see <code>Hash.select</code>.
*
* h = { "cat" => "feline", "dog" => "canine", "cow" => "bovine" }
* h.values_at("cow", "cat") #=> ["bovine", "feline"]
*/
mrb_value
mrb_hash_values_at
(
mrb_state
*
mrb
,
int
argc
,
mrb_value
*
argv
,
mrb_value
hash
)
{
mrb_value
result
=
mrb_ary_new_capa
(
mrb
,
argc
);
long
i
;
for
(
i
=
0
;
i
<
argc
;
i
++
)
{
mrb_ary_push
(
mrb
,
result
,
mrb_hash_get
(
mrb
,
hash
,
argv
[
i
]));
}
return
result
;
}
static
mrb_value
hash_values_at
(
mrb_state
*
mrb
,
mrb_value
hash
)
{
mrb_value
*
argv
;
int
argc
;
mrb_get_args
(
mrb
,
"*"
,
&
argv
,
&
argc
);
return
mrb_hash_values_at
(
mrb
,
argc
,
argv
,
hash
);
}
void
mrb_mruby_hash_ext_gem_init
(
mrb_state
*
mrb
)
{
struct
RClass
*
h
;
h
=
mrb
->
hash_class
;
mrb_define_method
(
mrb
,
h
,
"values_at"
,
hash_values_at
,
MRB_ARGS_ANY
());
}
void
mrb_mruby_hash_ext_gem_final
(
mrb_state
*
mrb
)
{
}
mrbgems/mruby-hash-ext/test/hash.rb
View file @
8e456308
...
...
@@ -18,3 +18,9 @@ assert('Hash#merge!') do
'xyz_key'
=>
'xyz_value'
}
end
assert
(
'Hash#values_at'
)
do
h
=
{
"cat"
=>
"feline"
,
"dog"
=>
"canine"
,
"cow"
=>
"bovine"
}
result
=
h
.
values_at
(
"cow"
,
"cat"
)
result
==
[
"bovine"
,
"feline"
]
end
mrblib/hash.rb
View file @
8e456308
...
...
@@ -24,6 +24,23 @@ class Hash
# Calls the given block for each element of +self+
# and pass the key and value of each element.
#
# call-seq:
# hsh.each {| key, value | block } -> hsh
# hsh.each_pair {| key, value | block } -> hsh
# hsh.each -> an_enumerator
# hsh.each_pair -> an_enumerator
#
#
# If no block is given, an enumerator is returned instead.
#
# h = { "a" => 100, "b" => 200 }
# h.each {|key, value| puts "#{key} is #{value}" }
#
# <em>produces:</em>
#
# a is 100
# b is 200
#
# ISO 15.2.13.4.9
def
each
(
&
block
)
self
.
keys
.
each
{
|
k
|
block
.
call
([
k
,
self
[
k
]])}
...
...
@@ -34,6 +51,20 @@ class Hash
# Calls the given block for each element of +self+
# and pass the key of each element.
#
# call-seq:
# hsh.each_key {| key | block } -> hsh
# hsh.each_key -> an_enumerator
#
# If no block is given, an enumerator is returned instead.
#
# h = { "a" => 100, "b" => 200 }
# h.each_key {|key| puts key }
#
# <em>produces:</em>
#
# a
# b
#
# ISO 15.2.13.4.10
def
each_key
(
&
block
)
self
.
keys
.
each
{
|
k
|
block
.
call
(
k
)}
...
...
@@ -44,6 +75,20 @@ class Hash
# Calls the given block for each element of +self+
# and pass the value of each element.
#
# call-seq:
# hsh.each_value {| value | block } -> hsh
# hsh.each_value -> an_enumerator
#
# If no block is given, an enumerator is returned instead.
#
# h = { "a" => 100, "b" => 200 }
# h.each_value {|value| puts value }
#
# <em>produces:</em>
#
# 100
# 200
#
# ISO 15.2.13.4.11
def
each_value
(
&
block
)
self
.
keys
.
each
{
|
k
|
block
.
call
(
self
[
k
])}
...
...
src/hash.c
View file @
8e456308
...
...
@@ -324,12 +324,6 @@ mrb_hash_aget(mrb_state *mrb, mrb_value self)
return
mrb_hash_get
(
mrb
,
self
,
key
);
}
mrb_value
mrb_hash_lookup
(
mrb_state
*
mrb
,
mrb_value
hash
,
mrb_value
key
)
{
return
mrb_hash_get
(
mrb
,
hash
,
key
);
}
/*
* call-seq:
* hsh.fetch(key [, default] ) -> obj
...
...
@@ -610,29 +604,6 @@ mrb_hash_shift(mrb_state *mrb, mrb_value hash)
*
*/
/*
* call-seq:
* hsh.values_at(key, ...) -> array
*
* Return an array containing the values associated with the given keys.
* Also see <code>Hash.select</code>.
*
* h = { "cat" => "feline", "dog" => "canine", "cow" => "bovine" }
* h.values_at("cow", "cat") #=> ["bovine", "feline"]
*/
mrb_value
mrb_hash_values_at
(
mrb_state
*
mrb
,
int
argc
,
mrb_value
*
argv
,
mrb_value
hash
)
{
mrb_value
result
=
mrb_ary_new_capa
(
mrb
,
argc
);
long
i
;
for
(
i
=
0
;
i
<
argc
;
i
++
)
{
mrb_ary_push
(
mrb
,
result
,
mrb_hash_get
(
mrb
,
hash
,
argv
[
i
]));
}
return
result
;
}
/*
* call-seq:
* hsh.select {|key, value| block} -> a_hash
...
...
@@ -813,69 +784,6 @@ mrb_hash_empty_p(mrb_state *mrb, mrb_value self)
return
mrb_bool_value
(
empty_p
);
}
/* 15.2.13.4.11 */
/*
* call-seq:
* hsh.each_value {| value | block } -> hsh
* hsh.each_value -> an_enumerator
*
* Calls <i>block</i> once for each key in <i>hsh</i>, passing the
* value as a parameter.
*
* If no block is given, an enumerator is returned instead.
*
* h = { "a" => 100, "b" => 200 }
* h.each_value {|value| puts value }
*
* <em>produces:</em>
*
* 100
* 200
*/
/* 15.2.13.4.10 */
/*
* call-seq:
* hsh.each_key {| key | block } -> hsh
* hsh.each_key -> an_enumerator
*
* Calls <i>block</i> once for each key in <i>hsh</i>, passing the key
* as a parameter.
*
* If no block is given, an enumerator is returned instead.
*
* h = { "a" => 100, "b" => 200 }
* h.each_key {|key| puts key }
*
* <em>produces:</em>
*
* a
* b
*/
/* 15.2.13.4.9 */
/*
* call-seq:
* hsh.each {| key, value | block } -> hsh
* hsh.each_pair {| key, value | block } -> hsh
* hsh.each -> an_enumerator
* hsh.each_pair -> an_enumerator
*
* Calls <i>block</i> once for each key in <i>hsh</i>, passing the key-value
* pair as parameters.
*
* If no block is given, an enumerator is returned instead.
*
* h = { "a" => 100, "b" => 200 }
* h.each {|key, value| puts "#{key} is #{value}" }
*
* <em>produces:</em>
*
* a is 100
* b is 200
*
*/
static
mrb_value
inspect_hash
(
mrb_state
*
mrb
,
mrb_value
hash
,
int
recur
)
{
...
...
test/t/hash.rb
View file @
8e456308
...
...
@@ -52,9 +52,12 @@ end
assert
(
'Hash#default_proc'
,
'15.2.13.4.7'
)
do
a
=
Hash
.
new
b
=
Hash
.
new
{
|
s
,
k
|
s
[
k
]
=
k
}
b
=
Hash
.
new
{
|
s
,
k
|
s
[
k
]
=
k
+
k
}
c
=
b
[
2
]
d
=
b
[
'cat'
]
a
.
default_proc
==
nil
and
b
.
default_proc
.
class
==
Proc
a
.
default_proc
==
nil
and
b
.
default_proc
.
class
==
Proc
and
c
=
4
and
d
=
'catcat'
end
assert
(
'Hash#delete'
,
'15.2.13.4.8'
)
do
...
...
@@ -278,3 +281,11 @@ assert('Hash#select!') do
h
==
{
:two
=>
2
,
:four
=>
4
}
end
# Not ISO specified
assert
(
'Hash#inspect'
)
do
h
=
{
"c"
=>
300
,
"a"
=>
100
,
"d"
=>
400
,
"c"
=>
300
}
ret
=
h
.
to_s
ret
=
"{
\"
c
\"
=>300,
\"
a
\"
=>100,
\"
d
\"
=>400}"
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