modules

  • Thread starter Mickael Faivre-Macon
  • Start date
M

Mickael Faivre-Macon

Hi,

I don't understand how to use modules.
Could someone explain this to me please ?

module Test
def yo
puts 'yo'
end
end

Test.yo # undefined method `yo' for Test:Module

Mickael.
 
A

Andrew Wagner

[Note: parts of this message were removed to make it a legal post.]

Modules in ruby have a lot of similarities to classes (in fact, Class
inherits from Module). Anyway, for this reason, it should make sense that
you want to define "self.yo" instead of just "yo", because it's a
module-level method, not some sort of instance method.

Once you do that, to use the module as a scope, you use two colons. So, the
correct code is:

module Test
def self.yo
puts 'yo'
end
end

Test::yo #=> yo
 
R

Rob Biedenharn

Modules in ruby have a lot of similarities to classes (in fact, Class
inherits from Module). Anyway, for this reason, it should make sense
that
you want to define "self.yo" instead of just "yo", because it's a
module-level method, not some sort of instance method.

Once you do that, to use the module as a scope, you use two colons.
So, the
correct code is:

module Test
def self.yo
puts 'yo'
end
end

Test::yo #=> yo

Or, if you want to use a module to hold methods to be mixed in to
another class:

irb> module Test
irb> def yo
irb> puts 'yo'
irb> end
irb> end
=> nil
irb> class Toy
irb> include Test
irb> end
=> Toy
irb> Toy.new.yo
yo
=> nil

It all depends on what you want to do.

-Rob

Rob Biedenharn
(e-mail address removed) http://AgileConsultingLLC.com/
(e-mail address removed) http://GaslightSoftware.com/
 
M

Markus Fischer

Hi,

Or, if you want to use a module to hold methods to be mixed in to
another class:

irb> module Test
irb> def yo
irb> puts 'yo'
irb> end
irb> end
=> nil
irb> class Toy
irb> include Test
irb> end
=> Toy
irb> Toy.new.yo
yo
=> nil

Interesting example. Is there another way to "access" or "make use" of
"yo" besides including the module in a class? Or putting it another way:
can I call yo directly when being defined that way?

thanks
 
J

jason joo

[Note: parts of this message were removed to make it a legal post.]

or just

include Test
yo

in such way?
 
R

Rob Biedenharn

Hi,



Interesting example. Is there another way to "access" or "make use" of
"yo" besides including the module in a class? Or putting it another
way:
can I call yo directly when being defined that way?

thanks

Well, hard you know what you mean, but does this help:

irb> hi = "hello"
=> "hello"
irb> hi.extend Test
=> "hello"
irb> hi
=> "hello"
irb> hi.yo
yo
=> nil

A Module is like a Class in most respects except that it cannot be
instantiated. (So there's no Test.new for that module Test.)

You can include a Module into a class to have its methods appear in
the lookup path for instances of that class. Or you can extend any
object like I did above.

You should try things out in irb and then ask questions when your
attempt to read the docs and understand the behavior fall short.

-Rob

Rob Biedenharn
(e-mail address removed) http://AgileConsultingLLC.com/
(e-mail address removed) http://GaslightSoftware.com/
 
B

Brian Candler

Markus said:
Interesting example. Is there another way to "access" or "make use" of
"yo" besides including the module in a class? Or putting it another way:
can I call yo directly when being defined that way?

Try "module_function".

module Test
def yo
puts 'yo'
end
module_function :yo
end

Test.yo

module Test
public :yo
end

a = Object.new
a.extend Test
a.yo

class Foo
include Test
end

Foo.new.yo
 

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,470
Messages
2,571,809
Members
48,797
Latest member
PeterSimpson
Top