We all know how evil class variables are, and they are as dangerous as the “three headed dog” at the dungeon and we shall not talk about it.
But they are necessary for many of the thingies ruby does and used extensively.But today we shall not talk about them.
We saw earlier that, although class instance variables are excellent, but not so friendly to use and they make Ruby look like C++(ahem).
Here goes a little hack, that allows you to define class level attributes based on class instance variables. Since, i often use it in rails and they have taken cattr for class level attributes.
class Object
def self.metaclass; class << self; self; end; end
def self.iattr_accessor *args
metaclass.instance_eval do
attr_accessor *args
end
args.each do |attr|
class_eval do
define_method(attr) do
self.class.send(attr)
end
end
end
end
endWhat above code does is, creates those class instance variables at class scope and also creates accessor methods for it, so that it doesn’t have to suck when used from class instances(remember @foo.class.i_am_class_instance_var)
Here is a sample that, shows it in action:
class Foobar
iattr_accessor :foo
end
Foobar.foo = "Hemant"
p Foobar.foo
lol = Foobar.new
p lol.fooDid i miss anything? comments?
Useful code, but I think naming the generator-method iattr_accessor is a bit misleading. What is 'i' supposed to stand for ? I'd advocate naming the method class_attr_accessor because we are talking about 'attributes of the class object'. Other names could be possible, but just adding 'i' to the normal attr_accessor isn't visually compelling enough. Mistakes would be likely.
Posted by: kesV | March 09, 2007 at 10:01 AM
Useful code, but I think naming the generator-method iattr_accessor is a bit misleading. What is 'i' supposed to stand for ? I'd advocate naming the method class_attr_accessor because we are talking about 'attributes of the class object'. Other names could be possible, but just adding 'i' to the normal attr_accessor isn't visually compelling enough. Mistakes would be likely.
Posted by: kesV | March 09, 2007 at 10:01 AM
Useful code, but I think naming the generator-method iattr_accessor is a bit misleading. What is 'i' supposed to stand for ? I'd advocate naming the method class_attr_accessor because we are talking about 'attributes of the class object'. Other names could be possible, but just adding 'i' to the normal attr_accessor isn't visually compelling enough. Mistakes would be likely.
Posted by: kesV | March 09, 2007 at 10:01 AM
Useful code, but I think naming the generator-method iattr_accessor is a bit misleading. What is 'i' supposed to stand for ? I'd advocate naming the method class_attr_accessor because we are talking about 'attributes of the class object'. Other names could be possible, but just adding 'i' to the normal attr_accessor isn't visually compelling enough. Mistakes would be likely.
Posted by: kesV | March 09, 2007 at 10:01 AM
Useful code, but I think naming the generator-method iattr_accessor is a bit misleading. What is 'i' supposed to stand for ? I'd advocate naming the method class_attr_accessor because we are talking about 'attributes of the class object'. Other names could be possible, but just adding 'i' to the normal attr_accessor isn't visually compelling enough. Mistakes would be likely.
Posted by: kesV | March 09, 2007 at 10:02 AM
Hmm... Browser dysfunction. It seems pressing submit in konqueror submits the entry but didn't reload properly :P
Posted by: kesV | March 09, 2007 at 10:03 AM
I took 1 st home loans when I was 25 and it helped my relatives very much. Nevertheless, I need the auto loan over again.
Posted by: WoodwardVIVIAN | July 28, 2011 at 09:34 AM