How to Make Ruby Even More Magical
For the last two weeks, my coding life has been Rails, Rails, Rails. Ruby on Rails is a web-application framework built on top of Ruby. My initial introduction to frameworks was with Sinatra. Sinatra provided me with a solid foundation to help understand some of what’s going on behind the scenes with Rails, but whose primary purpose seemed to be highlighting how superior Rails is(apologies to any Sinatra die-hards out there). The benefits of Rails became apparent as soon as I started familiarizing myself with it. Directly from rubyonrails.org:
It includes everything you need to build fantastic applications, and you can learn it with the support of our large, friendly community.
Rails is packed with all sorts of features and can do so much for you that at times it can seem like it’s actually performing magic(this is actually why I found Sinatra helpful, it broke down some of the Rails magic for me). My favorite things about coding so far has been discovering shortcuts, helpers, packages, gems, resources, basically anything that can make my coding experience and my code cleaner, more concise, more efficient, or more fun. So I decided to do a little digging and find out what else is out there that could make Rails even better.
*Not all of these are applicable solely to Ruby or Rails, some are for command-line, and some are just resources, they’re all just what I’ve found personally useful while learning Rails.
Ruby enVironment Manager, or Ruby Version Manager,
is a command-line tool which allows you to easily install, manage, and work with multiple ruby environments from interpreters to sets of gems.
Basically, RVM lets you easily switch between different versions of Ruby. At first I wasn’t sure why this was valuable but after reading some documentation it quickly became clear. Say you use the most current version of Ruby for personal use but your job requires a previous version of you want to contribute to an open-source project that uses a different version. Switching back and forth to different versions can become confusing and waste time but RVM lets you bounce around easily, all from your command line. Additionally, RVM provides you with a gem management system called Named Gem Sets. I haven’t personally used RVM yet, but it kept coming up in my searches and I think the value is apparent.
CLI Tips and Tricks
This one in particular isn’t exclusive to Ruby, but every developer spends a good amount of time in their terminal and these tips saved me quite a bit of time. I found these on a blog post and have high-lighted my personal favorites below.
These are helpful for general navigation around your cli, especially if you’re in the middle of a really long line.
Ctrl + k - Deletes everything to the right of the cursor
Ctrl + w - Deletes everything to the left of the cursor (one word at a time)
Ctrl + a - Go to the beginning of the line
Ctrl + e - Go to the end of the line
This next one is super helpful if you want to mess around with values and instance variables but you don’t want to save anything to your database. Instead of running
rails console, run
rails console --sandbox. The key difference here is that you can add, edit, or delete values but once you exit your session the database is rolled back. Maybe not vital, but for someone like me who’s really particular even about your test data, it’s saved me a bit of headache.
I haven’t used this one quite as much yet, but I’ve recently started getting into some longer commands, specifically Rails generators. Rather than looking up docs, I’ve started using
Ctrl + r. This opens up reverse-i-search. If you start typing the first few characters of the command you’re looking for it’ll start showing you previous commands you’ve entered. You can also tap your “up” key but this can sometimes be tedious if you’re trying to find a command from a few hours before. You can also enter
history to get a list of all commands you’ve ever input(until you clear your history).
This isn’t a tool like the others listed above but it’s a resource I’ve found personally helpful. Stack Overflow and official documentation like Ruby Docs are invaluable and have typically been the first places I go to when trying to troubleshoot a problem I’m having but they haven’t always had the answer I’m looking for.
Pretty quickly, I found out Stack Overflow functions a lot closer to a wiki than a forum and that it’s not really the place to go to ask a question. There’s almost always an existing page with the solution and if can’t find it, it’s more than likely that I don’t know how to ask the right question. It’s kind of difficult to ask a question when you don’t know what the question is. Enter Reddit.
Reddit has a bit of a reputation that I won’t try to dive into but if you’re unfamiliar, try searching “reddit controversies” and see what pops up. That being said, I’ve found it to be a useful resource with engaged communities so long as I stay within specific subreddits.
The two that I’ve been using more and more of are /r/learnprogramming and /r/ruby. And if you ever need a quick break or laugh, check out /r/programmerhumor. The learnprogramming and ruby subreddits haven’t had a particularly helpful history when searching for the answer to a problem, but they have an active, engaged community. I can ask questions there and usually get a response within a day or less. Take the recommendation with a grain of salt, every subreddit is moderated by volunteers with different rules and expectations, but for the most part I’ve found the community to be helpful and respectful, if a little heavy on the memes.
I’m sure there’s plenty more out there, but these are a few things that has made my coding experience better and hopefully they’ll be able to help you too.