Once we collected for you some tips and methods to write a better Ruby code. And now we are going to show you some magic. Please, welcome seven techniques to increase the developer experience during a Rails Console session. Please note that some of them will come from Ruby itself, and thus won’t be necessarily restricted to the console.
So, let’s start!
Rolling back after the end of a session. It is possible to run the console in a mode called
sandbox. In that mode, every change made to the database will be automatically rolled back when the session terminates
Retrieving the previous execution value. The result of the immediately previous console execution can be retrieved and assigned to a local variable by calling the
Searching for methods with grep. It is possible to find out the complete name of a method having only a part of it. By calling
Arrayone can execute a handy search over the methods of a given object.
Finding out a method location. The
source_locationmethod from the
Objectclass returns the full path of a method’s file definition, including the line where it was defined.
Returning the source code of a method. While it is interesting to know the exactly location of a method, there are cases where might be even better to output its source code directly to the console. This can be achieved by calling
The helper object. The console provides an object called
helper, which can be used to directly access any view helper from a Rails application.
The app object. The console also provides an interesting object called
app, which is basically an instance of your application.
If you want to explore the magic of Rail console sessions in more details, please read this material where you can find more code.