Ruby Shorthand

A Beginners Reference

Mathew Phillip Wheatley
3 min readMay 26, 2020

As I have been learning Ruby I have been making an effort to keep by code concise and readable. The first step to doing this is using the available shorthand. Most people are likely familiar with the shorthand for creating arrays and hashes as show below:

# Traditional New Array Form
x = Array.new()
# Abbreviated New Array Form
x = []
# Traditional New Hash Form
x = Hash.new()
# Abbreviated New Hash Form
x = {}

Figure 1: New Array and Hash Shorthand

Some lesser known shorthand includes single line if and unless statements.

# Multi-Line If Statement
if condition_test
code_if_true
end
# Single Line If Statement
code_if_true if condition_test
# Multi-Line Unless Statement
unless condition_test
code_if_true
end
# Single Line Unless Statement
code_if_true unless condition_test
Figure 2: If and Unless Shorthand

Building on the above, Ruby also provides shorthand for If/Else statements.

# Multi-Line If/Else Statement
if condition_test
code_to_execute_if_true
else
code_to_excute_if_false
end
# Single Line If/Else Statement (Turneary)
condition_test ? code_if_true : code_if_false
Figure 3: If/Else Shorthand

Most code blocks such as the each array enumerable can also be written as a single line. This should only be done when it makes sense, if there is too much going on in the code block it will be more readable in the block form as opposed to the shorthand.

# Code Block (Each Enumerable Example)
[1, 2, 3].each do |x|
puts x
end
# Single Line (Each Enumerable Example)
[1, 2, 3].each{|x| puts x}
Figure 4: Single Line Code Block

Another way to simply your Ruby code is to chain together methods and/or enumerables. This should be done when it makes your code more readable.

# Chaining in a Code Block
[1, 3, 2, 2].uniq.map do |x|
x+1
end.sort
=> [2, 3, 4]
# Chaining in a Single Line Code Block
[1, 3, 2, 2].uniq.map{|x| x+1}.sort
=> [2, 3, 4]
Figure 5: Chaining Methods/Enumerables

Inclusive and exclusive range can often be helpful

# Inclusive Range
(1..5)
=> [1, 2, 3, 4, 5]
# Exclusive Range
(1...5)
=> [1, 2, 3, 4]
Figure 5: Inclusive and Exclusive Range

Common arithmetic operations can be shortened with the following notation.

# Addition
x = x + 2
# Addition Shorthand
x += 2
# Subtraction
x = x - 2
# Subtraction Shorthand
x -= 2
# Multiplication
x = x * 2
# Multiplication Shorthand
x *= 2
# Division
x = x / 2
# Division Shorthand
x /= 2
# Modulus
x = x % 2
# Modulus Shorthand
x %= 2
# Exponent
x = x ** 2
# Exponent Shorthand
x **= 2
Figure 6: Assignment Operators

When working with strings Regular expressions are often helpful and Ruby has a shortcut for creating those as well.

# Traditional New Array Form
x = Regexp.new()
# Abbreviated New Array Form
x = //
Figure 7: New Regular Expression

Finally some bonus shorthand for creating an array of strings and an array of symbols which are separated by whitespace.

%w[walla walla bing bang]
=> ["walla", "walla", "bing", "bang"]
%i[walla walla bing bang]
=> [:walla, :walla, :bing, :bang]
Figure 8: Array of Strings and Symbol Shorthand

--

--

Mathew Phillip Wheatley

I am a software engineer with a mechanical background. Interests swing from aerospace, to woodworking, travel, skiing, hiking, running, climbing, and lasers.