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
73593f7d
Commit
73593f7d
authored
Oct 29, 2014
by
Yukihiro "Matz" Matsumoto
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #2625 from suzukaze/add-kernel.Integer
Add kernel#Integer
parents
aec12c30
28c80462
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
61 additions
and
2 deletions
+61
-2
include/mruby.h
include/mruby.h
+1
-0
mrbgems/mruby-kernel-ext/src/kernel.c
mrbgems/mruby-kernel-ext/src/kernel.c
+46
-0
mrbgems/mruby-kernel-ext/test/kernel.rb
mrbgems/mruby-kernel-ext/test/kernel.rb
+8
-0
src/object.c
src/object.c
+6
-2
No files found.
include/mruby.h
View file @
73593f7d
...
...
@@ -316,6 +316,7 @@ MRB_API mrb_sym mrb_obj_to_sym(mrb_state *mrb, mrb_value name);
MRB_API
mrb_bool
mrb_obj_eq
(
mrb_state
*
,
mrb_value
,
mrb_value
);
MRB_API
mrb_bool
mrb_obj_equal
(
mrb_state
*
,
mrb_value
,
mrb_value
);
MRB_API
mrb_bool
mrb_equal
(
mrb_state
*
mrb
,
mrb_value
obj1
,
mrb_value
obj2
);
MRB_API
mrb_value
mrb_convert_to_integer
(
mrb_state
*
mrb
,
mrb_value
val
,
int
base
);
MRB_API
mrb_value
mrb_Integer
(
mrb_state
*
mrb
,
mrb_value
val
);
MRB_API
mrb_value
mrb_Float
(
mrb_state
*
mrb
,
mrb_value
val
);
MRB_API
mrb_value
mrb_inspect
(
mrb_state
*
mrb
,
mrb_value
obj
);
...
...
mrbgems/mruby-kernel-ext/src/kernel.c
View file @
73593f7d
...
...
@@ -23,6 +23,51 @@ mrb_f_method(mrb_state *mrb, mrb_value self)
return
mrb_nil_value
();
}
/*
* call-seq:
* Integer(arg,base=0) -> integer
*
* Converts <i>arg</i> to a <code>Fixnum</code>.
* Numeric types are converted directly (with floating point numbers
* being truncated). <i>base</i> (0, or between 2 and 36) is a base for
* integer string representation. If <i>arg</i> is a <code>String</code>,
* when <i>base</i> is omitted or equals to zero, radix indicators
* (<code>0</code>, <code>0b</code>, and <code>0x</code>) are honored.
* In any case, strings should be strictly conformed to numeric
* representation. This behavior is different from that of
* <code>String#to_i</code>. Non string values will be converted using
* <code>to_int</code>, and <code>to_i</code>. Passing <code>nil</code>
* raises a TypeError.
*
* Integer(123.999) #=> 123
* Integer("0x1a") #=> 26
* Integer(Time.new) #=> 1204973019
* Integer("0930", 10) #=> 930
* Integer("111", 2) #=> 7
* Integer(nil) #=> TypeError
*/
static
mrb_value
mrb_f_integer
(
mrb_state
*
mrb
,
mrb_value
self
)
{
mrb_value
*
argv
;
mrb_value
arg
;
int
argc
;
int
base
=
0
;
mrb_get_args
(
mrb
,
"*"
,
&
argv
,
&
argc
);
switch
(
argc
)
{
case
2
:
base
=
mrb_fixnum
(
argv
[
1
]);
case
1
:
arg
=
argv
[
0
];
break
;
default:
mrb_raisef
(
mrb
,
E_ARGUMENT_ERROR
,
"wrong number of arguments (%S for 1..2)"
,
mrb_fixnum_value
(
argc
));
}
return
mrb_convert_to_integer
(
mrb
,
arg
,
base
);
}
/*
* call-seq:
* Float(arg) -> float
...
...
@@ -137,6 +182,7 @@ mrb_mruby_kernel_ext_gem_init(mrb_state *mrb)
mrb_define_module_function
(
mrb
,
krn
,
"fail"
,
mrb_f_raise
,
MRB_ARGS_OPT
(
2
));
mrb_define_method
(
mrb
,
krn
,
"__method__"
,
mrb_f_method
,
MRB_ARGS_NONE
());
mrb_define_module_function
(
mrb
,
krn
,
"Integer"
,
mrb_f_integer
,
MRB_ARGS_ANY
());
mrb_define_module_function
(
mrb
,
krn
,
"Float"
,
mrb_f_float
,
MRB_ARGS_REQ
(
1
));
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
));
...
...
mrbgems/mruby-kernel-ext/test/kernel.rb
View file @
73593f7d
...
...
@@ -17,6 +17,14 @@ assert('Kernel#__method__') do
assert_equal
(
:m2
,
c
.
new
.
m2
)
end
assert
(
'Kernel#Integer'
)
do
assert_equal
(
123
,
Integer
(
123.999
))
assert_equal
(
26
,
Integer
(
"0x1a"
))
assert_equal
(
930
,
Integer
(
"0930"
,
10
))
assert_equal
(
7
,
Integer
(
"111"
,
2
))
assert_raise
(
TypeError
)
{
Integer
(
nil
)
}
end
assert
(
'Kernel#Float'
)
do
assert_equal
(
1.0
,
Float
(
1
))
assert_equal
(
123.456
,
Float
(
123.456
))
...
...
src/object.c
View file @
73593f7d
...
...
@@ -516,7 +516,7 @@ mrb_to_int(mrb_state *mrb, mrb_value val)
return
mrb_to_integer
(
mrb
,
val
,
"to_int"
);
}
static
mrb_value
MRB_API
mrb_value
mrb_convert_to_integer
(
mrb_state
*
mrb
,
mrb_value
val
,
int
base
)
{
mrb_value
tmp
;
...
...
@@ -537,13 +537,17 @@ mrb_convert_to_integer(mrb_state *mrb, mrb_value val, int base)
if
(
base
!=
0
)
goto
arg_error
;
return
val
;
case
MRB_TT_STRING
:
string_conv:
return
mrb_str_to_inum
(
mrb
,
val
,
base
,
TRUE
);
default:
break
;
}
if
(
base
!=
0
)
{
tmp
=
mrb_check_string_type
(
mrb
,
val
);
if
(
!
mrb_nil_p
(
tmp
))
{
return
mrb_str_to_inum
(
mrb
,
val
,
base
,
TRUE
)
;
goto
string_conv
;
}
arg_error:
mrb_raise
(
mrb
,
E_ARGUMENT_ERROR
,
"base specified for non string value"
);
...
...
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