I've said it before, but I'll say it again: I love Ruby! Here are some of my favorite Ruby features these days.

Struct

Struct is a quick way to create simple classes with a set of readable and writeable attributes.

Node = Struct.new(:key, :value, :next)
# ^ this is equivalent to:
class Node
  attr_accessor :key, :value, :next
end

Interacting with structures is exactly like interacting with the equivalent classes. They can be instantiated with any number of parameter arguments up to the number of attributes in Struct.new, with the nth parameter being assigned to the nth attribute. Any attributes not set during instantiation are nil.

n = Node.new
# => #<struct Node key=nil, value=nil, next=nil>
n = Node.new(:language, "French")
# => #<struct Node key=:language, value="French", next=nil>
n = Node.new(:language, "French", Node.new, "oops!")
# ArgumentError: struct size differs

To make your structures do more than just store a set of attributes, you can pass them a block:

Person = Struct.new(:first_name, :middle_name, :last_name) do
  def full_name
    "#{last_name}, #{first_name} #{middle_name}"
  end
end

jane = Person.new("Jane", "McSunshine", "Doe")
jane.full_name
# => "Doe, Jane McSunshine"

I've mostly used Structs for nested helper classes that support larger data structures, like entry structures in dictionaries or linked node structures in trees.

.nil?

.nil? is easy to take for granted, but it is a great example of the amazing simplicity in the Ruby language. In many other languages, some things are objects while other things are primitives. In Ruby, everything is an object - numbers, strings, and even nil itself. This means that with Ruby, you can test values like this:

orange_hat_people = query_by_hat_color("orange")
display(orange_hat_people) unless orange_hat_people.nil?

Instead of like this (in Javascript):

var orangeHatPeople = queryByHatColor("orange");
if (orangeHatPeople !== null) {
  display(orangeHatPeople);
}
The Workings of this Magic

Ruby's Object class, which all other classes inherit from, defines .nil? to return false. NilClass is the one class that overrides .nil? to return true.

Retry

At work, I spend much of my time on a Rails application that communicates with various 3rd party services. Because mysteries happen, retry has become my friend. A retry inside of a rescue block causes re-execution of the contents in the begin block.

begin
  method_that_might_raise_error
rescue DerpException => e
  retry
end

This makes it easy in incorporate smart retry logic and failure alerts.

attempts = 0
begin
  attempts += 1
  super_fun_stuff
rescue => e
  retry if attempts < 5
  alert_developer("exhausted retry attempts") if attempts >= 5
end

My inbox and my workplace's chat client thank retry. :)

For more, see Ruby-Doc

Ruby is so pleasantly documented that I have read much of its documentation for fun. Luckily, there's still an unending number of subtle, powerful features that I have yet to learn about, so I won't get bored for a while. Check out the documentation here!