Understanding to_a vs to_ary in Ruby (And Why It Matters)
At first glance, to_a and to_ary look like they do the same thing.
They don’t.
Both convert objects into arrays, but they exist for very different reasons, and confusing them can lead to subtle bugs—especially in Rails apps where implicit conversions happen all the time.
This post breaks down what each method is for, when Ruby calls them automatically, and how to use them safely.
What to_a Is For
to_a is an explicit conversion.
It means:
“If someone asks for an array, here’s how to represent this object as one.”
Ruby will only call to_a when you explicitly ask for it.
Example:
1range = (1..3)
2range.to_aResult:
1[1, 2, 3]This is safe and predictable. Nothing magical happens behind the scenes.
In Rails, you’ll see to_a used when:
- Converting ActiveRecord relations
- Materializing lazy enumerables
- Making data serializable
What to_ary Is For
to_ary is an implicit conversion.
It tells Ruby:
“This object is an array.”
Because of that, Ruby will call to_ary automatically in situations where it expects an array.
Example:
1class Pair
2 def to_ary
3 [1, 2]
4 end
5end
6
7pair = Pair.new
8a, b = pairRuby calls to_ary without asking you.
This is powerful—but dangerous.
Why to_ary Is Dangerous
When you define to_ary, Ruby may use it in places you didn’t expect:
- Multiple assignment
- Array destructuring
- Splat (
*) operations - Method argument expansion
Example:
1def takes_array(arr)
2 arr.length
3end
4
5takes_array(pair)If pair implements to_ary, Ruby silently treats it as an array.
This can:
- Hide bugs
- Break method contracts
- Make debugging harder
That’s why to_ary should be used very rarely.
A Rails Example Where This Matters
Consider a Rails model that wraps multiple values:
1class Coordinates
2 def initialize(x, y)
3 @x = x
4 @y = y
5 end
6endIf you add to_ary here, Rails helpers, form builders, or serializers may start treating it as a real array.
That’s usually not what you want.
If you want explicit conversion, use to_a instead:
1def to_a
2 [@x, @y]
3endNow conversion only happens when you opt in.
When You Should Use to_ary
Use to_ary only when:
- Your object truly is an array conceptually
- Implicit conversion makes the code clearer
- You fully understand where Ruby might call it
Common examples:
- Tuple-like objects
- Value objects meant for destructuring
- Internal DSLs with controlled usage
Even then, caution is warranted.
When You Should Use to_a
Use to_a almost everywhere else.
It’s the right choice when:
- You want predictable behavior
- You’re working with collections or enumerables
- You don’t want Ruby guessing your intent
Rails itself heavily prefers explicit conversions for this reason.
Final Thoughts
The difference between to_a and to_ary isn’t about arrays.
It’s about intent.
to_asays: “Convert me if asked.”to_arysays: “Treat me as an array everywhere.”
In most Ruby and Rails codebases, explicit conversion wins.
If you ever feel unsure, don’t implement to_ary.
That’s usually the right choice.