Commit cec28739 authored by Yukihiro "Matz" Matsumoto's avatar Yukihiro "Matz" Matsumoto

Merge pull request #968 from kouki-o-iij/pr-mrbgem-numeric-ext

add mrbgems/ext/mruby-numeric, and method: Integer#chr
parents c881a6a2 d3ff90f5
......@@ -26,6 +26,9 @@ MRuby::Build.new do |conf|
# Use extensional String class
conf.gem 'mrbgems/mruby-string-ext'
# Use extensional Numeric class
conf.gem 'mrbgems/mruby-numeric-ext'
# Generate binaries
# conf.bins = %w(mrbc mruby mirb)
......
MRuby::Gem::Specification.new('mruby-numeric-ext') do |spec|
spec.license = 'MIT'
spec.authors = 'mruby developers'
end
#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)
{
}
##
# Numeric(Ext) Test
assert('Integer#chr') do
assert_equal(65.chr, "A")
assert_equal(0x42.chr, "B")
# multibyte encoding (not support yet)
assert_raise(RangeError) { 12345.chr }
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment