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
d3c7e117
Commit
d3c7e117
authored
Oct 23, 2014
by
Yukihiro "Matz" Matsumoto
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #2617 from suzukaze/add-kernel.Hash
Add Kernel#Hash
parents
5616208e
9e053664
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
42 additions
and
0 deletions
+42
-0
mrbgems/mruby-kernel-ext/src/kernel.c
mrbgems/mruby-kernel-ext/src/kernel.c
+35
-0
mrbgems/mruby-kernel-ext/test/kernel.rb
mrbgems/mruby-kernel-ext/test/kernel.rb
+7
-0
No files found.
mrbgems/mruby-kernel-ext/src/kernel.c
View file @
d3c7e117
#include "mruby.h"
#include "mruby/error.h"
#include "mruby/array.h"
#include "mruby/hash.h"
/*
* call-seq:
...
...
@@ -75,6 +76,39 @@ mrb_f_array(mrb_state *mrb, mrb_value self)
return
tmp
;
}
/*
* call-seq:
* Hash(arg) -> hash
*
* Converts <i>arg</i> to a <code>Hash</code> by calling
* <i>arg</i><code>.to_hash</code>. Returns an empty <code>Hash</code> when
* <i>arg</i> is <tt>nil</tt> or <tt>[]</tt>.
*
* Hash([]) #=> {}
* Hash(nil) #=> {}
* Hash(key: :value) #=> {:key => :value}
* Hash([1, 2, 3]) #=> TypeError
*/
static
mrb_value
mrb_f_hash
(
mrb_state
*
mrb
,
mrb_value
self
)
{
mrb_value
arg
,
tmp
;
mrb_get_args
(
mrb
,
"o"
,
&
arg
);
if
(
mrb_nil_p
(
arg
))
{
return
mrb_hash_new
(
mrb
);
}
tmp
=
mrb_check_convert_type
(
mrb
,
arg
,
MRB_TT_HASH
,
"Hash"
,
"to_hash"
);
if
(
mrb_nil_p
(
tmp
))
{
if
(
mrb_array_p
(
arg
)
&&
RARRAY_LEN
(
arg
)
==
0
)
{
return
mrb_hash_new
(
mrb
);
}
mrb_raisef
(
mrb
,
E_TYPE_ERROR
,
"can't convert %S into Hash"
,
mrb_str_new_cstr
(
mrb
,
mrb_obj_classname
(
mrb
,
arg
)));
}
return
tmp
;
}
void
mrb_mruby_kernel_ext_gem_init
(
mrb_state
*
mrb
)
{
...
...
@@ -84,6 +118,7 @@ mrb_mruby_kernel_ext_gem_init(mrb_state *mrb)
mrb_define_method
(
mrb
,
krn
,
"__method__"
,
mrb_f_method
,
MRB_ARGS_NONE
());
mrb_define_module_function
(
mrb
,
krn
,
"String"
,
mrb_f_string
,
MRB_ARGS_REQ
(
1
));
mrb_define_module_function
(
mrb
,
krn
,
"Array"
,
mrb_f_array
,
MRB_ARGS_REQ
(
1
));
mrb_define_module_function
(
mrb
,
krn
,
"Hash"
,
mrb_f_hash
,
MRB_ARGS_REQ
(
1
));
}
void
...
...
mrbgems/mruby-kernel-ext/test/kernel.rb
View file @
d3c7e117
...
...
@@ -29,3 +29,10 @@ assert('Kernel#Array') do
assert_equal
([
1
,
2
,
3
,
4
,
5
],
Kernel
.
Array
(
1
..
5
))
assert_equal
([[
:a
,
1
],
[
:b
,
2
],
[
:c
,
3
]],
Kernel
.
Array
({
a
:
1
,
b
:
2
,
c
:
3
}))
end
assert
(
'Kernel#Hash'
)
do
assert_equal
({},
Hash
([]))
assert_equal
({},
Hash
(
nil
))
assert_equal
({
:key
=>
:value
},
Hash
(
key: :value
))
assert_raise
(
TypeError
)
{
Hash
([
1
,
2
,
3
])
}
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