Date: 2009-02-02 07:37 am (UTC)
You didn't ask me, but if you did I'd tell you about an invaluable software engineering axiom called DRY (Don't Repeat Yourself (http://c2.com/cgi/wiki?DontRepeatYourself)).

The premise is that if something occurs twice in code, that's twice you have to remember to keep it updated when something changes. And by golly, it'll change. In this instance, observe how many times the format string appears in your function, versus in mine.

I also changed the argument order because code is poetry, it should read as you would speak it: pluralize 101 dalmations. This is known as the Principle of Least Surprise (http://c2.com/cgi/wiki?PrincipleOfLeastSurprise) because one day, maybe tomorrow, you're going to forget which way you defined those arguments ... was it dog then number? number then dog?

def pluralize(quantity, unit)
  unit_suffix = ""
  if 0 == quantity or 1 < quantity then
    unit_suffix = "s"
  end
  return "#{quantity} #{unit}#{unit_suffix}"
end


It's never too early to start forming good habits.
This account has disabled anonymous posting.
If you don't have an account you can create one now.
HTML doesn't work in the subject.
More info about formatting

June 2025

S M T W T F S
1234567
891011121314
15161718192021
2223 2425262728
2930     

Most Popular Tags