Class variables from modules

hi!

how can i make that the example below returns 'Hello, my name is test'?

module Barable
   def self.append_features(base)
     base.extend(ClassMethods)
     super
   end

   module ClassMethods
     def bar(bar)
       @bar = bar
     end
   end
end

class Foo
   include Barable
   bar "test"

   def hi
     puts "Hello, my bar is #{@bar}"
   end
end

Foo.new.hi

thanks!

ciao!
florian

Hi --

···

On Wed, 3 Nov 2004, Florian Weber wrote:

hi!

how can i make that the example below returns 'Hello, my name is test'?

module Barable
   def self.append_features(base)
     base.extend(ClassMethods)
     super
   end

   module ClassMethods
     def bar(bar)
       @bar = bar
     end
   end
end

class Foo
   include Barable
   bar "test"

   def hi
     puts "Hello, my bar is #{@bar}"
   end
end

Foo.new.hi

Your subject line says class variables, but here you're using instance
variables (which always belong to whatever object is 'self' at a given
point in execution). Did you mean @@bar ?

David

--
David A. Black
dblack@wobblini.net

One way:

module ClassMethods
  attr_accessor :bar
end

class Foo
  extend ClassMethods
  self.bar = "test"

  def hi
    puts "Hello, my bar is #{self.class.bar}"
  end
end

Foo.new.hi

···

On Nov 3, 2004, at 7:14 AM, Florian Weber wrote:

how can i make that the example below returns 'Hello, my name is test'?

--
"Despite the surge of power you feel upon learning Ruby,
resist the urge to trip others or slap them in the bald head.
DO NOT LORD YOUR RUBYNESS OVER OTHERS!"
- Why the Lucky Stiff

"Gavin Kistner" <gavin@refinery.com> schrieb im Newsbeitrag
news:F6B331F6-2DAB-11D9-9DD4-000A959CF5AC@refinery.com...

> how can i make that the example below returns 'Hello, my name is

test'?

One way:

module ClassMethods
attr_accessor :bar
end

class Foo
extend ClassMethods
self.bar = "test"

def hi
puts "Hello, my bar is #{self.class.bar}"
end
end

Foo.new.hi

Or

module Barable
  def self.included(cl)
    class << cl
      attr_accessor :bar
    end
  end

  def bar() self.class.bar end
  def bar=(b) self.class.bar(b) end
end

class Foo
   include Barable
   self.bar = "test"

   def hi
     puts "Hello, my bar is #{bar}"
   end
end

Kind regards

    robert

···

On Nov 3, 2004, at 7:14 AM, Florian Weber wrote: