File: //usr/share/ri/3.0.0/system/page-implicit_conversion_rdoc.ri
U:RDoc::TopLevel[ i I"implicit_conversion.rdoc:EFcRDoc::Parser::Simpleo:RDoc::Markup::Document:@parts[QS:RDoc::Markup::Heading:
leveli: textI"Implicit Conversions;To:RDoc::Markup::BlankLine o:RDoc::Markup::Paragraph;[I"2Some Ruby methods accept one or more objects ;TI"that can be either:;To:RDoc::Markup::List:
@type:BULLET:@items[o:RDoc::Markup::ListItem:@label0;[o;
;[I"4<i>Of a given class</i>, and so accepted as is.;To;;0;[o;
;[I"@<i>Implicitly convertible to that class</i>, in which case ;TI"+the called method converts the object.;T@
o;
;[I"IFor each of the relevant classes, the conversion is done by calling ;TI""a specific conversion method:;T@
o;;;;[ o;;0;[o;
;[I"Array: +to_ary+;To;;0;[o;
;[I"Hash: +to_hash+;To;;0;[o;
;[I"Integer: +to_int+;To;;0;[o;
;[I"String: +to_str+;T@
S; ;
i;I"Array-Convertible Objects;T@
o;
;[I":An <i>Array-convertible object</i> is an object that:;To;;;;[o;;0;[o;
;[I""Has instance method +to_ary+.;To;;0;[o;
;[I"%The method accepts no arguments.;To;;0;[o;
;[I"^The method returns an object +obj+ for which <tt>obj.kind_of?(Array)</tt> returns +true+.;T@
o;
;[I"EThe examples in this section use method <tt>Array#replace</tt>, ;TI"1which accepts an Array-convertible argument.;T@
o;
;[I"%This class is Array-convertible:;T@
o:RDoc::Markup::Verbatim;[I"class ArrayConvertible
;TI" def to_ary
;TI" [:foo, 'bar', 2]
;TI" end
;TI" end
;TI"a = []
;TI";a.replace(ArrayConvertible.new) # => [:foo, "bar", 2]
;T:@format0o;
;[I">This class is not Array-convertible (no +to_ary+ method):;T@
o;;[ I"$class NotArrayConvertible; end
;TI"a = []
;TI"S# Raises TypeError (no implicit conversion of NotArrayConvertible into Array)
;TI"(a.replace(NotArrayConvertible.new)
;T;0o;
;[I"KThis class is not Array-convertible (method +to_ary+ takes arguments):;T@
o;;[
I"class NotArrayConvertible
;TI" def to_ary(x)
;TI" [:foo, 'bar', 2]
;TI" end
;TI" end
;TI"a = []
;TI"N# Raises ArgumentError (wrong number of arguments (given 0, expected 1))
;TI"(a.replace(NotArrayConvertible.new)
;T;0o;
;[I"MThis class is not Array-convertible (method +to_ary+ returns non-Array):;T@
o;;[
I"class NotArrayConvertible
;TI" def to_ary
;TI" :foo
;TI" end
;TI" end
;TI"a = []
;TI"o# Raises TypeError (can't convert NotArrayConvertible to Array (NotArrayConvertible#to_ary gives Symbol))
;TI"(a.replace(NotArrayConvertible.new)
;T;0S; ;
i;I"Hash-Convertible Objects;T@
o;
;[I"8A <i>Hash-convertible object</i> is an object that:;To;;;;[o;;0;[o;
;[I"#Has instance method +to_hash+.;To;;0;[o;
;[I"%The method accepts no arguments.;To;;0;[o;
;[I"]The method returns an object +obj+ for which <tt>obj.kind_of?(Hash)</tt> returns +true+.;T@
o;
;[I"BThe examples in this section use method <tt>Hash#merge</tt>, ;TI"/which accepts a Hash-convertible argument.;T@
o;
;[I"$This class is Hash-convertible:;T@
o;;[I"class HashConvertible
;TI" def to_hash
;TI"" {foo: 0, bar: 1, baz: 2}
;TI" end
;TI" end
;TI"h = {}
;TI"Ch.merge(HashConvertible.new) # => {:foo=>0, :bar=>1, :baz=>2}
;T;0o;
;[I">This class is not Hash-convertible (no +to_hash+ method):;T@
o;;[ I"#class NotHashConvertible; end
;TI"h = {}
;TI"Q# Raises TypeError (no implicit conversion of NotHashConvertible into Hash)
;TI"%h.merge(NotHashConvertible.new)
;T;0o;
;[I"KThis class is not Hash-convertible (method +to_hash+ takes arguments):;T@
o;;[
I"class NotHashConvertible
;TI" def to_hash(x)
;TI"" {foo: 0, bar: 1, baz: 2}
;TI" end
;TI" end
;TI"h = {}
;TI"N# Raises ArgumentError (wrong number of arguments (given 0, expected 1))
;TI"%h.merge(NotHashConvertible.new)
;T;0o;
;[I"LThis class is not Hash-convertible (method +to_hash+ returns non-Hash):;T@
o;;[
I"class NotHashConvertible
;TI" def to_hash
;TI" :foo
;TI" end
;TI" end
;TI"h = {}
;TI"o# Raises TypeError (can't convert NotHashConvertible to Hash (ToHashReturnsNonHash#to_hash gives Symbol))
;TI"%h.merge(NotHashConvertible.new)
;T;0S; ;
i;I" Integer-Convertible Objects;T@
o;
;[I"<An <i>Integer-convertible object</i> is an object that:;To;;;;[o;;0;[o;
;[I""Has instance method +to_int+.;To;;0;[o;
;[I"%The method accepts no arguments.;To;;0;[o;
;[I"`The method returns an object +obj+ for which <tt>obj.kind_of?(Integer)</tt> returns +true+.;T@
o;
;[I"AThe examples in this section use method <tt>Array.new</tt>, ;TI"3which accepts an Integer-convertible argument.;T@
o;
;[I"4This user-defined class is Integer-convertible:;T@
o;;[I"class IntegerConvertible
;TI" def to_int
;TI" 3
;TI" end
;TI" end
;TI"0a = Array.new(IntegerConvertible.new).size
;TI"a # => 3
;T;0o;
;[I"MThis class is not Integer-convertible (method +to_int+ takes arguments):;T@
o;;[I"!class NotIntegerConvertible
;TI" def to_int(x)
;TI" 3
;TI" end
;TI" end
;TI"N# Raises ArgumentError (wrong number of arguments (given 0, expected 1))
;TI"*Array.new(NotIntegerConvertible.new)
;T;0o;
;[I"QThis class is not Integer-convertible (method +to_int+ returns non-Integer):;T@
o;;[I"!class NotIntegerConvertible
;TI" def to_int
;TI" :foo
;TI" end
;TI" end
;TI"u# Raises TypeError (can't convert NotIntegerConvertible to Integer (NotIntegerConvertible#to_int gives Symbol))
;TI"*Array.new(NotIntegerConvertible.new)
;T;0S; ;
i;I"String-Convertible Objects;T@
o;
;[I":A <i>String-convertible object</i> is an object that:;To;;;;[o;;0;[o;
;[I""Has instance method +to_str+.;To;;0;[o;
;[I"%The method accepts no arguments.;To;;0;[o;
;[I"_The method returns an object +obj+ for which <tt>obj.kind_of?(String)</tt> returns +true+.;T@
o;
;[I"CThe examples in this section use method <tt>String::new</tt>, ;TI"1which accepts a String-convertible argument.;T@
o;
;[I"&This class is String-convertible:;T@
o;;[I"class StringConvertible
;TI" def to_str
;TI" 'foo'
;TI" end
;TI" end
;TI"2String.new(StringConvertible.new) # => "foo"
;T;0o;
;[I"?This class is not String-convertible (no +to_str+ method):;T@
o;;[I"%class NotStringConvertible; end
;TI"U# Raises TypeError (no implicit conversion of NotStringConvertible into String)
;TI"*String.new(NotStringConvertible.new)
;T;0o;
;[I"LThis class is not String-convertible (method +to_str+ takes arguments):;T@
o;;[I" class NotStringConvertible
;TI" def to_str(x)
;TI" 'foo'
;TI" end
;TI" end
;TI"N# Raises ArgumentError (wrong number of arguments (given 0, expected 1))
;TI"*String.new(NotStringConvertible.new)
;T;0o;
;[I"OThis class is not String-convertible (method +to_str+ returns non-String):;T@
o;;[I" class NotStringConvertible
;TI" def to_str
;TI" :foo
;TI" end
;TI" end
;TI"r# Raises TypeError (can't convert NotStringConvertible to String (NotStringConvertible#to_str gives Symbol))
;TI")String.new(NotStringConvertible.new);T;0:
@file@:0@omit_headings_from_table_of_contents_below0