Forum: Ruby The cost of import

Posted by connor culleton (culletron)
on 2013-02-18 16:08
Hi Everyone,

I am working with a test framework which has a module where a lot of
functionally has been defined.


module Stuff
    # lots_of_stuff...
end


 When the test framework starts this module is loaded.

I am creating a controller class, which will be loaded for certain cases
and I need access to the methods defined in the module "stuff".  Rather
Crudely I have done this

class MyController
    include Stuff
end

So in essence "load Stuff" will be called twice. Is there a cost to
doing this or will my class just get passed a pointer to all those
functions that are all ready in memory?

Or are there any nicer ways to do what I wanted?

Thanks a mil !
Posted by Robert Klemme (robert_k78)
on 2013-02-18 16:48
(Received via mailing list)
On Mon, Feb 18, 2013 at 4:10 PM, connor culleton <lists@ruby-forum.com> 
wrote:
>
> So in essence "load Stuff" will be called twice.
What do you mean by that?  I do not see any "load" in your code at
all.  Keyword "include" will not load any code.  It will just insert
the module in the inheritance chain (see MyController.ancestors) for
method lookup.

Did you measure any timing issus?

> Is there a cost to
> doing this or will my class just get passed a pointer to all those
> functions that are all ready in memory?
>
> Or are there any nicer ways to do what I wanted?

You typically load code only once - either through "load" or even
better "require".

I am under the impression that you might confuse terms here.  Loading
typically means what "load" and "require" do: the source file is read
by the Ruby interpreter, parsed and compiled into some internal
representation.  When you include a module in a class nothing like
that happens.

Kind regards

robert
Posted by connor culleton (culletron)
on 2013-02-18 17:19
Hey Robert!

Yes you are correct. That was confusing terminology on my part, I am
just using 'include' keyword. I'll edit the op now. (edit: no I won't 
because it gets locked after 15 mins... :) )

"It will just insert the module in the inheritance chain (see
MyController.ancestors) for
method lookup."

Ok that is helpful!

To be more precise about what is happening in my case. Our framework is
using cucumber and the large module is being included like this

World(Stuff)

Then I am later including it in a controller class to get access to the
same methods. Including the whole thing again into another class feels
wrong some how...
Posted by Robert Klemme (robert_k78)
on 2013-02-18 17:40
(Received via mailing list)
On Mon, Feb 18, 2013 at 5:19 PM, connor culleton <lists@ruby-forum.com> 
wrote:
>
> To be more precise about what is happening in my case. Our framework is
> using cucumber and the large module is being included like this
>
> World(Stuff)
>
> Then I am later including it in a controller class to get access to the
> same methods. Including the whole thing again into another class feels
> wrong some how...

Why? That's the whole point in placing code in a module vs. in a class.

Cheers

robert
Posted by Robert Klemme (robert_k78)
on 2013-02-18 17:41
(Received via mailing list)
On Mon, Feb 18, 2013 at 5:39 PM, Robert Klemme
<shortcutter@googlemail.com> wrote:
> On Mon, Feb 18, 2013 at 5:19 PM, connor culleton <lists@ruby-forum.com> wrote:

>> Then I am later including it in a controller class to get access to the
>> same methods. Including the whole thing again into another class feels
>> wrong some how...
>
> Why? That's the whole point in placing code in a module vs. in a class.

Just an example for how much reuse there is:

$ ruby -e 'ObjectSpace.each_object(Class) {|cl| puts cl if
cl.ancestors.include? Enumerable}'
Enumerator::Generator
Enumerator
Struct::Tms
Dir
File
ARGF.class
IO
Range
Struct
Hash
Array

Cheers

robert
Posted by connor culleton (culletron)
on 2013-02-19 10:51
Thanks a lot Robert
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.