diff options
author | knu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2009-03-20 09:43:24 +0000 |
---|---|---|
committer | knu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2009-03-20 09:43:24 +0000 |
commit | f1f12658ee64d443e17cd7e39e12556123580817 (patch) | |
tree | 36a6efd9d63db4a12438ec4af5330cfb04aa9168 | |
parent | 885b6c6ecf10eb14f27d4fc78e383fcf71f01bac (diff) |
* array.c (Array#try_convert): New method backported from 1.9.
* hash.c (Hash#try_convert): New method backported from 1.9.
* io.c (IO#try_convert): New method backported from 1.9.
* re.c (Regexp#try_convert): New method backported from 1.9.
* string.c (String#try_convert): New method backported from 1.9.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@23018 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r-- | ChangeLog | 12 | ||||
-rw-r--r-- | NEWS | 8 | ||||
-rw-r--r-- | array.c | 28 | ||||
-rw-r--r-- | hash.c | 23 | ||||
-rw-r--r-- | io.c | 20 | ||||
-rw-r--r-- | re.c | 38 | ||||
-rw-r--r-- | string.c | 19 |
7 files changed, 145 insertions, 3 deletions
@@ -1,3 +1,15 @@ +Fri Mar 20 18:41:53 2009 Akinori MUSHA <[email protected]> + + * array.c (Array#try_convert): New method backported from 1.9. + + * hash.c (Hash#try_convert): New method backported from 1.9. + + * io.c (IO#try_convert): New method backported from 1.9. + + * re.c (Regexp#try_convert): New method backported from 1.9. + + * string.c (String#try_convert): New method backported from 1.9. + Fri Mar 20 18:01:12 2009 Tanaka Akira <[email protected]> * ext/openssl/ossl_digest.c (GetDigestPtr): use StringValueCStr @@ -22,6 +22,14 @@ with all sufficient information, see the ChangeLog file. * builtin classes + * Array#try_convert() + * Hash#try_convert() + * IO#try_convert() + * Regexp#try_convert() + * String#try_convert() + + New methods. + * Array#sample New method with which replaces #choice. @@ -268,6 +268,33 @@ rb_check_array_type(ary) return rb_check_convert_type(ary, T_ARRAY, "Array", "to_ary"); } +/* + * call-seq: + * Array.try_convert(obj) -> array or nil + * + * Try to convert <i>obj</i> into an array, using to_ary method. + * Returns converted array or nil if <i>obj</i> cannot be converted + * for any reason. This method is to check if an argument is an + * array. + * + * Array.try_convert([1]) # => [1] + * Array.try_convert("1") # => nil + * + * if tmp = Array.try_convert(arg) + * # the argument is an array + * elsif tmp = String.try_convert(arg) + * # the argument is a string + * end + * + */ + +static VALUE +rb_ary_s_try_convert(dummy, ary) + VALUE dummy, ary; +{ + return rb_check_array_type(ary); +} + static VALUE rb_ary_replace _((VALUE, VALUE)); /* @@ -3836,6 +3863,7 @@ Init_Array() rb_define_alloc_func(rb_cArray, ary_alloc); rb_define_singleton_method(rb_cArray, "[]", rb_ary_s_create, -1); + rb_define_singleton_method(rb_cArray, "try_convert", rb_ary_s_try_convert, 1); rb_define_method(rb_cArray, "initialize", rb_ary_initialize, -1); rb_define_method(rb_cArray, "initialize_copy", rb_ary_replace, 1); @@ -21,6 +21,8 @@ #include <crt_externs.h> #endif +static VALUE rb_hash_s_try_convert _((VALUE, VALUE)); + #define HASH_DELETED FL_USER1 #define HASH_PROC_DEFAULT FL_USER2 @@ -343,7 +345,7 @@ rb_hash_s_create(argc, argv, klass) int i; if (argc == 1) { - tmp = rb_check_convert_type(argv[0], T_HASH, "Hash", "to_hash"); + tmp = rb_hash_s_try_convert(Qnil, argv[0]); if (!NIL_P(tmp)) { hash = hash_alloc0(klass); RHASH(hash)->tbl = st_copy(RHASH(tmp)->tbl); @@ -390,6 +392,24 @@ to_hash(hash) return rb_convert_type(hash, T_HASH, "Hash", "to_hash"); } +/* + * call-seq: + * Hash.try_convert(obj) -> hash or nil + * + * Try to convert <i>obj</i> into a hash, using to_hash method. + * Returns converted hash or nil if <i>obj</i> cannot be converted + * for any reason. + * + * Hash.try_convert({1=>2}) # => {1=>2} + * Hash.try_convert("1=>2") # => nil + */ +static VALUE +rb_hash_s_try_convert(dummy, hash) + VALUE dummy, hash; +{ + return rb_check_convert_type(hash, T_HASH, "Hash", "to_hash"); +} + static int rb_hash_rehash_i(key, value, tbl) VALUE key, value; @@ -2685,6 +2705,7 @@ Init_Hash() rb_define_alloc_func(rb_cHash, hash_alloc); rb_define_singleton_method(rb_cHash, "[]", rb_hash_s_create, -1); + rb_define_singleton_method(rb_cHash, "try_convert", rb_hash_s_try_convert, 1); rb_define_method(rb_cHash,"initialize", rb_hash_initialize, -1); rb_define_method(rb_cHash,"initialize_copy", rb_hash_replace, 1); rb_define_method(rb_cHash,"rehash", rb_hash_rehash, 0); @@ -3625,6 +3625,25 @@ rb_io_check_io(io) return rb_check_convert_type(io, T_FILE, "IO", "to_io"); } +/* + * call-seq: + * IO.try_convert(obj) -> io or nil + * + * Try to convert <i>obj</i> into an IO, using to_io method. + * Returns converted IO or nil if <i>obj</i> cannot be converted + * for any reason. + * + * IO.try_convert(STDOUT) # => STDOUT + * IO.try_convert("STDOUT") # => nil + * + */ +static VALUE +rb_io_s_try_convert(dummy, io) + VALUE dummy, io; +{ + return rb_io_check_io(io); +} + static const char* rb_io_mode_string(fptr) rb_io_t *fptr; @@ -5930,6 +5949,7 @@ Init_IO() rb_define_singleton_method(rb_cIO, "read", rb_io_s_read, -1); rb_define_singleton_method(rb_cIO, "select", rb_f_select, -1); rb_define_singleton_method(rb_cIO, "pipe", rb_io_s_pipe, 0); + rb_define_singleton_method(rb_cIO, "try_convert", rb_io_s_try_convert, 1); rb_define_method(rb_cIO, "initialize", rb_io_initialize, -1); @@ -1973,6 +1973,39 @@ rb_reg_options(re) return options; } +VALUE +rb_check_regexp_type(re) + VALUE re; +{ + return rb_check_convert_type(re, T_REGEXP, "Regexp", "to_regexp"); +} + +static VALUE rb_reg_s_try_convert _((VALUE, VALUE)); + +/* + * call-seq: + * Regexp.try_convert(obj) -> re or nil + * + * Try to convert <i>obj</i> into a Regexp, using to_regexp method. + * Returns converted regexp or nil if <i>obj</i> cannot be converted + * for any reason. + * + * Regexp.try_convert(/re/) #=> /re/ + * Regexp.try_convert("re") #=> nil + * + * o = Object.new + * Regexp.try_convert(o) #=> nil + * def o.to_regexp() /foo/ end + * Regexp.try_convert(o) #=> /foo/ + * + */ +static VALUE +rb_reg_s_try_convert(dummy, re) + VALUE dummy, re; +{ + return rb_check_regexp_type(re); +} + static VALUE rb_reg_s_union(self, args0) VALUE self; @@ -1986,7 +2019,7 @@ rb_reg_s_union(self, args0) } else if (argc == 1) { VALUE v; - v = rb_check_convert_type(rb_ary_entry(args0, 0), T_REGEXP, "Regexp", "to_regexp"); + v = rb_check_regexp_type(rb_ary_entry(args0, 0)); if (!NIL_P(v)) return v; else { @@ -2004,7 +2037,7 @@ rb_reg_s_union(self, args0) volatile VALUE v; if (0 < i) rb_str_buf_cat2(source, "|"); - v = rb_check_convert_type(rb_ary_entry(args0, i), T_REGEXP, "Regexp", "to_regexp"); + v = rb_check_regexp_type(rb_ary_entry(args0, i)); if (!NIL_P(v)) { if (FL_TEST(v, KCODE_FIXED)) { if (kcode == -1) { @@ -2399,6 +2432,7 @@ Init_Regexp() rb_define_singleton_method(rb_cRegexp, "escape", rb_reg_s_quote, -1); rb_define_singleton_method(rb_cRegexp, "union", rb_reg_s_union_m, -2); rb_define_singleton_method(rb_cRegexp, "last_match", rb_reg_s_last_match, -1); + rb_define_singleton_method(rb_cRegexp, "try_convert", rb_reg_s_try_convert, 1); rb_define_method(rb_cRegexp, "initialize", rb_reg_initialize_m, -1); rb_define_method(rb_cRegexp, "initialize_copy", rb_reg_init_copy, 1); @@ -614,6 +614,24 @@ rb_check_string_type(str) return str; } +/* + * call-seq: + * String.try_convert(obj) -> string or nil + * + * Try to convert <i>obj</i> into a String, using to_str method. + * Returns converted regexp or nil if <i>obj</i> cannot be converted + * for any reason. + * + * String.try_convert("str") # => str + * String.try_convert(/re/) # => nil + */ +static VALUE +rb_str_s_try_convert(dummy, str) + VALUE dummy, str; +{ + return rb_check_string_type(str); +} + VALUE rb_str_substr(str, beg, len) VALUE str; @@ -5003,6 +5021,7 @@ Init_String() rb_include_module(rb_cString, rb_mComparable); rb_include_module(rb_cString, rb_mEnumerable); rb_define_alloc_func(rb_cString, rb_str_s_alloc); + rb_define_singleton_method(rb_cString, "try_convert", rb_str_s_try_convert, 1); rb_define_method(rb_cString, "initialize", rb_str_init, -1); rb_define_method(rb_cString, "initialize_copy", rb_str_replace, 1); rb_define_method(rb_cString, "<=>", rb_str_cmp_m, 1); |