New to Rails - Why does the console not give a return value for new object?

Hi! Im new in this forum, and I hope I have not posted in the wrong discussion. Anyway, here is my question: When I look at some guides and someone use the rails console to create an object, the console automatically returns the object, listing all its propertys and values (if it has any). But when I try the same example I can create the object, but there is no return value when I hit return. I just get a new line with the prompt. Why is that? Is there some configuration of the console that I have missed? Or does it have to do with the version of rails? I have version 6.0.2.1.

Here is what it looks like in my terminal (zshell on macos Catalina):

irb(main):001:0> subject = Subject.new(:name => 'First subject', :visible => true)
irb(main):001:0>

So, no return value. I was expecting to get a return value like this:

=> #<Subject id: nil, name: "First subject", position: nil, visible: true, created_at: nil, updated_at: nil>

So, any idea why I just get the prompt when I hit return?

So now I have tried both subject.save and Subject.create with a database (Postgres). When I run the commands the console show me the database transactions, but still no object gets returned. As I understand it irb should return the newly created object. Or have I misunderstood something?

Hi @neromancer! What is the output if you run puts irb_context.echo ? if the output is false then try irb_context.echo = true and after that try subject = Subject.new(name: 'First subject', visible: true) again to see if now the object is returned.

1 Like

Hi @victorperez ! Thanks for your suggestion! But when I run irb_context.echo it returns true. By the way, I have now fire up bash and it behaves the same way as zsh. So the new shell don’t seems to be the problem. It’s more as if irb is suppressing the output/return value. Do you know if there is some other setting that could be controlling the return values?

@neromancer as far I know echo is the only setting to control if the result is returned, you could try with irb --help which should return a list of all available settings.

By default if you run conf.return_format in the rails console you should see: "=> %s\n". This means after running any command the console will print "=> " then the return value then a new line.

To temporarily reset this option to default run:

conf.return_format = "=> %s\n"

then run your command again:

subject = Subject.new(:name => 'First subject', :visible => true)

If this fixes the problem check the content of .irbrc file in your home directory.

Hi Jörgen,

I’m guessing you’re using Ruby 2.7 or higher? Ruby 2.7 changed the console (irb) acts when you assign a variable. Previously, it would echo the value of the new variable, but in 2.7 and higher, it does not.

To see the value of the new variable, just enter the variable by itself and Ruby will evaluate it and show you the current value.

4 Likes

@victorperez Thanks for the tip!

Hi @Jeff1 ! Thank you for your answer! Yes, I am using Ruby 2.7 and irb behaves as you describe it. No return value, but if I just enter the variable and hit return, irb will show me the current value. I suspected that this was a change in behavior and not a bug (since I could get the values). Thank you very much for clarifying the reason of this change of behavior!

1 Like

Thank you for your help @mibradev! But It seems my problem comes from an update of Ruby itself. From 2.7 this is apparently the new behavior when you run irb.

1 Like

So, just to sum this up: From Ruby 2.7 IRB is not echoing out assignment expressions. As a new-be in both Ruby and Rails I really prefer to be able to read the returned value without having to call this object explicitly. I have managed to restore the old behavior by changing the default value in a preference file for IRB. To do this on a Mac (and possibly Linux?) I created a .irbrc file in my home directory. In that file I put this line of code:

IRB.conf[:ECHO_ON_ASSIGNMENT] = true

When I then opened a new terminal window and typed Rails console and run my code, this is what I got:

subject = Subject.new(:name => 'First subject', :visible => true)
=> #<Subject id: nil, name: "First subject", position: nil, visible: true, created_at: nil, updated_at: nil>

So, there is a way to get the return values echoed back in IRB even in 2.7. Thank you for your help @victorperez and @Jeff1!

2 Likes

And thanks @mibradev (I could only mention two users in one post)

1 Like

Glad you have resolved it.

Also, this is funny, it works, but I can’t find it in the documentation: Module: IRB (Ruby 2.7.1) :laughing:

No, that is correct @mibradev. In fact, it is really difficult to find anything about this changed behavior in IRB. Eventually I found the github page for the code change in Ruby 2.7. I posted a comment there, stating my feelings about this (undocumented) change, that is effecting thousands of Ruby/Rails users ;-). https://github.com/ruby/irb/pull/12

1 Like