Hello. I’m new to programming and new to ruby. I’ve recently
written a script and have asked people (including this group) about
components of the script. When people see that I’m using class
variables they ask me why and make a frowny face;).
So as I’m trying to get a better understanding of ruby I ask what’s
wrong with class variables? Or could it be how I’ve used them and not
a problem with class variables? I use class variables as a list where
I can add items for future reference. For example let’s say I have a
class variable (@@over_quota) that holds the list of users who are
over quoata. Then I iterate through the passwd file and check each
users home directory and if they are over quota I toss them onto the
class variable (@@over_quoata). Once I’ve iterated through the
passwd file I see if anything is in my class variable
(@@over_quota)and then iterate over the class variable sending email
to each user who’s over quoata.
I’ve recently (hour ago) learned about class instance variables and
I’m wondering if they are less frowned upon than class variables.
In the following example would it be better to use an class instance
variable over a class variable. From my perpective the end result
is the same.
#!/usr/bin/env ruby -w
class Simple
@@var = []
def Simple.append(l)
@@var << l
end
def Simple.return_var
@@var
end
end
one = Simple.append(“fred”)
puts Simple.return_var
two = Simple.append(“tony”)
puts Simple.return_var
./class_variable.rb
fred
fred
tony
#!/usr/bin/env ruby -w
class Simple
@var = []
def Simple.append(l)
@var << l
end
def Simple.return_var
@var
end
end
one = Simple.append(“fred”)
puts Simple.return_var
two = Simple.append(“tony”)
puts Simple.return_var
fred
fred
tony
Thanks in advance for your time!
Hopefully one day I’ll be able to answer someone elses question!
G.