summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--include/mruby/boxing_word.h2
-rw-r--r--include/mruby/value.h6
-rw-r--r--mrblib/string.rb11
-rw-r--r--test/t/string.rb7
4 files changed, 21 insertions, 5 deletions
diff --git a/include/mruby/boxing_word.h b/include/mruby/boxing_word.h
index 3b7167b28..59fdb730d 100644
--- a/include/mruby/boxing_word.h
+++ b/include/mruby/boxing_word.h
@@ -116,6 +116,8 @@ mrb_type(mrb_value o)
#define mrb_fixnum_p(o) ((o).value.i_flag == MRB_FIXNUM_FLAG)
#define mrb_undef_p(o) ((o).w == MRB_Qundef)
#define mrb_nil_p(o) ((o).w == MRB_Qnil)
+#define mrb_false_p(o) ((o).w == MRB_Qfalse)
+#define mrb_true_p(o) ((o).w == MRB_Qtrue)
#define BOXWORD_SET_VALUE(o, ttt, attr, v) do { \
switch (ttt) {\
diff --git a/include/mruby/value.h b/include/mruby/value.h
index 1ed20858f..8530950a9 100644
--- a/include/mruby/value.h
+++ b/include/mruby/value.h
@@ -157,6 +157,12 @@ typedef void mrb_value;
#ifndef mrb_nil_p
#define mrb_nil_p(o) (mrb_type(o) == MRB_TT_FALSE && !mrb_fixnum(o))
#endif
+#ifndef mrb_false_p
+#define mrb_false_p(o) (mrb_type(o) == MRB_TT_FALSE && !!mrb_fixnum(o))
+#endif
+#ifndef mrb_true_p
+#define mrb_true_p(o) (mrb_type(o) == MRB_TT_TRUE)
+#endif
#ifndef mrb_bool
#define mrb_bool(o) (mrb_type(o) != MRB_TT_FALSE)
#endif
diff --git a/mrblib/string.rb b/mrblib/string.rb
index 9ad8e8e73..e9eb2be1d 100644
--- a/mrblib/string.rb
+++ b/mrblib/string.rb
@@ -197,12 +197,12 @@ class String
def []=(*args)
anum = args.size
if anum == 2
- pos, value = args
+ pos, value = args[0], args[1].__to_str
case pos
when String
posnum = self.index(pos)
if posnum
- b = self[0, posnum.to_i]
+ b = self[0, posnum]
a = self[(posnum + pos.length)..-1]
self.replace([b, value, a].join(''))
else
@@ -217,17 +217,18 @@ class String
end
return self[head, tail-head]=value
else
+ pos = pos.__to_int
pos += self.length if pos < 0
if pos < 0 || pos > self.length
raise IndexError, "index #{args[0]} out of string"
end
- b = self[0, pos.to_i]
+ b = self[0, pos]
a = self[pos + 1..-1]
self.replace([b, value, a].join(''))
end
return value
elsif anum == 3
- pos, len, value = args
+ pos, len, value = args[0].__to_int, args[1].__to_int, args[2].__to_str
pos += self.length if pos < 0
if pos < 0 || pos > self.length
raise IndexError, "index #{args[0]} out of string"
@@ -235,7 +236,7 @@ class String
if len < 0
raise IndexError, "negative length #{len}"
end
- b = self[0, pos.to_i]
+ b = self[0, pos]
a = self[pos + len..-1]
self.replace([b, value, a].join(''))
return value
diff --git a/test/t/string.rb b/test/t/string.rb
index cf3702cbe..404cf03e1 100644
--- a/test/t/string.rb
+++ b/test/t/string.rb
@@ -161,6 +161,9 @@ assert('String#[]=') do
assert_equal 'aXc', e
end
+ assert_raise(TypeError) { 'a'[0] = 1 }
+ assert_raise(TypeError) { 'a'[:a] = '1' }
+
# length of args is 2
a1 = 'abc'
assert_raise(IndexError) do
@@ -197,6 +200,10 @@ assert('String#[]=') do
assert_raise(IndexError) do
b3['XX'] = 'Y'
end
+
+ assert_raise(TypeError) { 'a'[:a, 0] = '1' }
+ assert_raise(TypeError) { 'a'[0, :a] = '1' }
+ assert_raise(TypeError) { 'a'[0, 1] = 1 }
end
assert('String#capitalize', '15.2.10.5.7') do