[rspec-devel] EvalModule meaning and naming concerns

David Chelimsky dchelimsky at gmail.com
Fri May 4 08:47:15 EDT 2007

On 5/3/07, Michael Klishin <michael.s.klishin at gmail.com> wrote:
> Hi,
> I''m digging through RSpec source code and stumbled upon EvalModule
> class in Spec::DSL::Behaviour module.
> First question is what is semantics of this class? I see the way it is
> used in Behaviour module but doubt I get it right.

FWIW, this is the most confusing part of RSpec's internals for me too.

The examples each get run in a separate instance of some class. By
default, this is an instance of Object, but you can use the #inherit
method to make it an instance of any other class you like. That's what
allows us to get all the rails fixture set up, etc, for free.

Whatever type it is, EvalModule gets mixed in to that instance.

> Second question is why it is called module? We know that Ruby modules
> cannot be instantiated so it is a bit confusing.

EvalModule extends Module. For Module, Ruby gives us a nice keyword:

  module SomeModule
    def some_method; end
  class SomeClass
    include SomeModule

Alternatively, you could say:

  class SomeClass

  mod = Module.new do
    def some_method; end

  SomeClass.send :include, mod

The result is the same. Since we want to use a subclass of Module and
not Module itself, we use the latter form (as Ruby does not provide us
with an evalModule keyword - although we could write one!)

> Maybe EvalContext is
> a better fit?

We used to call Behaviours Contexts, so EvalContext had a conflicting
meaning with that. I guess we could change it now, but I'm not certain
that it would make things more clear.


> Thanks in advance. MK.
> --
> This was freedom. Losing all hope was freedom.
> Chasing the beauty in programming:
> www.ruby-lang.org | www.rubyonrails.org
> _______________________________________________
> rspec-devel mailing list
> rspec-devel at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-devel

More information about the rspec-devel mailing list