<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">
<br><div><div>On Dec 27, 2007, at 11:30 AM, Corey Haines wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite">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.<br>[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] <br clear="all"><br>I expected to see SharedExampleGroup &lt; ExampleGroup, but, instead, I saw SharedExampleGroup &lt; 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</blockquote><div><br class="webkit-block-placeholder"></div><div>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):</div><div><br class="webkit-block-placeholder"></div><div>When you use the keyword module, you are actually creating a new instance of the Module class.  So these two are equivalent:</div><div><br class="webkit-block-placeholder"></div><div>MyMod = Module.new()</div><div><br class="webkit-block-placeholder"></div><div>module MyMod; end</div><div><br class="webkit-block-placeholder"></div><div>So - Module is a class, but a module is a module (i.e., an instance of the class Module).</div><div><br class="webkit-block-placeholder"></div><div>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.  </div><div><br class="webkit-block-placeholder"></div><div>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.</div><div><br class="webkit-block-placeholder"></div><div>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.</div><div><br class="webkit-block-placeholder"></div><div>Now - for why it was implemented this way, and not another way, David or Aslak could tell you much better than I.  </div><div><br class="webkit-block-placeholder"></div><div>Also - it's likely this implementation will change in the near future.</div><div><br class="webkit-block-placeholder"></div><div>Hope that helps.</div><div><br class="webkit-block-placeholder"></div><div>Scott</div><div><br class="webkit-block-placeholder"></div><blockquote type="cite"> have a hierarchical relationship. <br><br>If there is some hidden meaning, I'd love to hear it. I'll post it as an update to the blog entry, too.<br><br>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. <br><br><p>part 1: <a title="http://www.coreyhaines.com/coreysramblings/2007/12/15/ARubyNewbieLooksThroughRSpecPartIWhatIsThis.aspx" href="http://www.coreyhaines.com/coreysramblings/2007/12/15/ARubyNewbieLooksThroughRSpecPartIWhatIsThis.aspx"> http://www.coreyhaines.com/coreysramblings/2007/12/15/ARubyNewbieLooksThroughRSpecPartIWhatIsThis.aspx</a></p><p>part 2: <a title="http://www.coreyhaines.com/coreysramblings/2007/12/15/ARubyNewbieLooksThroughRSpecPartIIDescribe.aspx" href="http://www.coreyhaines.com/coreysramblings/2007/12/15/ARubyNewbieLooksThroughRSpecPartIIDescribe.aspx"> http://www.coreyhaines.com/coreysramblings/2007/12/15/ARubyNewbieLooksThroughRSpecPartIIDescribe.aspx</a></p><p>part 3: <a title="http://www.coreyhaines.com/coreysramblings/2007/12/22/ARubyNewbieLooksThroughRSpecPartIIIDescribeRedux.aspx" href="http://www.coreyhaines.com/coreysramblings/2007/12/22/ARubyNewbieLooksThroughRSpecPartIIIDescribeRedux.aspx"> http://www.coreyhaines.com/coreysramblings/2007/12/22/ARubyNewbieLooksThroughRSpecPartIIIDescribeRedux.aspx</a></p>part 4: I'm working on this, which is where I noticed the ExampleGroup/SharedExampleGroup thing.<br><br> <br>-Corey<br><br>-- <br><a href="http://www.coreyhaines.com">http://www.coreyhaines.com</a><br>The Internet's Premiere source of information about Corey Haines <div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">_______________________________________________</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">rspec-users mailing list</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><a href="mailto:rspec-users@rubyforge.org">rspec-users@rubyforge.org</a></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><a href="http://rubyforge.org/mailman/listinfo/rspec-users">http://rubyforge.org/mailman/listinfo/rspec-users</a></div> </blockquote></div><br></body></html>