Ruby's class variables

So, after 6 years of hiatus I finally decided to get back to programming. And sure, I though I'd like to learn something new and switch from C/C++ to Ruby. The problem, however is this: now that I've finally gotten to the point where I know how to make classes in Ruby, one thing that bewilders me is the Ruby's way of defining class variables - or should I say the lack of way of defining them.

Will it not become very prone to errors if the programmer can just throw in new variables to classes whenever he feels like, without first defining them in some block inside the class? I mean, okay, I was writing this simple 100 line class that did some pretty general stuff: write to file and read from file or something like that, just for the heck of it. Yet, I managed to mispell a variable two times in the program code. The first instance was not so bad: I had this general logical testing in the like of:
if @variable_name >= some_value
so, I forgot the @ from the beginning, no big deal - I hadn't mispelled it like that in anywhere within the same method, so Ruby saw it as a new undefined local variable and gave me an error.

But the second time I wanted to give a value to a class variable and mispelled the variable name there (instead of "@variable = some_value", I wrote "variable = some_value") and that messed up the program pretty badly. It took me a relatively long time (considering the program was very basic and small) time to figure out where the problem was because that expression was perfectly legal in Ruby's point of view.

So, that made me wonder - just how error prone the Ruby's way of declaring class variables is? Am I creating a fuzz over nothing, or is this a common problem? If this is a common problem, what are the strategies that the professional Ruby programmers use to get rid of errors like that?

Yeah, I guess run time evaluation of code has its own challenges but somehow I've never had a problem with any other variables, but this strange way to use class variables is making me very nervous about using Ruby.

I hope you can understand what I'm trying to say: english is not my mother tongue and I know I have some problems expressing myself in english, but I've tried to be as clear as I can.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories