Module methods

P

Pito Salas

I am trying to do this:

==== File: good.rb ====

module Good
def init_module
@var = "good"
end

def life_is_good
puts "life is #{@var}"
end

[... more methods ...]
end

include X
init_module


======== and in a different file ========

include Good

life_is_good

- - - - - - - -

That works but messes up when I try to use ruby-debug with it. So I
changed it to try to call init_module in a different way: instead of
include X, init_module, I tried both of these, neither of which works:

X::init_module

X.init_module

Each of those calls lead to an 'undefined method 'init_module' for
X:Module.

I am getting myself confused with Module methods etc etc.

Any help would be gratefully accepted!!

Pito
 
M

Mark Thomas

It looks like what you really want is a class. Is there a specific
reason you don't want to do this?

class Good
def initialize
@var = "good"
end

def life
puts @var
end
end

g = Good.new
g.life
 
P

Pito Salas

Mark said:
It looks like what you really want is a class. Is there a specific
reason you don't want to do this?

class Good
def initialize
@var = "good"
end

def life
puts @var
end
end

g = Good.new
g.life

Yeah, I am trying to make as transparent as possible "Domain Specific
Language" so that the second file looks like a program in the DSL with
as little extra stuff as possible. So the second file ends up looking
like:

include Good

life
bicycle :first
telephone :last
life


(FOR EXAMPLE :)
 
N

Nation, Carey

Pretty sure that you need to prefix the module methods with the module
name. Yeah, I know.

Try:
module Good
def Good.init_module
@var =3D "good"
end
....

And the in your "different file"

require 'good';
Good.init_module

Which isn't exactly what you want. =20

<talking out of ear now>
I think that you have to prefix the method with the module name since
using it this way gets things added to main rather than to your own
class/instance. It smells like include should make it so that you can
omit the Good. on the call, but it doesn't work for me. Again, I think
it may be something to do with it being in main rather than in your own
class. You're basically trying to make a mixin for main and I
suspect/know that the rules are somewhat different.=20
</talking out of ear now>

Hope that this helps some...

-----Original Message-----
From: (e-mail address removed) [mailto:[email protected]]=20
Sent: Wednesday, June 03, 2009 9:17 AM
To: ruby-talk ML
Subject: Module methods

I am trying to do this:

=3D=3D=3D=3D File: good.rb =3D=3D=3D=3D

module Good
def init_module
@var =3D "good"
end

def life_is_good
puts "life is #{@var}"
end

[... more methods ...]
end

include X
init_module


=3D=3D=3D=3D=3D=3D=3D=3D and in a different file =
=3D=3D=3D=3D=3D=3D=3D=3D

include Good

life_is_good

- - - - - - - -

That works but messes up when I try to use ruby-debug with it. So I
changed it to try to call init_module in a different way: instead of
include X, init_module, I tried both of these, neither of which works:

X::init_module

X.init_module

Each of those calls lead to an 'undefined method 'init_module' for
X:Module.

I am getting myself confused with Module methods etc etc.

Any help would be gratefully accepted!!

Pito
--=20
Posted via http://www.ruby-forum.com/.
 
G

Gregory Brown

Greg, I read the chapter. Excellent stuff. When's the book coming out?
Or is it?

It's already off to the printers so if you pre-order it now you should
have it within the next couple weeks.

-greg
 

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,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top