diff options
| author | Kouki Ooyatsu <[email protected]> | 2013-03-08 10:02:31 +0900 |
|---|---|---|
| committer | Kouki Ooyatsu <[email protected]> | 2013-03-08 10:05:41 +0900 |
| commit | 6c066302e2200f91847befd56b4ddbb555f41925 (patch) | |
| tree | e618b058791b4e1b868c48f00f575f0019be8823 /mrbgems/mruby-numeric-ext | |
| parent | aa73a1068f2fd5cbff7e4623ab2036d360933ff9 (diff) | |
| download | mruby-6c066302e2200f91847befd56b4ddbb555f41925.tar.gz mruby-6c066302e2200f91847befd56b4ddbb555f41925.zip | |
add mrbgems/ext/mruby-numeric, and method: Integer#chr
Diffstat (limited to 'mrbgems/mruby-numeric-ext')
| -rw-r--r-- | mrbgems/mruby-numeric-ext/mrbgem.rake | 4 | ||||
| -rw-r--r-- | mrbgems/mruby-numeric-ext/src/numeric_ext.c | 30 | ||||
| -rw-r--r-- | mrbgems/mruby-numeric-ext/test/numeric.rb | 7 |
3 files changed, 41 insertions, 0 deletions
diff --git a/mrbgems/mruby-numeric-ext/mrbgem.rake b/mrbgems/mruby-numeric-ext/mrbgem.rake new file mode 100644 index 000000000..69c4fde4c --- /dev/null +++ b/mrbgems/mruby-numeric-ext/mrbgem.rake @@ -0,0 +1,4 @@ +MRuby::Gem::Specification.new('mruby-numeric-ext') do |spec| + spec.license = 'MIT' + spec.authors = 'mruby developers' +end diff --git a/mrbgems/mruby-numeric-ext/src/numeric_ext.c b/mrbgems/mruby-numeric-ext/src/numeric_ext.c new file mode 100644 index 000000000..b2c0e7986 --- /dev/null +++ b/mrbgems/mruby-numeric-ext/src/numeric_ext.c @@ -0,0 +1,30 @@ +#include "mruby.h" +#include "mruby/numeric.h" + +static mrb_value +mrb_int_chr(mrb_state *mrb, mrb_value x) +{ + mrb_int chr; + char c; + + chr = mrb_fixnum(x); + if (chr >= (1 << CHAR_BIT)) { + mrb_raisef(mrb, E_RANGE_ERROR, "%ld out of char range", chr); + } + c = (char)chr; + + return mrb_str_new(mrb, &c, 1); +} + +void +mrb_mruby_numeric_ext_gem_init(mrb_state* mrb) +{ + struct RClass *i = mrb_class_get(mrb, "Integer"); + + mrb_define_method(mrb, i, "chr", mrb_int_chr, ARGS_NONE()); +} + +void +mrb_mruby_numeric_ext_gem_final(mrb_state* mrb) +{ +} diff --git a/mrbgems/mruby-numeric-ext/test/numeric.rb b/mrbgems/mruby-numeric-ext/test/numeric.rb new file mode 100644 index 000000000..cafa0f1aa --- /dev/null +++ b/mrbgems/mruby-numeric-ext/test/numeric.rb @@ -0,0 +1,7 @@ +## +# Numeric(Ext) Test + +assert('Integer#chr') do + assert_equal(65.chr, "A") + assert_equal(0x42.chr, "B") +end |
