Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > what's wrong with this picture?

Reply
Thread Tools

what's wrong with this picture?

 
 
Serialhex ..
Guest
Posts: n/a
 
      12-02-2010
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 `<main>'
irb(main):009:0> sin 0
NoMethodError: undefined method `sin' for main:Object
from (irb):9
from C:/Ruby192/bin/irb:12:in `<main>'
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/.

 
Reply With Quote
 
 
 
 
Andrew Wagner
Guest
Posts: n/a
 
      12-02-2010
[Note: parts of this message were removed to make it a legal post.]

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 `<main>'
> irb(main):009:0> sin 0
> NoMethodError: undefined method `sin' for main:Object
> from (irb):9
> from C:/Ruby192/bin/irb:12:in `<main>'
> 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

 
Reply With Quote
 
 
 
 
Jeremy Bopp
Guest
Posts: n/a
 
      12-02-2010
On 12/2/2010 2: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 `<main>'
> irb(main):009:0> sin 0
> NoMethodError: undefined method `sin' for main:Object
> from (irb):9
> from C:/Ruby192/bin/irb:12:in `<main>'
> 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.


You defined an instance method for DSP, but you need to call it as a
class method. The problem then is that your sinc method will need to
call sin also as a class method, so you need to extend Math rather than
include it. Try the following:

module DSP
extend Math
def self.sinc(x)
sin(PI*x)/PI*x
end
end

-Jeremy

 
Reply With Quote
 
Reid Thompson
Guest
Posts: n/a
 
      12-02-2010
On Fri, Dec 03, 2010 at 05:20:49AM +0900, Serialhex .. wrote:
> so, i dont believe that i've done anything wrong, but i cant seem to get
>...snip...
> 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.


perhaps

irb(main):011:0> include DSP
=> Object
irb(main):012:0> DSP::sinc(0)
=> 0.0
irb(main):013:0> DSP.sinc(0)
=> 0.0


 
Reply With Quote
 
.serialhex ..
Guest
Posts: n/a
 
      12-02-2010
thanks Reid, your fix worked, but why should i have to 'include DSP' if
the definition of the module is right there? that dosnt make any
sense!! but it's working right now so i wont question too much, thanks!

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

 
Reply With Quote
 
Brian Candler
Guest
Posts: n/a
 
      12-02-2010
serialhex .. wrote in post #965797:
> Edit: Andrew your solution works too, (i didnt see it before, even tho
> you posted first) but thats alot of self.xxx's. is the main library
> written like that? is Math written:
>
> module Math
> def self.sine(x)
> # sine stuff
> end
> def self.cos(x)
> # cosine stuff
> end
> end
>
> ??? that seems a little tedious (though i imagine with some nifty
> metaprogramming you can add all the self's) that just kinda seems odd to
> me.


There is module_function (which is subtly different though

module Math
module_function
def sinc(x)
sin(PI*x)/PI*x
end
end

# or you can do module_function :sinc after the definition

>> Math.sinc(0.5)

=> 0.159154943091895

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

 
Reply With Quote
 
.serialhex ..
Guest
Posts: n/a
 
      12-03-2010
Brian, that's wonderful... that's a beautiful, elegant way to solve my
problem! though i'm sure the other ways are better in different
situations, this works beautifully (and your output of Math.sinc(0.5)
pointed out that my 'newer and faster' organization of the code was
apparently also goving me truncation errors and giving a value of
0.6366197723675814 WAYYYY off...)

thanks everybody for the help!

i'm sure i'll be back with more Q's... i'm still a noob but ruby is
proving WAY easier than my forays into C/C++, VB (which is ridiculously
verbose IMO) and the few other langs i've tried.

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

 
Reply With Quote
 
botp
Guest
Posts: n/a
 
      12-03-2010
On Fri, Dec 3, 2010 at 5:05 AM, .serialhex .. <> wrote:
> thanks Reid, your fix worked, but why should i have to 'include DSP' if
> the definition of the module is right there?


http://www.ruby-doc.org/docs/Program...t_modules.html

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Have I bought wrong product? enquirer Wireless Networking 2 06-10-2005 10:59 PM
Zero Config keeps connecting to the wrong AP =?Utf-8?B?ZGdyaWZmaXRo?= Wireless Networking 2 03-04-2005 05:52 PM
Is XML Doc wrong or is Schema wrong? (or both) Matthew XML 7 01-07-2005 10:05 PM
wrong connection status Peter Welk Wireless Networking 0 12-22-2004 03:26 PM
XP SP2 Wrong IP on connection D Wells Wireless Networking 3 12-09-2004 03:35 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57