Style: Case determines what's a constants?

O

OrganicFreeStyle

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?
 
Z

zycte

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?

To my knowledge, the variable naming conventions are deeply rooted in
the Ruby language. You could use _LH, though.
 
O

OrganicFreeStyle

zycte said:
To my knowledge, the variable naming conventions are deeply rooted in
the Ruby language. You could use _LH, though.

Oh? Hmm, the Pragmatic Programmer's Guide had me thinking otherwise.

"Ruby, unlike less flexible languages, lets you alter the value of a
constant, although this will generate a warning message.

MY_CONST = 1
MY_CONST = 2 # generates a warning


"

This would seem to indicate that, although I can't change what Ruby
sees as a constant, I could perhaps at least pretend a constant is a
variable and supress the warning? Bad idea? Doable? Thanks...
 
O

OrganicFreeStyle

Jeffrey said:
OrganicFreeStyle said:
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
 
O

OrganicFreeStyle

Jeffrey said:
I think you have a reasonable point. The Ruby convention makes perfect
sense to those of us from Unix backgrounds, but must look dead wrong to
someone accustomed to Windows or VMS. I guess it boils down to Matz's
personal preference. Dictating this kind of convention does seem more
Pythonic than Rubyish, though.

I know what you mean when you say "dictating this kind of convention",
but at least in Python, this certainly isn't a problem. I can name my
variables in Python whatever the bleep I please, so long as I don't use
double underscores incorrectly (i.e. __bleep__ is usually reserved for
buit-in attributes and __bleep invokes name mangling so that variables
are somewhat private). Python has no notion of constants. Whether this
is more or less flexible is quite open for debate.

I've always had a personal adversion to underscores in variable names,
particularly those placed in the front (i.e. _LH), so maybe I'll go
with something like xLH or vLH etc... :) Thanks for the help folks!
I'll live . . .
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top