Ruby and Rails tricks that you might not know about. (part 1)

1. Underscore in the terminal always contains your last calculated expression

If you do forget, like I do sometimes, to assign some expression in the IRB (or Rails console) to some variable, then you can always use the underscore, which already has the calculated expression.

For example:

> User.where(admin: true)
=> #<ActiveRecord::Relation [#<User id: 27, email: "omar.qunsul.fake.email@gmail.com", encrypted_password: "$2a$10$T1rYV3jvUx2hV0LMXQI.......
> admins = _

In the previous example, the admins relation will be assigned to the admins variable.

2. Look up the implementation of any method, even if it was in a Gem.

If you are curious to know about where some method is implemented, just use the source_location method to help you.

Example:

> 2.2.2 :003 > u = User.last
=> #<User id: 41, email: "guest139256@chessduo.com", encrypted_password: "$2a$10$etAi11lymKPCDohf2HVERuoYEd8Vss5AX7TPTktl/eC...", name: "Guest139256", reset_p.....
> u.method(:destroy).source_location
=> ["/home/omar/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/transactions.rb", 280]

But keep on mind, that ruby native methods, will return nil, because they are implemented in C, in Ruby source code.

I will write more in part 2

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.