On 6/22/07, John N. Joyner <jnj / thecoopercompany.com> wrote:
> Is there a significant performance difference between these two ways to
> call a method?
> (a) Define the method in a module, then call it with
> MODULE_NAME::METHOD_NAME

You don't need to use ::, that's mostly meant for constants and nested
class seperation.

Instead, do:

module MyModule

   module_function

  def whatever

  end

end

you can also of course pass specific method names to module_function.

This lets you do:

MyModule.whatever

> (b) Define the method in a class, then instantiate an object from the
> class, then call the method with OBJECT_REFERENCE_VAR.METHOD_NAME

You can also make class methods.

class A
  def self.whatever
  end
end

A.whatever

This is useful when an object is stateful but doesn't need to have
instances (or has behaviour at the class level and instance level)

Finally, to answer you question about performance, a big difference is
that modules *can't* be instantiated.  But I doubt that they will be
much less heavyweight than a class that you don't create instances of,
or use just one instance.  You'll want to use profile memory usage to
be sure.