Read more

Solving "TypeError (nil can't be coerced into Integer)" in the Rails console / IRB

Dominik Schöler
November 07, 2023Software engineer at makandra GmbH

On the Rails console, assigning an object to a variable can lead to this strange error (without stacktrace):

irb > recipient = Recipient.find(123)
Traceback (most recent call last):
TypeError (nil can't be coerced into Integer)
irb > recipient
#<Recipient ...
Illustration web development

Do you need DevOps-experts?

Your development team has a full backlog? No time for infrastructure architecture? Our DevOps team is ready to support you!

  • We build reliable cloud solutions with Infrastructure as code
  • We are experts in security, Linux and databases
  • We support your dev team to perform
Read more Show snapshot

The error is only in the output – the assignment is working. It only occurs when using the --nomultiline option, and thus only with IRB 1.2.0+ and before Ruby 3 Show snapshot . Also, it depends on the value of the assignment – not every Recipient (in this example) will trigger it.


Its cause is in the reline gem Show snapshot , and the issue is fixed in reline 0.2.0. So either gem install reline, or in projects with Bundler:

gem 'reline', '>= 0.2.0' # Fixes TypeError in Rails console
Posted by Dominik Schöler to makandra dev (2023-11-07 13:15)