Posted over 4 years ago. Visible to the public.

Ruby: How to make an object that works with multiple assignment

Ruby allows multiple assignment:

a, b, c = o

In order to prove multiple values from a single object, Ruby calls #to_a on the object:

o ='O') def o.to_a [1,2,3] end a, b, c = o # Implicit call to #to_a here a # => 1 b # => 2 c # => 3

Hence, if you want your class to support multiple assignment, make sure to define a #to_a method.

Does your version of Ruby on Rails still receive security updates?
Rails LTS provides security patches for old versions of Ruby on Rails (3.2 and 2.3).

Owner of this card:

Dominik Schöler
Last edit:
over 2 years ago
by Henning Koch
array, destructuring
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Dominik Schöler to makandra dev
This website uses cookies to improve usability and analyze traffic.
Accept or learn more