ruby-changes:17120
From: shyouhei <ko1@a...>
Date: Fri, 27 Aug 2010 16:33:26 +0900 (JST)
Subject: [ruby-changes:17120] Ruby:r29120 (trunk): * string.c (rb_str_prepend): new method by Sora Harakami
shyouhei 2010-08-27 16:33:19 +0900 (Fri, 27 Aug 2010) New Revision: 29120 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=29120 Log: * string.c (rb_str_prepend): new method by Sora Harakami [Feature #3765] Modified files: trunk/ChangeLog trunk/string.c trunk/test/ruby/test_string.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 29119) +++ ChangeLog (revision 29120) @@ -1,3 +1,8 @@ +Fri Aug 27 16:20:01 2010 URABE Shyouhei <shyouhei@r...> + + * string.c (rb_str_prepend): new method by Sora Harakami + [Feature #3765] + Fri Aug 27 15:24:20 2010 NAKAMURA Usaku <usa@r...> * math.c (math_atan2): you should know that M_PI is not the feature Index: string.c =================================================================== --- string.c (revision 29119) +++ string.c (revision 29120) @@ -2056,6 +2056,26 @@ } } +/* + * call-seq: + * str.prepend(other_str) -> str + * + * Prepend---Prepend the given string to <i>str</i>. + * + * a = "world" + * a.prepend("hello ") #=> "hello world" + * a #=> "hello world" + */ + +static VALUE +rb_str_prepend(VALUE str, VALUE str2) +{ + StringValue(str2); + StringValue(str); + rb_str_update(str, 0L, 0L, str2); + return str; +} + st_index_t rb_memhash(const void *ptr, long len) { @@ -7525,6 +7545,7 @@ rb_define_method(rb_cString, "reverse!", rb_str_reverse_bang, 0); rb_define_method(rb_cString, "concat", rb_str_concat, 1); rb_define_method(rb_cString, "<<", rb_str_concat, 1); + rb_define_method(rb_cString, "prepend", rb_str_prepend, 1); rb_define_method(rb_cString, "crypt", rb_str_crypt, 1); rb_define_method(rb_cString, "intern", rb_str_intern, 0); rb_define_method(rb_cString, "to_sym", rb_str_intern, 0); Index: test/ruby/test_string.rb =================================================================== --- test/ruby/test_string.rb (revision 29119) +++ test/ruby/test_string.rb (revision 29120) @@ -1882,4 +1882,20 @@ assert_equal('"\\u3042\\u3044\\u3046"', "\u3042\u3044\u3046".encode(e).inspect) end end + + def test_prepend + assert_equal(S("hello world!"), "world!".prepend("hello ")) + + foo = Object.new + def foo.to_str + "b" + end + assert_equal(S("ba"), "a".prepend(foo)) + + a = S("world") + b = S("hello ") + a.prepend(b) + assert_equal(S("hello world"), a) + assert_equal(S("hello "), b) + end end -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/