include and require should not be compared.

include should be compared to inheritance. require should be compared to 
load.

both require and load will execute files that contain ruby code. load 
will do it automatically. require will do it only once (i.e. if it 
hasn't been loaded already).

require and load are both about FILES.

include and subclassing are both about modules/classes. In ruby, 
technically a class is actually a type of module.

modules support multiple inheritance. so a class/module can include 
multiple modules. classes only support single inheritance. a class can 
only inherit from one other class.

when people use include instead of require, they put include at the top 
of the file. this means that the scope of include is EVERYTHING. so it 
means that everything in ruby, every class in ever gem and every library 
should inherit from your module. playing with DYNAMITE.

THEREFORE: only use "include" inside the scope of a module or class that 
you want to add behavior to.

bret

Chuck van der Linden wrote:
> That's a decent analogy.
>
> I'd extend it by giving examples of how you would make use of
> something in both situations, and why you might NOT want to use
> "include" all the time
>
> Let me see if I can do this right..
>
>   if I'm using require then
> require 'otherhouse'
>   # make me a sofa using the pattern in the other house
> sofa = Otherhouse::Browncouch.new
>
>  but if I'm using include, then
> include 'otherhouse'
>  # make that sofa again
> sofa = Browncouch.new
>
> however, what happens in the latter case if there as already a
> 'Browncouch' in your first house?  now you have two different things,
> both with the same name, in the same house.. which will get used to
> make the sofa in the second case?
>
> Seems to me that you are a lot safer to use 'require' unless you are
> intimately familiar with all the objects, methods, etc defined in the
> other code you are about to include in your own.
>
> am I thinking in the correct 'ruby way' here?    if so then given the
> original posters complaint, he should be using 'require' and not
> 'include' if he wants to avoid conflicts with similarly named objects,
> methods, etc.
>
> --Chuck
>
> You can invoke:  user.leadto(knowledge.location)
> but unfortunately: user.drink()
> is a private method
>
> On Mar 13, 8:58 am, marekj <marekj....@gmail.com> wrote:
>   
>> 'require' and 'include' are completely two different concepts.
>> When you require a ruby file you tell the current file to look for classes
>> and modules in that file when needed, also any code not wrapped in method
>> will be executed on require automatically. So you have to 'require' other
>> file before you can 'include' modules that it owns. it's not either-or
>> proposition.
>>
>> When you include a module into a class your class inherits methods defined
>> in the module as it's own.
>> You can see this in a decorator pattern. here is a good simple example with
>> Coffee and Cream modulehttp://ruby.simapse.com/2008/08/test.html
>>
>> Let's say you have a 1 bedroom apartment and one day you buy another 1
>> bedroom apartment next to yours. Now you are the owner of two apartments and
>> you are free to go next door and use anything in that apartment for your
>> needs in your first apartment.
>> Include is like if one day you decide to knock the walls between two
>> apartments and make it one. Therefore you no longer have to go next door, it
>> is now part of your apartment.
>>
>> (disclaimer: I am hoping to write about Metaphors We Ruby By. how am I
>> doing?)
>> marekj
>>
>> Watirloo: Semantic Page Objects in UseCaseshttp://github.com/marekj/watirloo/
>>
>> On Thu, Mar 12, 2009 at 5:54 PM, Chuck van der Linden 
>> <sqa...@gmail.com>wrote:
>>
>>
>>
>>
>>
>>     
>>> I'm still learning a lot regarding how ruby handles modules, classes
>>> etc..
>>>       
>>> but in general, woudln't using "require" instead of include solve his
>>> problem?  (unless he's actually extending or subclassing stuff from
>>> the modules)
>>>       
>>> I thought I remember seeing something about a lot of folks using
>>> "include 'watir'" when they should be using "require 'watir'"
>>>       
>>> On Mar 12, 3:00 pm, marekj <marekj....@gmail.com> wrote:
>>>       
>>>> On Thu, Mar 12, 2009 at 4:38 AM, wesley chen <cjq....@gmail.com> wrote:
>>>>         
>>>>> Thanks, :), it is exactly what I want.
>>>>>           
>>>>> I ask this kind of questions because I find, in my code, I use too
>>>>>           
>>> many:
>>>       
>>>>> include module.
>>>>> When I include the module into the class, all the methods in the module
>>>>> comes into the class's method. That's terrible.
>>>>>           
>>>> to remove the 'terrible' from listing do this use Object.local_methods
>>>> patch.
>>>>         
>>>> class Object
>>>>   def local_methods
>>>>     (methods - Object.instance_methods).sort
>>>>   end
>>>> end
>>>>         
>>>> so if you have
>>>>         
>>>> class Foo
>>>>   def foo
>>>>     'foo'
>>>>   end
>>>> end
>>>>         
>>>> then Bla.new.local_methods #=> ['foo']
>>>> if you want methods defined only as instances of that object.
>>>> useful.
>>>> Wirble uses it for irb inspectionhttp://pablotron.org/software/wirble/
>>>>         
>>>> marekj
>>>>         
>>>> Watirloo: Semantic Page Objects in UseCases
>>>> Human Readable Machine Executable Acceptance Testinghttp://
>>>>         
>>> github.com/marekj/watirloo/
>>>       
>>>>> Thanks.
>>>>> Wesley Chen.- Hide quoted text -
>>>>>           
>>>> - Show quoted text -- Hide quoted text -
>>>>         
>> - Show quoted text -
>>     
> >
>   


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Watir General" group.
To post to this group, send email to watir-general@googlegroups.com
Before posting, please read the following guidelines: 
http://wiki.openqa.org/display/WTR/Support
To unsubscribe from this group, send email to 
watir-general-unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/watir-general
-~----------~----~----~----~------~----~------~--~---

Reply via email to