Silence is Foo Mental notes on Ruby, Git, Rails and whatever geeky thing

30Jan/10Off

My friend the Rails Console

NOTE: There's a second part of this post.

Ok, I know this post could be stupid for some smart people, but, c'mon I just discovered a very good "trick" and I'd like to share it, besides, I'd like to share some others I use as a daily basis.

first, fire the console: $ruby script/console:

Examining objects


>>  user = User.find_by_name('john') # user whatever model you want

=> #<User id: 876, name: "john", email: "john@raflabs.com", etc, etc, etc...
if you want to see the object in a better way, you can use:
>> puts user.to_yaml
now, the shortcut I just discovered:
  >> y user 

Accessing the last returning value

Let me introduce you a magic variable: _

_ returns the last value, so:


>> user.roles

=> [#<Role id: 1, name: "admin" etc, etc, etc...

>> _

=> [#<Role id: 1, name: "admin" etc, etc, etc...

>> (2 + 5 ) * (10 / 2)

=> 35

>> _

=> 35

Calling controller actions


>> app.get 'controller/action?parameters'

>> app.get 'sessions/new?login=john&password=john'

=> 200 # this is the response (an http status code)

>> app.response.body # the html

>> app.response.cookies # the cookies

IRB sub-sessions (one of my favorites)

You can create irb (Interactive Ruby) sessions inside irb sessions, you just need to use the following commands:

irb start a new subsession

jobs list subsessions

fg # switch to a subsession

kill # kill a subsession


>> user = User.first
>> irb
>> user # undefined local variable
>> user = User.last
>> irb
>> user # undefined local variable
>> user  = User.find 4
>> jobs
=> #0->irb on main (#<Thread:0x100170358>: stop)
#1->irb#1 on main (#<Thread:0x102f1ca18>: stop)
#2->irb#2 on main (#<Thread:0x102e0cf88>: running)
>> fg 0
>> user
=> #<User id: 1, name: "john" ...
>> fg 1
>> user
=> #<User id: 587, name: "peter" ...
>> fg 2
>> user
=> #<User id: 7, name: "other" ...
>> kill 2
>> kill 1
>> jobs
=> #0->irb on main (#<Thread:0x100170354>: running)
ok, let's go a little bit further, let's say you just opened a rails console and write the following
>> irb_context.workspace.main #it can be context.workspace.main or even self
=> #<Object:<strong>0x100177298</strong> @helper_proxy=#<Object:0x102fb42f0>, @controller=#<ApplicationController:0x102fb3ff8>>
>> irb #create a new subsession
>> self
=> #<Object:<strong>0x100177298</strong> @helper_proxy=#<Object:0x102fb42f0>, @controller=#<ApplicationController:0x102fb3ff8>>

nice!, it's the same object (0x100177298) in all the sessions, so we can share methods along all the sessions because we would be working in the same object,
but, could I change the object I am working on? YES!

>> user = User.first
>> irb user
>> name
=> "john"
>> email
=> "john@raflabs.com"
>> exit
>> name
=> NameError: undefined local variable or method `name' for #<Object:0x100177298>
wooow, I'm like inside the object!!!

Ruby documentation in the console

For people like me, with a -100 IQ brain which doesn't have a very good memory this is AMAZING!

You may know that the Ruby Array class has an "each" method to iterate collections and that if you write in the terminal (bash console, for example):


$ri Array#each

you'll be presented with the documentation of the Array#each method, but what if you were able to do this in the console:


>> my_array = [3,5,6]

>my_array.ri_each

=> array.each { |item| block }

Calls _block_ once for each element in _self_, passing that element as a parameter

Well, now you can do it, just go to this page an see how to: http://eigenclass.org/hiki/irb+ri+completion

This is not a trick

as you may know, if you do this:


users = User.all

ruby returns the whole array of all the existing users (it may be thousands of them), but sometimes you don't want to see it, so you can do this:


users = User.all; nil
as you may remember, ruby returns the last evaluated expression, so if nil is the last one, it returns nil.
That's it
Filed under: rails 8 Comments