Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

add mrbgems/ext/mruby-numeric, and method: Integer#chr #968

Merged
merged 3 commits into from

2 participants

@mattn

I prefer to add failing test to utf-8. mruby doesn't support any encodings. I know the future won't come. But it will be poissible to support utf-8. So I guess, you've better to add failing test for utf-8. For example: 12354.chr raise error.

Kouki Ooyatsu added some commits
@ghost

@mattn thank you. I just added test case.

@matz matz merged commit cec2873 into mruby:master
@ghost ghost deleted the unknown repository branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 8, 2013
  1. add Integer#chr test case: multibyte (raise RangeError)

    Kouki Ooyatsu authored
  2. fix comment

    Kouki Ooyatsu authored
This page is out of date. Refresh to see the latest.
View
3  build_config.rb
@@ -26,6 +26,9 @@
# 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)
View
4 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
View
30 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)
+{
+}
View
10 mrbgems/mruby-numeric-ext/test/numeric.rb
@@ -0,0 +1,10 @@
+##
+# 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
Something went wrong with that request. Please try again.