[rspec-users] newbie cucumber tutorial

Jeremiah Heller jeremiah at itmustbe.com
Wed Dec 24 02:11:40 EST 2008

On 23 Dec 2008, at 23:07, Jeremiah Heller wrote:

> On 23 Dec 2008, at 18:26, Sarah Allen wrote:
>> Matt Wynne wrote:
>>> ActiveRecord::Validations uses a trick that's used widely in rails,
>>> and is arguably[1] becoming a Ruby idiom, apparently.
>>> This is the interesting bit, around line #275:
>>>    def self.included(base) # :nodoc:
>>>      base.extend ClassMethods
>>> So basically that means that when the class ActiveRecord::Base
>>> includes the module ActiveRecord::Validations, that module extends  
>>> the
>>> thing which included it (in this case ActiveRecord::Base) with the
>>> class methods defined in ActiveRecords::Validations::ClassMethods.
>>> Does that make sense?
>> um, no.  You say that the class ActiveRecord::Base includes the  
>> module
>> ActiveRecord::Validations, but I don't see that in the code I'm  
>> looking
>> at here:
>> http://dev.rubyonrails.org/browser/branches/2-0-stable/activerecord/lib/active_record/base.rb
> Try here: http://dev.rubyonrails.org/browser/branches/2-0-stable/activerecord/lib/active_record.rb
> On line 42: require 'active_record/validations'

oops... line 59 looks useful as well: include ActiveRecord::Validations

>> If I assume that I'm looking at the wrong code or it happens thru  
>> some
>> magic then you can make a module which will add its class methods  
>> to the
>> class which includes it?
> I believe that's how modules work, but it's not by magic. If I'm  
> reading this Ruby book (by the Pragmatic Programmer's) here  
> correctly then Ruby creates a singleton class of the object calling  
> extend (which I think is the reasoning behind the code referenced by  
> Matt), then includes the module into that singleton class, which  
> adds the instance methods of the module into the superclass chain  
> for that object's class.
>> Is that some kind of trick to create multiple
>> inheritance?  Is there any way without digging thru source to know  
>> which
>> class include methods from other classes or do y'all just get so cozy
>> with every Rails class you use that you "just know?"
> The Ruby classes Module and Object have some methods to determine  
> other included modules, available public|private|protected methods,  
> etc. if that helps.
> Perhaps someone more experienced can fill in any gaps I may've  
> missed - just happened to be reading about this myself...
> Jeremiah

More information about the rspec-users mailing list