From: Brian Candler Date: 2010-03-31T04:35:43+09:00 Subject: Re: require and include?? 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/.