From: jason joo on 9 Aug 2010 21:58 [Note: parts of this message were removed to make it a legal post.] or just include Test yo in such way? 2010/8/10 Markus Fischer <markus(a)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: > > > > 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 > > >
From: Rob Biedenharn on 9 Aug 2010 22:55 On Aug 9, 2010, at 6:37 PM, Markus Fischer wrote: > 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 > 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 Rob(a)AgileConsultingLLC.com http://AgileConsultingLLC.com/ rab(a)GaslightSoftware.com http://GaslightSoftware.com/
From: Brian Candler on 10 Aug 2010 08:57 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/.
From: Markus Fischer on 10 Aug 2010 18:09 On 10.08.2010 16:53, Matt Neuburg wrote: > That's why my Ruby tutorial *starts* with modules: > > http://www.apeth.com/rubyIntro/justenoughruby.html 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
First
|
Prev
|
Pages: 1 2 Prev: windows stty IRB question Next: Accessing programs through the terminal with ruby |