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.

···

--
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

···

On Mon, Aug 9, 2010 at 4:23 PM, Mickael Faivre-Macon <faivrem@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/\.

That's why my Ruby tutorial *starts* with modules:

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

m.

···

Mickael Faivre-Macon <faivrem@gmail.com> wrote:

Hi,

I don't understand how to use modules.

--
matt neuburg, phd = matt@tidbits.com <http://www.tidbits.com/matt/&gt;
A fool + a tool + an autorelease pool = cool!
AppleScript: the Definitive Guide - Second Edition!
Matt Neuburg’s Home Page

Hi,

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

module Test
def yo
  puts 'yo'
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:

module Test
   def yo
       puts 'yo'
     end
  end

=> nil

class Toy
  include Test
  end

=> Toy

Toy.new.yo

yo
=> nil

It all depends on what you want to do.

-Rob

Rob Biedenharn
Rob@AgileConsultingLLC.com http://AgileConsultingLLC.com/
rab@GaslightSoftware.com http://GaslightSoftware.com/

···

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

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

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

···

On 10.08.2010 16:53, Matt Neuburg wrote:

That's why my Ruby tutorial *starts* with modules:

Just Enough Ruby

Thanks Rob !
Mickael

···

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

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:

> module Test
> def yo
> puts 'yo'
> end
> end
=> nil
> class Toy
> include Test
> end
=> Toy
> 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 Fischer <markus@fischer.name>

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:
>
> > module Test
> > def yo
> > puts 'yo'
> > end
> > end
> => nil
> > class Toy
> > include Test
> > end
> => Toy
> > 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

Hi,

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

> module Test
> def yo
> puts 'yo'
> end
> end
=> nil
> class Toy
> include Test
> end
=> Toy
> 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

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

hi = "hello"

=> "hello"

hi.extend Test

=> "hello"

hi

=> "hello"

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

···

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

On 09.08.2010 22:46, Rob Biedenharn wrote:

Markus Fischer 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

···

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