views:

232

answers:

3

I'm learning Ruby in my spare time, and I have a question about language constructs for constants. Does Ruby have an equivalent of the C++ const keyword to keep variables from being modified? Here's some example code:

first_line   = f.gets().chomp()
column_count = first_line.split( %r{\s+} ).size()
print column_count, "\n"

I'd like to declare column_count to be const, because I use it below in my program and I really don't want to modify it by mistake. Does Ruby provide a language construct for doing this, or should I just suck it up and realize that my variables are always mutable?

Response to comments:

'The most likely cause of "accidental" overwriting of variables is, I'd guess, long blocks of code.' I agree with the spirit of your point, but disagree with the letter. Your point about avoiding long blocks of code and unnecessary state is a good one, but for constants can also be useful in describing the design of code inside of the implementation. A large part of the value of const in my code comes from annotating which variables I SHOULD change and which I shouldn't, so that I'm not tempted to change them if I come back to my code next year. This is the same sentiment that suggests that code that uses short comments because of good variable names and clear indentation is better than awkwardly written code explained by detailed comments.

Another option appears to be Ruby's #freeze method, which I like the look of as well. Thanks for the responses everyone.

+3  A: 

Variables that start with a capital letter are constants in Ruby. So you could change your code to this:

first_line   = f.gets().chomp()
Column_count = first_line.split( %r{\s+} ).size()
print Column_count, "\n"

Now you'll get a warning if you try to modify Column_count.

yjerem
That's cool. Is there any way to make that warning an error?
James Thompson
Convention is to make constants all caps, so in this case, COLUMN_COUNT.
Sarah Mei
Not this prevents you from changing which object is assigned to Column_count. You can still modify the object's internal state. To prevent that as well, use #freeze.
rampion
!!:s/Not/Note/ - sorry , mind the typo
rampion
+4  A: 

Ruby variables in general are, well, variable.

Beyond Jeremy's answer, while entirely accurate, doesn't lead you to a Ruby style that's very "mainstream" or idiomatically sound and I wouldn't recommend it for adoption. Ruby doesn't work like C++ and generally isn't very appropriate for things that C++ is best used for. Operating systems, word processors, that kind of thing.

The most likely cause of "accidental" overwriting of variables is, I'd guess, long blocks of code. After all, if you change the value of a variable in a five-line method, it's going to be fairly apparent! If you're habitually writing blocks of code longer than, say, 10 lines, then those chunks are probably doing too many things and I strongly advise that you make efforts to break them up (increase cohesion). Localise variables as much as possible to minimise the chance of unexpected side-effects (reduce coupling).

Mike Woodhouse
+3  A: 

By convention, constants in ruby are generally written in all caps such as COLUMN_COUNT. But as it was pointed out, all variables that start with a capital letter are Constants.

nkassis