Permalink
Browse files

String#concat: Try to convert when not string

  • Loading branch information...
ksss committed May 21, 2017
1 parent 78add72 commit 98f9c33fe07d098a1e06e0ea9a59324cf071a2d3
Showing with 18 additions and 7 deletions.
  1. +8 −1 mrbgems/mruby-string-ext/src/string.c
  2. +10 −6 mrbgems/mruby-string-ext/test/string.rb
@@ -134,6 +134,8 @@ mrb_str_swapcase(mrb_state *mrb, mrb_value self)
return str;
}
static mrb_value mrb_fixnum_chr(mrb_state *mrb, mrb_value num);
/*
* call-seq:
* str << integer -> str
@@ -153,7 +155,12 @@ static mrb_value
mrb_str_concat2(mrb_state *mrb, mrb_value self)
{
mrb_value str;
mrb_get_args(mrb, "S", &str);
mrb_get_args(mrb, "o", &str);
if (mrb_fixnum_p(str))
str = mrb_fixnum_chr(mrb, str);
else
str = mrb_string_type(mrb, str);
mrb_str_concat(mrb, self, str);
return self;
}
@@ -122,12 +122,16 @@ def to_i
end
assert('String#concat') do
s = "Hello "
s.concat "World!"
t = "Hello "
t << "World!"
assert_equal "Hello World!", t
assert_equal "Hello World!", s
assert_equal "Hello World!", "Hello " << "World" << 33
assert_equal "Hello World!", "Hello ".concat("World").concat(33)
o = Object.new
def o.to_str
"to_str"
end
assert_equal "hi to_str", "hi " << o
assert_raise(TypeError) { "".concat(Object.new) }
end
assert('String#casecmp') do

0 comments on commit 98f9c33

Please sign in to comment.