Hey aidy,

 There aren't any attribute accessors for class variables. Why? You could I
suppose, but you'd want to keep them accessible only by methods in the
class. I think I'm starting to understand your general question or I could
be totally offbase.

 Say for instance to take a classic example you want to figure out how many
times your class is being used. This is a pretty trivial example but I hope
will illustrate the point.

class MyClass
 @@users = 0

 def initialize
   @@users +=1
   puts "This could do something more useful" if @@users > 1 # this could
exit refuse new instances, etc
 end

 def count
   puts @@users
 end
end


m1 = MyClass.new
m1.count
=> 1
m2 = MyClass.new
=> "This could do something more useful"

The class itself in this case is responsible for it's own information. What
if you were able to set it from some other code through accessors?
some code....
MyClass.users += 1
...oops, my code died, i didn't have the chance to remove myself as a user
...worse yet, i maxed the connections to the MyClass and it's not creating
new instances from other people's code.

Hope that makes some sense.

-Charley



On 5/14/07, aidy lewis <[EMAIL PROTECTED]> wrote:

On 14/05/07, Charley Baker <[EMAIL PROTECTED]> wrote:
> class LoginInput
>   @@user_name = "Vipul.Goyal"
>
>   def LoginInput.user_name
>     @@user_name
>   end
> end
>
> puts LoginInput.user_name
> => "Vipul.Goyal"
>
> # Now with instance:
> class LoginInput
>   @@user_name = "Vipul.Goyal"
>
>   def user_name
>     @@user_name
>   end
> end
>
> l = LoginInput.new
> puts l.user_name
> => "Vipul.Goyal"

Hi Charley,

I looked at this http://www.rubycentral.com/book/tut_classes.html and
it seems to validate the structure of your code. My question then is;
does Ruby not allow short-cut accessor methods (attr_reader,
attr_writer, attr_accessor) for class variables? And if so, why are
they created and used only for instance variables?

cheers

aidy
_______________________________________________
Wtr-general mailing list
Wtr-general@rubyforge.org
http://rubyforge.org/mailman/listinfo/wtr-general

_______________________________________________
Wtr-general mailing list
Wtr-general@rubyforge.org
http://rubyforge.org/mailman/listinfo/wtr-general

Reply via email to