Write to rails console

Ruby on-RailsRubyDebuggingDevelopment EnvironmentRails Console

Ruby on-Rails Problem Overview


When I want to try or debug smthing I run rails console and do some stuff there. I can print some text or variables from code by raising exception with raise "blablabla". Question: How I can just write to rails console without exception raising (and obvious breaking code execution) like a simple logger.info "blah"?

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

As other have said, you want to use either puts or p. Why? Is that magic?

Actually not. A rails console is, under the hood, an IRB, so all you can do in IRB you will be able to do in a rails console. Since for printing in an IRB we use puts, we use the same command for printing in a rails console.

You can actually take a look at the console code in the rails source code. See the require of irb? :)

Solution 2 - Ruby on-Rails

puts or p is a good start to do that.

p "asd"       # => "asd"
puts "asd"    # => asd

here is more information about that: http://www.ruby-doc.org/core-1.9.3/ARGF.html

Solution 3 - Ruby on-Rails

I think you should use the Rails debug options:

logger.debug "Person attributes hash: #{@person.attributes.inspect}"
logger.info "Processing the request..."
logger.fatal "Terminating application, raised unrecoverable error!!!"

https://guides.rubyonrails.org/debugging_rails_applications.html

Solution 4 - Ruby on-Rails

In addition to already suggested p and puts — well, actually in most cases you do can write logger.info "blah" just as you suggested yourself. It works in console too, not only in server mode.

But if all you want is console debugging, puts and p are much shorter to write, anyway.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionfreemanoidView Question on Stackoverflow
Solution 1 - Ruby on-RailsNobitaView Answer on Stackoverflow
Solution 2 - Ruby on-RailsSeriousMView Answer on Stackoverflow
Solution 3 - Ruby on-RailsmatiasmascaView Answer on Stackoverflow
Solution 4 - Ruby on-RailsNIAView Answer on Stackoverflow