Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write to rails console

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"?

like image 420
freemanoid Avatar asked Feb 19 '13 15:02

freemanoid


People also ask

How do I get to Rails console?

Go to your browser and open http://localhost:3000, you will see a basic Rails app running. You can also use the alias “s” to start the server: rails s. The server can be run on a different port using the -p option. The default development environment can be changed using -e.

Where does Rails logger write to?

In a Rails app, logs are stored under the /log folder. In development mode, the development. log file is used & you see log output on the terminal you're running rails server on.

How do I print output in Ruby?

Ruby has another three methods for printing output. In the example, we present the p , printf and putc methods. The p calls the inspect method upon the object being printed. The method is useful for debugging.


1 Answers

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? :)

like image 172
Nobita Avatar answered Sep 23 '22 06:09

Nobita