1. Technology
You can opt-out at any time. Please refer to our privacy policy for contact information.

Ruby Tip Smorgasbord

Assorted Tips


The following are a number of smaller tips that aren't long enough to warrant an article in themselves. Some are genuinely useful, others only mildly amusing. Use at your own risk.

Underscores in Numeric Literals

Have you ever had to write a large numeric literal? Numbers such as 10,000,000 would normally be written 10000000, but what number is that? Who has time to stop and count zeros. We put the commas in numbers to easily tell their magnitude for a reason, without them the zeros all seem to run together and it's difficult to tell what the number really is.

It would be nice if you could use commas and just say 10,000,000, but sadly you cannot. The Ruby parser wouldn't be able to tell your numeric literal from a list numbers. Instead you can use the underscore character and say 10_000_000. It doesn't look quite as good, but it functions just fine.

This works at the parser-level. The Ruby parser will ignore any single underline characters it encounters inside of a string literal. It doesn't actually enforce where these underlines should be, so while you can say 10_000_000, you can also say 1_0_00000_0, which really doesn't make much sense but is perfectly valid Ruby code. However, you cannot use more than one consecutive underscore, nor can there be a trailing underscore, so you can't say 1______0, or 10_.

Should you use it? Yes! This increases the readability of large numeric literals and doesn't detract from the clarity of the code.

Easy Short Numeric IDs

Say you have a web application that generates short URLs from the ID of a resource. While the ID may be a long number, the URL could be shorter if you could easily encode that number in something that uses more than the digits 0 through 9. Luckily, the standard Fixnum#to_s method can do this.

Normally, you'd simply call the method like 10.to_s to get the string "10", but it can actually convert to any base you wish. Want a binary number? Try 145.to_s(2). A hex number? Try 345.to_s(16). If the method runs out of digits, it just starts using letters, and will work with any base up to 36. So say if you have a very long ID number 1234567890, you can shorten that down to "kf12oi" by using 1234567890.to_s(36).

Should you use it? For this use case, yeah, it's reasonable. This is not a very common method though, at least with bases other than 10.

Conditional Assignments

The &&= and ||= operators are quite useful, if not somewhat Perl-ish. They can both be used to conditionally assign to a variable depending on the value of the left hand side before the assignment.

The ||= operator can be used to assign to a variable if it hasn't yet been assigned to, or its value is otherwise false (in Ruby, things are false if they're the false object or nil). This can be used to make sure there is a value assigned to a variable, and if there is one there already to not disturb it. For example:

variable = nil

if a > 10
  variable = 10
elsif a < 1
  variable = 20

variable ||= 0

The variable is first declared and if neither branch of the conditional assigns a value to it, it will assign a default value. This variation is somewhat commonly used, but the &&= operator is not so common.

The &&= operator assigns to the variable only if the variable already has a true value. So you could say a &&= a + 1 which will be roughly equivalent to a = a + 1 if a. This allows you to let default false values drop on through without having to worry about checking the value every time you want to increment it.

Should you use this? Probably not. Ruby is not Perl. It does not pay to litter your source code with line noise just to save a few characters and a second or two of typing. Using the conditionals, if only in the postfix form, they're much more clear what you really intend to happen.

  1. About.com
  2. Technology
  3. Ruby
  4. Advanced Ruby
  5. Ruby Tip Smorgasbord

©2014 About.com. All rights reserved.