Prev: Its a bird, its a plane, no ummm, its a Ruide
Next: SmartImage 0.0.2: a sane cross-platform image compositingand thumbnail library
From: Javier Abaroa on 30 Mar 2010 12:29 Hello, But the alternative is to make a "require" into the script-file to import the modules, and a "include module-name" into a class definition to import all of the module-methods into that class. Javier Abaroa. rantingrick wrote: > On Mar 30, 10:40�am, Steve Howell <showel...(a)yahoo.com> wrote: > (..snip..) > > Thanks Steve that did it. I just used 'require "pathtofile"' and > called 'module::function()' and voila! > > Thanks everyone the responses, i learned a great deal from all of them! -- Posted via http://www.ruby-forum.com/.
From: rantingrick on 30 Mar 2010 14:00 On Mar 30, 11:29 am, Javier Abaroa <gamh03122...(a)hotmail.com> wrote: > Hello, > > But the alternative is to make a "require" into the script-file to > import the modules, and a "include module-name" into a class definition > to import all of the module-methods into that class. > > Javier Abaroa. Thanks Javier! I did look into this also. It is interesting how you can use include inside a class definition to bring in certain functions. As soon as i get a good grasp on this concept i will probably implement it in many scripts. Thanks. PS: i made a typo... "module::function()" should have been "module.function()". Which is also weird because the two are sometimes interchangeable and sometimes not?
From: Brian Candler on 30 Mar 2010 15:35
jbw wrote: > This might clear things up, but to summarise include adds methods, > require adds modules. That is almost completely inaccurate, I'm afraid. 'require' loads and executes code. For example, require "foo" looks for foo.rb or foo.so in the $LOAD_PATH and then loads and executes it. (Actually, it first looks in $LOADED_FEATURES to see if it has already been loaded, and passes if it has. Use 'load' to load and execute unconditionally) foo.rb *might* contain code which defines modules and/or classes, but it doesn't have to. require doesn't care, it just executes whatever is in there. $ cat foo.rb puts "hello world" $ irb --simple-prompt >> require 'foo' hello world => true >> require 'foo' => false # has already been loaded >> 'include' is used to add the instance methods of a module into a class. $ irb --simple-prompt >> module Bar; def hello; puts "hello world"; end; end => nil >> class Foo; include Bar; end => Foo >> Foo.new.hello hello world => nil >> It also works at the 'top level', which is probably what you're thinking of. >> include Math => Object >> sqrt(9) # short for Math.sqrt(9) => 3.0 Notice that 'include' takes a Module as its argument, whereas 'require' takes a String (which is the filename to load). -- Posted via http://www.ruby-forum.com/. |