OrganicFreeStyle
6/12/2006 3:00:00 PM
Jeffrey Schwab wrote:
> OrganicFreeStyle wrote:
> >
> > Jeffrey Schwab wrote:
> >> OrganicFreeStyle wrote:
> >>> Is there a way to change what Ruby sees as a constant? It's a real pain
> >>> for me not being able to use variables like "LH" (labour hours) and
> >>> instead have to use the far less visible "lh". Ruby thinks LH is a
> >>> constant though, and this really grates on my style preferences, as
> >>> well on how I've programmed for years. Any way to change this?
> >> Do other people have to read your code?
> >
> > Indeed, but they don't know a thing about Ruby, so they wouldn't know
> > the difference.
>
> All-caps identifiers are almost universally recognized as constants, not
> only by Ruby. If your readers have studied mathematics, or programmed
> in C or Java, they are likely to be unpleasantly surprised by the
> mutability of a variable called LH. If you are just performing a single
> operation repeatedly for different values of LH, you might consider
> embedding the operation in a method taking a hash, keyed by 'LH' or
> whatever other strings you like, as a parameter. E.g:
>
> # Calculate the total wages of an employee who has worked LH
> # labor-hours, and who earns WPLH wages per labor-hour. LH and
> # WPLH should be keys into the given map.
> def total_wages(wage_map)
> wage_map['LH'] * wage_map['WPLH']
> end
>
> puts total_wages('LH' => 12, 'WPLH' => 6)
> puts total_wages('LH' => 10, 'WPLH' => 6)
A hash! Brilliant, I had't thought of that. As for your point about all
caps constants, I see your point. But, unfortunately in Ruby (for me),
I can't even write LabourHours as this too would be interpreted as a
constant. I could live without using LH as a variable, but LabourHours,
come on. That totally clashes with the naming conventions and personal
style I'm used to.
Thanks for the hash idea!
Cheers,
DigiO