From: Rob Biedenharn Date: 2010-08-10T05:46:14+09:00 Subject: Re: modules On Aug 9, 2010, at 4:36 PM, Andrew Wagner wrote: > > On Mon, Aug 9, 2010 at 4:23 PM, Mickael Faivre-Macon >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@AgileConsultingLLC.com http://AgileConsultingLLC.com/ rab@GaslightSoftware.com http://GaslightSoftware.com/