class variables from modules

F

Florian Weber

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
 
D

David A. Black

Hi --

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
 
G

Gavin Kistner

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top