turnings :: daniel berlinger

passions :: perfections :: peoples :: stuffs

Ruby stuffs

leave a comment »

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!

Written by Daniel

November 15, 2007 at 3:57 pm

Posted in Solved!, code, personal

Leave a Reply