summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-08-23 01:15:04 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-08-23 01:15:04 +0000
commitc2bf7e6f7d2fbe0b50da59aaa1374222f233aa71 (patch)
tree6c53001c7239f072d3b68112c74226f8bcd86183
parent2e2f7df725aa604aed492fff9a927d3c5de7f538 (diff)
downloadruby-c2bf7e6f7d2fbe0b50da59aaa1374222f233aa71.tar.gz
string.c: rb_fs_setter
* string.c (rb_fs_setter): check and convert $; value at assignment. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@55990 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog5
-rw-r--r--string.c18
-rw-r--r--test/ruby/test_string.rb9
3 files changed, 25 insertions, 7 deletions
diff --git a/ChangeLog b/ChangeLog
index 9389de5270..ef06f9b7f2 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+Tue Aug 23 10:15:01 2016 Nobuyoshi Nakada <nobu@ruby-lang.org>
+
+ * string.c (rb_fs_setter): check and convert $; value at
+ assignment.
+
Tue Aug 23 02:09:57 2016 Nobuyoshi Nakada <nobu@ruby-lang.org>
* string.c (rb_str_split_m): show $; name in error message when it
diff --git a/string.c b/string.c
index f74cbaafd1..9bb739c154 100644
--- a/string.c
+++ b/string.c
@@ -7126,7 +7126,7 @@ rb_str_split_m(int argc, VALUE *argv, VALUE str)
split_type = regexp;
}
else if (rb_enc_asciicompat(enc2) == 1) {
- if (RSTRING_LEN(spat) == 1 && RSTRING_PTR(spat)[0] == ' '){
+ if (RSTRING_LEN(spat) == 1 && RSTRING_PTR(spat)[0] == ' ') {
split_type = awk;
}
}
@@ -8861,6 +8861,18 @@ rb_str_setter(VALUE val, ID id, VALUE *var)
*var = val;
}
+static void
+rb_fs_setter(VALUE val, ID id, VALUE *var)
+{
+ val = rb_fs_check(val);
+ if (!val) {
+ rb_raise(rb_eTypeError,
+ "value of %"PRIsVALUE" must be String or Regexp",
+ rb_id2str(id));
+ }
+ *var = val;
+}
+
/*
* call-seq:
@@ -9879,8 +9891,8 @@ Init_String(void)
rb_define_method(rb_cString, "ascii_only?", rb_str_is_ascii_only_p, 0);
rb_fs = Qnil;
- rb_define_variable("$;", &rb_fs);
- rb_define_variable("$-F", &rb_fs);
+ rb_define_hooked_variable("$;", &rb_fs, 0, rb_fs_setter);
+ rb_define_hooked_variable("$-F", &rb_fs, 0, rb_fs_setter);
rb_cSymbol = rb_define_class("Symbol", rb_cObject);
rb_include_module(rb_cSymbol, rb_mComparable);
diff --git a/test/ruby/test_string.rb b/test/ruby/test_string.rb
index e5aa2251a7..4970af15e4 100644
--- a/test/ruby/test_string.rb
+++ b/test/ruby/test_string.rb
@@ -1389,13 +1389,14 @@ CODE
assert_equal([], "".split(//, 1))
assert_equal("[2, 3]", [1,2,3].slice!(1,10000).inspect, "moved from btest/knownbug")
+ ensure
+ $; = fs
+ end
- $; = []
+ def test_fs
assert_raise_with_message(TypeError, /\$;/) {
- "".split
+ $; = []
}
- ensure
- $; = fs
end
def test_split_encoding