Implicit Conversions

Some Ruby methods accept one or more objects that can be either:

For each of the relevant classes, the conversion is done by calling a specific conversion method:

Array-Convertible Objects

AnArray-convertible object is an object that:

The Ruby core class that satisfies these requirements is:

The examples in this section use methodArray#replace, which accepts an Array-convertible argument.

This class is Array-convertible:

classArrayConvertibledefto_ary    [:foo,'bar',2]endenda = []a.replace(ArrayConvertible.new)# => [:foo, "bar", 2]

This class is not Array-convertible (noto_ary method):

classNotArrayConvertible;enda = []# Raises TypeError (no implicit conversion of NotArrayConvertible into Array)a.replace(NotArrayConvertible.new)

This class is not Array-convertible (methodto_ary takes arguments):

classNotArrayConvertibledefto_ary(x)    [:foo,'bar',2]endenda = []# Raises ArgumentError (wrong number of arguments (given 0, expected 1))a.replace(NotArrayConvertible.new)

This class is not Array-convertible (methodto_ary returns non-Array):

classNotArrayConvertibledefto_ary:fooendenda = []# Raises TypeError (can't convert NotArrayConvertible to Array (NotArrayConvertible#to_ary gives Symbol))a.replace(NotArrayConvertible.new)

Hash-Convertible Objects

AHash-convertible object is an object that:

The Ruby core class that satisfies these requirements is:

The examples in this section use methodHash#merge, which accepts a Hash-convertible argument.

This class is Hash-convertible:

classHashConvertibledefto_hash    {foo:0,bar:1,baz:2}endendh = {}h.merge(HashConvertible.new)# => {:foo=>0, :bar=>1, :baz=>2}

This class is not Hash-convertible (noto_hash method):

classNotHashConvertible;endh = {}# Raises TypeError (no implicit conversion of NotHashConvertible into Hash)h.merge(NotHashConvertible.new)

This class is not Hash-convertible (methodto_hash takes arguments):

classNotHashConvertibledefto_hash(x)    {foo:0,bar:1,baz:2}endendh = {}# Raises ArgumentError (wrong number of arguments (given 0, expected 1))h.merge(NotHashConvertible.new)

This class is not Hash-convertible (methodto_hash returns non-Hash):

classNotHashConvertibledefto_hash:fooendendh = {}# Raises TypeError (can't convert NotHashConvertible to Hash (ToHashReturnsNonHash#to_hash gives Symbol))h.merge(NotHashConvertible.new)

Integer-Convertible Objects

AnInteger-convertible object is an object that:

The Ruby core classes that satisfy these requirements are:

The examples in this section use methodArray.new, which accepts an Integer-convertible argument.

This user-defined class is Integer-convertible:

classIntegerConvertibledefto_int3endenda =Array.new(IntegerConvertible.new).sizea# => 3

This class is not Integer-convertible (methodto_int takes arguments):

classNotIntegerConvertibledefto_int(x)3endend# Raises ArgumentError (wrong number of arguments (given 0, expected 1))Array.new(NotIntegerConvertible.new)

This class is not Integer-convertible (methodto_int returns non-Integer):

classNotIntegerConvertibledefto_int:fooendend# Raises TypeError (can't convert NotIntegerConvertible to Integer (NotIntegerConvertible#to_int gives Symbol))Array.new(NotIntegerConvertible.new)

String-Convertible Objects

AString-convertible object is an object that:

The Ruby core class that satisfies these requirements is:

The examples in this section use methodString::new, which accepts a String-convertible argument.

This class is String-convertible:

classStringConvertibledefto_str'foo'endendString.new(StringConvertible.new)# => "foo"

This class is not String-convertible (noto_str method):

classNotStringConvertible;end# Raises TypeError (no implicit conversion of NotStringConvertible into String)String.new(NotStringConvertible.new)

This class is not String-convertible (methodto_str takes arguments):

classNotStringConvertibledefto_str(x)'foo'endend# Raises ArgumentError (wrong number of arguments (given 0, expected 1))String.new(NotStringConvertible.new)

This class is not String-convertible (methodto_str returns non-String):

classNotStringConvertibledefto_str:fooendend# Raises TypeError (can't convert NotStringConvertible to String (NotStringConvertible#to_str gives Symbol))String.new(NotStringConvertible.new)