隐式转换
某些 Ruby 方法接受一个或多个对象,这些对象可以是
-
给定类的对象,因此按原样接受。
-
可以隐式转换为该类的对象,在这种情况下,被调用的方法会转换该对象。
对于每个相关类,转换是通过调用特定的转换方法来完成的
-
Array:
to_ary -
Hash:
to_hash -
Integer:
to_int -
String:
to_str
可转换为 Array 的对象
可转换为 Array 的对象 是一个对象,它
-
拥有实例方法
to_ary。 -
该方法不接受任何参数。
-
该方法返回一个对象
obj,对于该对象,obj.kind_of?(Array)返回true。
满足这些要求的 Ruby 核心类是
本节中的示例使用方法 Array#replace,它接受一个可转换为 Array 的参数。
此类可转换为 Array
class ArrayConvertible def to_ary [:foo, 'bar', 2] end end a = [] a.replace(ArrayConvertible.new) # => [:foo, "bar", 2]
此类不可转换为 Array(无 to_ary 方法)
class NotArrayConvertible; end a = [] # Raises TypeError (no implicit conversion of NotArrayConvertible into Array) a.replace(NotArrayConvertible.new)
此类不可转换为 Array(to_ary 方法接受参数)
class NotArrayConvertible def to_ary(x) [:foo, 'bar', 2] end end a = [] # Raises ArgumentError (wrong number of arguments (given 0, expected 1)) a.replace(NotArrayConvertible.new)
此类不可转换为 Array(to_ary 方法返回非 Array 对象)
class NotArrayConvertible def to_ary :foo end end a = [] # Raises TypeError (can't convert NotArrayConvertible to Array (NotArrayConvertible#to_ary gives Symbol)) a.replace(NotArrayConvertible.new)
可转换为 Hash 的对象
可转换为 Hash 的对象 是一个对象,它
-
拥有实例方法
to_hash。 -
该方法不接受任何参数。
-
该方法返回一个对象
obj,对于该对象,obj.kind_of?(Hash)返回true。
满足这些要求的 Ruby 核心类是
本节中的示例使用方法 Hash#merge,它接受一个可转换为 Hash 的参数。
此类可转换为 Hash
class HashConvertible def to_hash {foo: 0, bar: 1, baz: 2} end end h = {} h.merge(HashConvertible.new) # => {:foo=>0, :bar=>1, :baz=>2}
此类不可转换为 Hash(无 to_hash 方法)
class NotHashConvertible; end h = {} # Raises TypeError (no implicit conversion of NotHashConvertible into Hash) h.merge(NotHashConvertible.new)
此类不可转换为 Hash(to_hash 方法接受参数)
class NotHashConvertible def to_hash(x) {foo: 0, bar: 1, baz: 2} end end h = {} # Raises ArgumentError (wrong number of arguments (given 0, expected 1)) h.merge(NotHashConvertible.new)
此类不可转换为 Hash(to_hash 方法返回非 Hash 对象)
class NotHashConvertible def to_hash :foo end end h = {} # Raises TypeError (can't convert NotHashConvertible to Hash (ToHashReturnsNonHash#to_hash gives Symbol)) h.merge(NotHashConvertible.new)
可转换为 Integer 的对象
可转换为 Integer 的对象 是一个对象,它
-
拥有实例方法
to_int。 -
该方法不接受任何参数。
-
该方法返回一个对象
obj,对于该对象,obj.kind_of?(Integer)返回true。
满足这些要求的 Ruby 核心类是
本节中的示例使用方法 Array.new,它接受一个可转换为 Integer 的参数。
此类用户自定义类可转换为 Integer
class IntegerConvertible def to_int 3 end end a = Array.new(IntegerConvertible.new).size a # => 3
此类不可转换为 Integer(to_int 方法接受参数)
class NotIntegerConvertible def to_int(x) 3 end end # Raises ArgumentError (wrong number of arguments (given 0, expected 1)) Array.new(NotIntegerConvertible.new)
此类不可转换为 Integer(to_int 方法返回非 Integer 对象)
class NotIntegerConvertible def to_int :foo end end # Raises TypeError (can't convert NotIntegerConvertible to Integer (NotIntegerConvertible#to_int gives Symbol)) Array.new(NotIntegerConvertible.new)
可转换为 String 的对象
可转换为 String 的对象 是一个对象,它
-
拥有实例方法
to_str。 -
该方法不接受任何参数。
-
该方法返回一个对象
obj,对于该对象,obj.kind_of?(String)返回true。
满足这些要求的 Ruby 核心类是
本节中的示例使用方法 String::new,它接受一个可转换为 String 的参数。
此类可转换为 String
class StringConvertible def to_str 'foo' end end String.new(StringConvertible.new) # => "foo"
此类不可转换为 String(无 to_str 方法)
class NotStringConvertible; end # Raises TypeError (no implicit conversion of NotStringConvertible into String) String.new(NotStringConvertible.new)
此类不可转换为 String(to_str 方法接受参数)
class NotStringConvertible def to_str(x) 'foo' end end # Raises ArgumentError (wrong number of arguments (given 0, expected 1)) String.new(NotStringConvertible.new)
此类不可转换为 String(to_str 方法返回非 String 对象)
class NotStringConvertible def to_str :foo end end # Raises TypeError (can't convert NotStringConvertible to String (NotStringConvertible#to_str gives Symbol)) String.new(NotStringConvertible.new)