[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

class variables from modules

Florian Weber

11/3/2004 2:14:00 PM

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




3 Answers

dblack

11/3/2004 2:25:00 PM

0

Gavin Kistner

11/3/2004 3:21:00 PM

0

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'?

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

--
"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



Robert Klemme

11/3/2004 3:55:00 PM

0


"Gavin Kistner" <gavin@refinery.com> schrieb im Newsbeitrag
news:F6B331F6-2DAB-11D9-9DD4-000A959CF5AC@refinery.com...
> 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'?
>
> 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