From: Andrew Wagner Date: 2010-12-03T05:35:52+09:00 Subject: Re: what's wrong with this picture? --001636c5bd5f0d9c0904967359d6 Content-Type: text/plain; charset=UTF-8 On Thu, Dec 2, 2010 at 3:20 PM, Serialhex .. wrote: > so, i dont believe that i've done anything wrong, but i cant seem to get > a module to function properly, mabye i'm doing something stupidly > retartedly wrong, i dont know, but i've even typed it into irb and i get > the same thing. ruby keeps telling me that my method is undefined. > heres the output from irb: > > irb(main):001:0> module DSP > irb(main):002:1> include Math > irb(main):003:1> def sinc(x) > irb(main):004:2> sin(PI*x)/PI*x > irb(main):005:2> end > irb(main):006:1> end > => nil > irb(main):007:0> DSP.public_instance_methods > => [:sinc] > irb(main):008:0> DSP.sinc(0) > NoMethodError: undefined method `sinc' for DSP:Module > from (irb):8 > from C:/Ruby192/bin/irb:12:in `
' > irb(main):009:0> sin 0 > NoMethodError: undefined method `sin' for main:Object > from (irb):9 > from C:/Ruby192/bin/irb:12:in `
' > irb(main):010:0> Math.sin(0) > => 0.0 > irb(main):011:0> exit > > as you can see i even tried to call Math.sin the same way i called > DSP.sinc and that worked... idfk what's wrong, i've looked through what > stuff i have and it dosnt seem to make any sence so hopefully someone > with more programming knowhow can help me. > > thx > > -- > Posted via http://www.ruby-forum.com/. > > Your first clue is that it shows up in the list of "public_instance_methods". Which means you need an instance of something to run it. But...you can't have an instance of a module, so wtf?. Well, if you define it this way, you need to have something like the following to use it: class MyDSPClass include DSP end MyDSPClass.new.sinc(3) Or, if you really want to be able to call DSP.sinc, then change your definition of DSP to the following module DSP include Math def self.sinc(x) # key difference here end end This puts the method on self, which is the _module_ at that point in time. Hope this helps. Andrew --001636c5bd5f0d9c0904967359d6--