diff options
author | matz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2007-08-24 17:47:09 +0000 |
---|---|---|
committer | matz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2007-08-24 17:47:09 +0000 |
commit | 5e1c401ff5f72bb5e2bc6eb844b65977881a71c9 (patch) | |
tree | 71fa92ca4779e1d2cccbd5937ca834e4c6981533 | |
parent | 487a7da22df69432d471a80982c446ec2e7db649 (diff) | |
download | ruby-5e1c401ff5f72bb5e2bc6eb844b65977881a71c9.tar.gz |
* array.c (rb_ary_s_try_convert): a new class method to convert
object or nil if it's not target-type. this mechanism is used
to convert types in the C implemented methods.
* hash.c (rb_hash_s_try_convert): ditto.
* io.c (rb_io_s_try_convert): ditto.
* re.c (rb_reg_s_try_convert): ditto.
* string.c (rb_str_s_try_convert): ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13251 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r-- | ChangeLog | 14 | ||||
-rw-r--r-- | array.c | 18 | ||||
-rw-r--r-- | hash.c | 18 | ||||
-rw-r--r-- | io.c | 18 | ||||
-rw-r--r-- | re.c | 17 | ||||
-rw-r--r-- | string.c | 18 |
6 files changed, 103 insertions, 0 deletions
@@ -1,3 +1,17 @@ +Sat Aug 25 02:08:45 2007 Yukihiro Matsumoto <matz@ruby-lang.org> + + * array.c (rb_ary_s_try_convert): a new class method to convert + object or nil if it's not target-type. this mechanism is used + to convert types in the C implemented methods. + + * hash.c (rb_hash_s_try_convert): ditto. + + * io.c (rb_io_s_try_convert): ditto. + + * re.c (rb_reg_s_try_convert): ditto. + + * string.c (rb_str_s_try_convert): ditto. + Sat Aug 25 00:49:44 2007 Koichi Sasada <ko1@atdot.net> * benchmark/bm_loop_generator.rb: added. @@ -245,6 +245,23 @@ rb_check_array_type(VALUE 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. + * + * Array.try_convert([1]) # => [1] + * Array.try_convert("1") # => nil + */ +static VALUE +rb_ary_s_try_convert(VALUE dummy, VALUE ary) +{ + return rb_check_array_type(ary); +} + +/* + * call-seq: * Array.new(size=0, obj=nil) * Array.new(array) * Array.new(size) {|index| block } @@ -2932,6 +2949,7 @@ Init_Array(void) 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); @@ -336,6 +336,23 @@ to_hash(VALUE 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(VALUE dummy, VALUE hash) +{ + return rb_check_convert_type(hash, T_HASH, "Hash", "to_hash"); +} + static int rb_hash_rehash_i(VALUE key, VALUE value, st_table *tbl) { @@ -2536,6 +2553,7 @@ Init_Hash(void) 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); @@ -242,6 +242,23 @@ rb_io_check_io(VALUE 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(VALUE dummy, VALUE io) +{ + return rb_io_check_io(io); +} + static void io_unread(rb_io_t *fptr) { @@ -5708,6 +5725,7 @@ Init_IO(void) 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); @@ -2034,6 +2034,22 @@ rb_check_regexp_type(VALUE re) return rb_check_convert_type(re, T_REGEXP, "Regexp", "to_regexp"); } +/* + * 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. + * + * IO.try_convert(/re/) # => /re/ + * IO.try_convert("re") # => nil + */ +static VALUE +rb_reg_s_try_convert(VALUE dummy, VALUE re) +{ + return rb_check_regexp_type(re); +} /* * call-seq: @@ -2422,6 +2438,7 @@ Init_Regexp(void) rb_define_singleton_method(rb_cRegexp, "escape", rb_reg_s_quote, -1); rb_define_singleton_method(rb_cRegexp, "union", rb_reg_s_union, -1); 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); @@ -615,6 +615,23 @@ rb_check_string_type(VALUE 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(VALUE dummy, VALUE str) +{ + return rb_check_string_type(str); +} + VALUE rb_str_substr(VALUE str, long beg, long len) { @@ -4877,6 +4894,7 @@ Init_String(void) rb_cString = rb_define_class("String", rb_cObject); rb_include_module(rb_cString, rb_mComparable); rb_define_alloc_func(rb_cString, str_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); |