Created
August 10, 2010 06:54
-
-
Save sorah/516817 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git string.c string.c | |
index 0aa2e6c..6c9be57 100644 | |
--- string.c | |
+++ string.c | |
@@ -2055,6 +2055,43 @@ rb_str_concat(VALUE str1, VALUE str2) | |
} | |
} | |
+/* | |
+ * 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; | |
+} | |
+ | |
+/* | |
+ * call-seq: | |
+ * str >> other_str -> other_str | |
+ * | |
+ * Prepend---Prepend self string to <i>other_str</i>. | |
+ * | |
+ * a = "world" | |
+ * "hello " >> a #=> "hello world" | |
+ * a #=> "hello world" | |
+ */ | |
+ | |
+static VALUE | |
+rb_str_prepend_to_another(VALUE str1, VALUE str2) | |
+{ | |
+ return rb_str_prepend(str2, str1); | |
+} | |
+ | |
st_index_t | |
rb_memhash(const void *ptr, long len) | |
{ | |
@@ -7524,6 +7561,8 @@ Init_String(void) | |
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, ">>", rb_str_prepend_to_another, 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); | |
diff --git test/ruby/test_string.rb test/ruby/test_string.rb | |
index 02271ce..da2477f 100644 | |
--- test/ruby/test_string.rb | |
+++ test/ruby/test_string.rb | |
@@ -1882,4 +1882,36 @@ class TestString < Test::Unit::TestCase | |
assert_equal('"\\u3042\\u3044\\u3046"', "\u3042\u3044\u3046".encode(e).inspect) | |
end | |
end | |
+ | |
+ def test_prepend_to_another | |
+ assert_equal(S("hello world"), "hello " >> "world") | |
+ | |
+ foo = Object.new | |
+ def foo.to_str | |
+ "b" | |
+ end | |
+ assert_equal(S("ab"), "a" >> foo) | |
+ | |
+ a = S("world!") | |
+ b = S("hello ") | |
+ b >> a | |
+ assert_equal(S("hello world!"), a) | |
+ assert_equal(S("hello "), b) | |
+ 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>> "hello " >> "world" | |
=> "hello world" | |
>> a = "world" | |
=> "world" | |
>> "hello " >> a | |
=> "hello world" | |
>> a | |
=> "hello world" | |
>> a.prepend("...") | |
=> "...hello world" | |
>> a | |
=> "...hello world" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment