3 tiny Ruby/Rails things you may love
Hi!
In this post I would like to show you a few small tricks and ideas I use in my development process almost everyday. It will not be surprising if you’re familiar with most of them, but even so I hope you’ll find something useful here.
Although I’ve applied them for a decent amount of times I still find these things very joyful. And I encourage you to give them a try!
1. Rails console variables
I visit rails console quite often. It is essential for investigating bugs and just fetching some data from DB, because ActiveRecord makes it extremely handy. When playing with rails console I heavily use its built-in variables.
The first is `_` (underscore).
Rails console stores the result of the last completed command in this variable.
Ability to call `_` saves me tons of time. It helps when I forget to assign the result of the last query to a variable. For example:
> # let's find some project
> Project.is_public.active.where(“backers_count > ?”, 3).last
=> #<Project id: 54700, photo: "photo.jpg", category_id: 39, ...>> # variable `_` now holds the result
> _
=> #<Project id: 54700, photo: "photo.jpg", category_id: 39, ...>> # using `_` it is easy to assign the result to our variable if we need it later (much more quickly than repeating and editing the last command)
> p = _
> p.category_id
=> 39
Shiny.
Another useful variable is `app`.
Calling app in rails console gives you ActionDispatch::Integration::Session instance, and you can use it for making requests and other fun, but most of the time I find this variable helpful for constructing routes in the console. You can simply ‘call’ them on app and you’ll get according strings:
> m = Material.last
=> #<Material:0x007fb2fae80008 id: 8,title: ... >
> app.material_path(m)
=> "/materials/8"
> app.material_url(m)
=> "http://www.example.com/materials/8"
And the last one is `helper`.
You may find this variable handy for getting access to your helper(surprise!) methods. Indeed, you’ll reach built-in Rails helpers too:
> helper.label_tag(:ring_text, “One Ring to rule them all”)
=> "<label for=\"ring_text\">One Ring to rule them all</label>"
I often use helper along with the app to generate links:
> helper.link_to(“Get it”, app.material_path(Material.last))
=> "<a href=\"/materials/8\">Get it</a>"
I find them very useful for constructing simple reports from my data.
2. Ruby Object#tap method
Following Ruby docs, Object#tap { |x| … }
yields x to the block, and then returns x. The primary purpose of this method is to “tap into” a method chain, in order to perform operations on intermediate results within the chain.
Honestly, I don’t meet #tap very often, but sometimes it may help you to write clear and expressive piece of code. And we love such code!
I’ve noticed that I use Object#tap mostly in my specs, especially for stubbing and mocking. For example:
In this example usage of #tap makes stub_material method very elegant and well-looking.
Many thanks to Upcase for demonstrating this beauty.
This one I’ve learned from Joe Ferris:
remigrate='rake db:migrate && rake db:migrate:redo && rake db:schema:dump db:test:prepare'
This is just an alias for applying the migration you’ve just created, I use it instead of rake db:migrate in development.
It simply runs the migration, than undoes it, than runs it again. It is kind of an unobtrusive way to check that up and down of your migraion play nicely together. Catching errors here is much cheaper than catching them later or even after the deployment (if, for some reason, we need to rollback).
That’s all for now, and thanks for reading!
Feel free to drop a note here to share your favorite little things about Rails you use everyday and love.