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 UI/UX Design

UI/UX Design by makandra brand

We make sure that your target audience has the best possible experience with your digital product. You get:

  • Design tailored to your audience
  • Proven processes customized to your needs
  • An expert team of experienced designers
Read more Show archive.org 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 archive.org snapshot . Also, it depends on the value of the assignment – not every Recipient (in this example) will trigger it.

Solution

Its cause is in the reline gem Show archive.org 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)