[rspec-users] Evaluating shared example customisation block before shared block

Myron Marston myron.marston at gmail.com
Sun Aug 1 23:08:32 EDT 2010


OK, I tried to implement #module_exec on ruby 1.8.6, and here's what I
came up with:

http://github.com/myronmarston/rspec-core/commit/364f20ebd5b7d9612227cb6e86a6e8c8c2e9931e

It works (at least in the sense that it allows the specs and features
I added in the previous commits in that branch to pass on ruby 1.8.6),
but I don't think it's correct.  It just calls #instance_exec, but
instance_exec and module_exec are not the same (at least as I
understand them...).  However, I based that implementation on rubinius
1.0.1's:

http://github.com/evanphx/rubinius/blob/release-1.0.1/kernel/common/module.rb#L438-441

Backports (a library that implements features of later versions of
ruby in 1.8.6) implements it in a similar fashion:

http://github.com/marcandre/backports/blob/v1.18.1/lib/backports/1.8.7/module.rb

Unfortunately, rubyspec doesn't provide much help in defining how
module_exec should work:

http://github.com/rubyspec/rubyspec/blob/master/core/module/module_exec_spec.rb

I'd need some concrete examples demonstrating how module_exec should
work (as compared to instance_exec) to implement it correctly.  My
understanding is that they are identical except in regards to method
definitions--def's within an instance_exec define methods directly on
the Module object instance, whereas def's within a module_exec define
instance methods in the module (i.e. methods that will be added to any
class that includes the module).

One side issue I discovered is that instance_exec is implemented in
rspec-expectations, but not rspec-core (which, incidentally, is why I
linked to the cucumber implementation; I grepped in rspec-core and
couldn't find it).  instance_exec is already used in rspec-core:

http://github.com/rspec/rspec-core/blob/v2.0.0.beta.19/lib/rspec/core/example_group.rb#L179

It needs to be implemented in rspec-core if you want rspec-core to be
able to be used on 1.8.6 without rspec-expectations.

Myron


More information about the rspec-users mailing list