On Dec 27, 2007, at 11:30 AM, Corey Haines wrote:
I'm working on a series going over the source code for rspec, and I
ran into something interesting with ExampleGroup and
SharedExampleGroup. I was wondering if anyone could shed light on it.
[NOTE: I'm working through the code for my own edification in
learning Ruby. Ruby has some features that I think are incredibly
cool, so I'm using a concrete implementation (RSpec) as a learning
tool to see them implemented. Along the way, I figured I would ask
questions that were confusing to me. So, please bear with me and
please please please do not take this as criticisms/attacks]
I expected to see SharedExampleGroup < ExampleGroup, but, instead,
I saw SharedExampleGroup < Module. This is incredibly confusing to
me. I realize that they don't even need to have any relationship to
each other since they get their shared functionality through the
module ExampleGroupMethods, but it still seems a bit odd to me that
they don't
Yep - it certainly was surprising to me too. Here's some explanation
(although not on the design decision front - just on the Ruby, how it
works front):
When you use the keyword module, you are actually creating a new
instance of the Module class. So these two are equivalent:
MyMod = Module.new()
module MyMod; end
So - Module is a class, but a module is a module (i.e., an instance
of the class Module).
The SharedExampleGroup is a class, which decends from the Module
class. This, in effect, makes a new SharedExampleGroup instance act
like a typical module (it is a kind_of?(Module)) - just as if you had
declared it with Module.new or the keyword.
The advantage of using the approach of instantiating over using the
keyword is that the keyword always requires a constant to be defined
- on the other hand, Module.new doesn't need a constant, and can be
passed around as a regular object without needing to define these
constants on the fly.
Since the SharedExampleGroup is a subclass of Module, the instances
of SharedExampleGroup can be included just as a regular module can
be, into the regular old ExampleGroup (this is what happens when you
say it_should_behave_like "..." - the instances methods of the
SharedExampleGroup are copied directly.
Now - for why it was implemented this way, and not another way, David
or Aslak could tell you much better than I.
Also - it's likely this implementation will change in the near future.
Hope that helps.
Scott
have a hierarchical relationship.
If there is some hidden meaning, I'd love to hear it. I'll post it
as an update to the blog entry, too.
Oh, and here are the links to the first few parts of the series if
anyone is interested: It looks like I'll be able to get about a
post a week on it.
part 1: http://www.coreyhaines.com/coreysramblings/2007/12/15/
ARubyNewbieLooksThroughRSpecPartIWhatIsThis.aspx
part 2: http://www.coreyhaines.com/coreysramblings/2007/12/15/
ARubyNewbieLooksThroughRSpecPartIIDescribe.aspx
part 3: http://www.coreyhaines.com/coreysramblings/2007/12/22/
ARubyNewbieLooksThroughRSpecPartIIIDescribeRedux.aspx
part 4: I'm working on this, which is where I noticed the
ExampleGroup/SharedExampleGroup thing.
-Corey
--
http://www.coreyhaines.com
The Internet's Premiere source of information about Corey Haines
_______________________________________________
rspec-users mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/rspec-users
_______________________________________________
rspec-users mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/rspec-users