Ruby stuffs

Two things worth noting:

def something(x = 0)
  ...
end

doesn’t ensure that x is set to a value of zero, because the method could be called with nil [something(nil)] so to ensure a default value of zero even in the face a call with nil

def something(x = 0)
  x ||= 0
  ...
end

would do the trick.

The second is that when adding text and an object’s property

x = 'some string'
y = x + something.description

that if something.description is nil, the addition will fail, but if you interpolate ala

x = 'some string'
y = x + "#{something.description}"

something.description evaluates to empty string and therefore won’t fail.

Two subtle regressions our tests caught today when refactoring, but were not easy to see in the code.
Onward!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s