Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Ruby (http://www.velocityreviews.com/forums/f66-ruby.html)
-   -   Ncurses module inclusion question (http://www.velocityreviews.com/forums/t852626-ncurses-module-inclusion-question.html)

Jos Backus 08-29-2008 06:30 PM

Ncurses module inclusion question
 
lizzy:~% cat n
Math.sqrt(4)

include Math
sqrt(4)

require 'rubygems'
require 'ncurses'

Ncurses.initscr

include Ncurses
initscr
lizzy:~% ruby n 2> err
lizzy:~% cat err
n:12: undefined local variable or method `initscr' for main:Object (NameError)
%
lizzy:~%

Why does including Ncurses not have the same effect as including Math?

--
Jos Backus
jos at catnook.com


John Pritchard-williams 08-29-2008 07:14 PM

Re: Ncurses module inclusion question
 
Jos,

Its a weird quirky feature I don't really understand - but managed to
put together this: I have to guess that Ncurses is defining a 'normal'
module, whereas I know (from my book!) that the Math library used
'Module#module_function'.

(My program but based on "The Ruby Programming Language : ISBN-10:
0-596-51617-7 , O'Reilly, Page 248 onwards (esp: page 251 "Includable
Namespace Modules").

Cheers

John

module Mymodule
Module#module_function
def method1
puts "method1 called"
end
end

module MyOtherModule
def MyOtherModule.method2
puts "method2 called"
end
end


include Mymodule
include MyOtherModule
Mymodule.method1
method1 # this will work.
MyOtherModule.method2
method2 #this will fail

Also note: if you do:

include Math
Object.respond_to?("sqrt")

You'll see that Object hasn't really grown a new method at all - it's
all a big trick ;-)
--
Posted via http://www.ruby-forum.com/.



All times are GMT. The time now is 06:32 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.