Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Ruby Refactorings

Never Boolean

You hardly ever have to use the word true in your code.

if (something == true)
  # do something cool
end

Can be rewritten as

if something
  # do something cool
end

Same goes with false

if (something == false)
  # do something cool
end

Can be rewritten as

if !something
  # do something cool
end

Or

unless something
  # do something cool
end

Similarly, methods that return booleans

def even?
  if x % 2 == 0
    return true
  else
    return false
  end
end

Can be rewritten as

def even?
  x % 2 == 0
end

Never Nil

You hardly ever have to use the nil object in your code.

if (something == nil)
  # do something cool
end

Can be rewritten as

if something.nil?
  # do something cool
end

Never For

Ruby supports for-loops, but you almost never have to use them.

for i in (1..3)
  puts i
end

Can be rewritten as

(1..3).each do |i|
   puts i
end

It doesn't look like much of a win, but you'll have more fun when you get comfortable with Ruby Enumerables.

There's also this one-liner

(1..3).each { |i| puts i }

Use this syntax when you get comfortable with Ruby blocks.

Never Class

You hardly ever have to explicitly check the class of something.

if (thing.class == String)
  # do something stringy
end

Can be rewritten as

if (thing.instance_of? String)
  # do something stringy
end

Which reads better.

However, you probably want to do this instead

if (thing.is_a? String)
  # do something stringy
end

Just in case thing isn't a String, but a subclass of String, which may be just as good.

kind_of? is the same as is_a?, but reads more explicitly.

if (thing.kind_of? String)
  # do something stringy
end

So thing is a "kind of" String, even if it's not exactly a String.

In all these examples you're still implicitly referring to the class of an object, even if you're not using the class method. Ruby prefers Duck typing instead.

if (thing.respond_to? :quack)  # Can I do the damn thang?
  thing.quack                  # Then let's do the damn thang!
end

If thing, whatever it may be, can quack, then tell it to quack for you. thing's class doesn't matter. So thing can be, for example, a duck, a person, or a toy.

Each DRYer

Ruby's each method (available on all Enumerables) is a wonderful way to DRY up your code.

  require 'json'
  require 'minitest'
  require 'httparty'
  require 'sequel'

The require here is repetitive. This can be rewritten as

['json', 'minitest', 'httparty', 'sequel'].each { |lib| require lib }

Or, using the word array syntax, as

%w[json minitest httparty sequel].each { |lib| require lib }

Any code that matches this pattern

do_something arg1
do_something arg2
do_something arg3

Can be rewritten as

[arg1, arg2, arg3].each { |arg| do_something arg }

To remove the repetitive typing of do_something.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.