The cost of import

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 !

On Mon, Feb 18, 2013 at 4:10 PM, connor culleton [email protected]
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

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… :slight_smile: )

“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…

On Mon, Feb 18, 2013 at 5:19 PM, connor culleton [email protected]
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

Thanks a lot Robert

On Mon, Feb 18, 2013 at 5:39 PM, Robert K.
[email protected] wrote:

On Mon, Feb 18, 2013 at 5:19 PM, connor culleton [email protected] 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