Using a @@class_variable into a Module

Hi, I want to declare a @@class_variable in class A.
Class A includes a Module M and I want some methods in M using the
@@class_variable but I get some problem:

···

-------------------
module M
  def kk
    #puts (eval %{ #{send('class').class_variables[0]} })
    puts @@class_var
  end
end

class A
  include M
  @@class_var = "kaka1"
end

a = A.new
a.kk
NameError: uninitialized class variable @@class_var in M
        from ./class_attr1.rb:4:in `kk'
-------------------

I understand the problem. Because the nature of class variables when
the Module M uses @@ it looks into a class variable defined in same
class/module.

Is there any way I could use a class A @class_var into a included M
module methods? If not then I'll use a normal attribute but I don't
like it since it's a constant value that could perfectly be a class
variable instead of initialiting it in each A instance creation.

Thanks a lot.

--
Iñaki Baz Castillo
<ibc@aliax.net>

Hi, I want to declare a @@class_variable in class A.
Class A includes a Module M and I want some methods in M using the
@@class_variable but I get some problem:

-------------------
module M
       def kk
               #puts (eval %{ #{send('class').class_variables[0]} })
               puts @@class_var
       end
end

class A
       include M
       @@class_var = "kaka1"
end

a = A.new
a.kk
NameError: uninitialized class variable @@class_var in M
       from ./class_attr1.rb:4:in `kk'
-------------------

I understand the problem. Because the nature of class variables when
the Module M uses @@ it looks into a class variable defined in same
class/module.

Is there any way I could use a class A @class_var into a included M
module methods? If not then I'll use a normal attribute but I don't
like it since it's a constant value that could perfectly be a class
variable instead of initialiting it in each A instance creation.

So why not making it a constant? Try to stay away from class
variables, they are evil.

···

On Mon, May 19, 2008 at 8:02 PM, Iñaki Baz Castillo <ibc@aliax.net> wrote:

Thanks a lot.

--
Iñaki Baz Castillo
<ibc@aliax.net>

> So why not making it a constant? Try to stay away from class
> variables, they are evil.

Nice suggestion, but I'm not sure if that wwill be valid since I need
various clases each one with a own constant value and all of then
including a module that uses that constant. I'll try it.

Yeah, it works!

module M
  def kk
    puts self.class::VAR
  end
end

class A
  include M
  VAR = "--------------------- @@ A ---------------------"
end

class B
  include M
  VAR = "--------------------- @@ B ---------------------"
end

a=A.new
a.kk

"--------------------- @@ A ---------------------"

b=B.new
b.kk

"--------------------- @@ BA ---------------------"

a.kk

"--------------------- @@ A ---------------------"

···

2008/5/20, Iñaki Baz Castillo <ibc@aliax.net>:

2008/5/20, Michael Fellinger <m.fellinger@gmail.com>:

--
Iñaki Baz Castillo
<ibc@aliax.net>