DEV Community

Burdette Lamar
Burdette Lamar

Posted on

Convertible Objects

Little-known fact: Many Ruby methods accept as arguments not only an instance of a class (e.g., Array object), but also an object that can be converted to that class (e.g., Array-convertible object).

This is true of a dozen or so methods in Hash, and of scores of methods in Array. How many others? I don't know (yet).

There are even a few methods that accept a convertible object as a block return value. Check out method Array#max.

Obscure, I know, but interesting.

Read all about it: Class-Convertible Objects.

Top comments (1)

Collapse
 
kwstannard profile image
Kelly Stannard

Nice.