Modules

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.

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

On Aug 9, 2010, at 4:36 PM, Andrew W. wrote:

def yo
puts ‘yo’
end
end

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

Mickael.

Posted via http://www.ruby-forum.com/.

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 B.
[email protected] http://AgileConsultingLLC.com/
[email protected] http://GaslightSoftware.com/

Hi,

On 09.08.2010 22:46, Rob B. wrote:

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

or just

include Test
yo

in such way?

2010/8/10 Markus F. [email protected]

Thanks Rob !
Mickael

On Aug 9, 2010, at 6:37 PM, Markus F. wrote:

irb> end
“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 B.
[email protected] http://AgileConsultingLLC.com/
[email protected] http://GaslightSoftware.com/

Mickael Faivre-Macon [email protected] wrote:

Hi,

I don’t understand how to use modules.

That’s why my Ruby tutorial starts with modules:

http://www.apeth.com/rubyIntro/justenoughruby.html

m.

On 10.08.2010 16:53, Matt N. wrote:

That’s why my Ruby tutorial starts with modules:

Just Enough Ruby

I’m currently sucking in everything I can find on ruby to iron out my
skills and I’ve to say, besides the official ruby tutorials and rails
stuff, that one gives a very nice insights, thanks!

  • Markus

Markus F. wrote:

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