Justin Weiss's Blog, page 3
June 2, 2015
3 ways to configure your Ruby API wrappers
When you use Ruby to wrap an API, you have to have a way to configure it. Maybe the wrapper needs a username and secret key, or maybe just a host.
There are a few different ways to handle this. So which one should you choose?
The easy, global wayYou might want your service to act like it’s always around. No matter where you are in your app, you’d have it ready to use. Otherwise, you’ll spend three lines of configuring it for every line of using it!
You could make the configuration global, us...
May 26, 2015
With so much Rails to learn, where do you start?
Have you seen the Rails Competency Chart?
Brook Riggio of CodeFellows put it together to show all of the concepts a modern Rails developer should know. Take a look:
Frightening, isn’t it? It looks like a two-hundred-tentacled monster that’s preparing to attack you.
It’s no wonder learning Rails is intimidating. Some of the branches, like SQL and Deployment, could be entire career paths. You could spend years on Application Architecture and still not feel like an expert.
But it’s accurate. If...
May 22, 2015
A new way to understand your Rails app's performance
Is your Rails app slow?
When it takes seconds to load what should be a simple view, you have a problem worth digging into.
You could have too many database calls or some slow methods. Or maybe it’s that speedup loop someone put in your code and forgot about.
You can find lots of tools to help you find out what’s slowing your app down. A few weeks ago, I talked about rbtrace. New Relic’s rpm gem has also helped me speed up my apps.
But my favorite tool for exploring performance problems does a...
How to dispel Ruby magic and understand your gems
When you build Rails apps, you’ll use piles of gems. Some of them seem totally magical! But how does that magic happen?
In most gems, there’s nothing they’re doing that you couldn’t. It’s just Ruby code. Sometimes it’s complicated Ruby code. But if you explore that code, you’ll begin to understand where that magic comes from.
Finding the source codeTo understand how a gem works, you have to find its code.
If there’s a method that you want to know more about, you have an easy way to find the...
May 9, 2015
How much testing is too much?
You know how painful it is to work with badly tested code. Every time you fix a bug, you create five more. And when things do work, you never really know if it was designed that way, or just worked coincidentally.
On the other hand, you just wrote what seems like 200 tests to ship one tiny feature. You constantly have to redesign already-working code to hit 100% test coverage. You can’t shake the feeling that your best-tested code is somehow getting less readable. And worst of all, you’re sta...
May 3, 2015
3 quick gem tricks
I recently updated Ruby and upgraded a few projects. And while I did, I found some pretty cool RubyGems features that I didn’t know about before:
When your executables get out-of-dateI used to use rvm to manage Ruby versions. But the last time I set up my machine, I decided to try going without it. You don’t need Gemsets when you have Bundler, and you can use Homebrew to keep Ruby up to date.
This works great, until you update Ruby. And rails new, bundle install and all those other commands...
April 25, 2015
How to select database records in an arbitrary order
In Rails, it’s easy to get a bunch of records from your database if you have their IDs:
1 Person.where(id: [1, 2, 3]).map(&:id) = [1, 2, 3]But what if you wanted to get the records back in a different order? Maybe your search engine returns the most relevant IDs first. How do you keep your records in that order?
You could try where again:
1 Person.where(id: [2, 1, 3]).map(&:id) = [1, 2, 3]But that doesn’t work at all. So how do you get your records back in the right order?
The compatible...April 19, 2015
A shortcut to see your Minitest failures instantly
A quick note: I’m joining Creston Jamison, creator of Discover Ansible, for a free talk about using Ansible to run Rails apps in production. We’ll start at 8am PDT on Thursday, April 16th. Register for it here before Thursday morning, and I hope to see you there!
You’re about to check in your next small feature, so you kick off a full integration test run. You wait, and wait, as the dots fill your screen, until…
......FF....
:-(
You still have a few minutes before your tests finish running. B...
April 7, 2015
How to debug Ruby performance problems in production
You know that performance is a feature. And a lot of performance problems can be found and fixed during development.
But what about those slowdowns that only show up in production? Do you have to add log messages to every single line of code? That would just slow things down even more! Or do you ship tons of tiny “maybe this fixes it” commits to see what sticks?
You don’t have to ruin your code to analyze it. Instead, try rbtrace-ing it.
Trace your running Ruby appWith rbtrace, you can detec...
April 4, 2015
Fun with keyword arguments, hashes, and splats
You’ve probably seen this pattern before. A method has an options hash as its last argument, which holds extra parameters:
1 2 3 4 5 6 def hello_message(name_parts = {}) first_name = name_parts.fetch(:first_name) last_name = name_parts.fetch(:last_name) "Hello, #{first_name} #{last_name}" endUnfortunately, you need to extract those parameters out of the hash. And that means there’s a lot of setup to wade through before you get to the good part.
But if you changed this method to use keyw...


