From: Mickael Faivre-Macon on
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.
--
Posted via http://www.ruby-forum.com/.

From: Andrew Wagner on
[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


On Mon, Aug 9, 2010 at 4:23 PM, Mickael Faivre-Macon <faivrem(a)gmail.com>wrote:

> 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.
> --
> Posted via http://www.ruby-forum.com/.
>
>

From: Rob Biedenharn on

On Aug 9, 2010, at 4:36 PM, Andrew Wagner wrote:
>
> On Mon, Aug 9, 2010 at 4:23 PM, Mickael Faivre-Macon <faivrem(a)gmail.com
> >wrote:
>
>> 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.
>> --
>> Posted via http://www.ruby-forum.com/.

>
> 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
Rob(a)AgileConsultingLLC.com http://AgileConsultingLLC.com/
rab(a)GaslightSoftware.com http://GaslightSoftware.com/


From: Mickael Faivre-Macon on
Thanks Rob !
Mickael
--
Posted via http://www.ruby-forum.com/.

From: Markus Fischer on
Hi,

On 09.08.2010 22:46, Rob Biedenharn wrote:
> 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