simple ruby language question

M

MichaelEconomy

ok, i've got some module and it has some instance variables i want to
be set by the classes extending the module, and aparently i'm not doing
this correctly:


module A
def test
raise 'ARG' if !@blah
@blah
end
end

class B
include A
extend A
@blah = 'YES!'
end



irb(main):025:0> b = B.new
=> #<B:0x136376c>
irb(main):026:0> b.test
RuntimeError: ARG
from (irb):9:in `test'
from (irb):26


can anyone tell me what the syntax should be? how do i set @blah in the
B class so that its available to the test method
 
S

Scott

In class B, you are attepmting to use an instance variable as if it
were a class variable. To set @blah to a value when its instantiated,
you'll need to stick that line in the constructor:

class B
include A
extend A

def initialize
@blah = 'YES!'
end
end

Also, you dont need to both include and extend A inside B. Use extend
when you want to use the modules methods as class methods:

module X
def classy
puts "Yup, I'm classy"
end
end

class Y
extend X
end

Y.classy
=> Yup, I'm classy

You can also extend an instance of an object:

s = "Holy shniekes"
s.extend(X)
s.classy
=> Yup, I'm classy

Use include when you want to "mix-in" the modules methods with the
instance methods defined in the class:

module X
def classy
puts "Yup, I'm classy"
end
end

class Y
include X
end

Y.new.classy
=> Yup, I'm classy

Hope that helps :)

- Scott
 
M

Marcin Mielżyński

ok, i've got some module and it has some instance variables i want to
be set by the classes extending the module, and aparently i'm not doing
this correctly:


module A
def test
raise 'ARG' if !@blah
@blah
end
end

class B
include A
extend A
@blah = 'YES!'
end



irb(main):025:0> b = B.new
=> #<B:0x136376c>
irb(main):026:0> b.test
RuntimeError: ARG
from (irb):9:in `test'
from (irb):26


can anyone tell me what the syntax should be? how do i set @blah in the
B class so that its available to the test method

Try this one:

module A
def test
raise 'ARG' if !@blah
@blah
end
end

class B
include A

def initialize
@blah = 'YES!'
end
end

b = B.new
p b.test

Module#include will make module methods to become instance ones.
Object#extend adds methods to an instance (to the class B in this case,
you could say then B.test)

consider also:

class A
@a=4
end

p A.class_eval{@a}
=> 4

which means that @a is an instance variable of class object, not an
instance variable of class instance.


lopex
 
M

Marcin Mielżyński

Marcin Mielżyński wrote:

It can be easily seen who is the default receiver (class object or
instance object) by doing this:


class A
p self

def initialize
p self
end
end

A.new


lopex
 
R

Robert Klemme

Marcin said:
module A
def test
raise 'ARG' if !@blah
@blah
end
end

class B
include A

def initialize
@blah = 'YES!'
end
end

b = B.new
p b.test

You can even automate this with proper use of initializers:

module A
def initialize(*a,&b)
super
@blah = "set"
end

def test
raise 'ARG' if !@blah
@blah
end
end

class B
include A
end

class C
include A

def initialize(x)
super()
@another_member = x
end
end
=> "set"
Module#include will make module methods to become instance ones.
Object#extend adds methods to an instance (to the class B in this case,
you could say then B.test)

Adding to that, usually the OP should decide whether to do one or another.

Kind regards

robert
 

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
474,202
Messages
2,571,057
Members
47,667
Latest member
DaniloB294

Latest Threads

Top