# Newbie cucumber tutorial

**URL:** <https://www.ruby-forum.com/t/newbie-cucumber-tutorial/156182>\
**Category:** RSpec\
**Created:** [December 22, 2008, 10:01am UTC](https://www.ruby-forum.com/t/newbie-cucumber-tutorial/156182 "2008-12-22T10:01:45Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![sackerson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/59ef9b/32.png) [@sackerson](https://www.ruby-forum.com/u/sackerson)\
**Post date:** [December 22, 2008, 10:01am UTC](https://www.ruby-forum.com/t/newbie-cucumber-tutorial/156182/1 "2008-12-22T10:01:45Z")

</div>

I got started with cucumber and it sure is fun. I’ve written up my  
initial experience in tutorial format here for any newbies who want to  
follow in my tracks:  
[http://www.ultrasaurus.com/code/2008/12/rails-2-day-3.html](http://www.ultrasaurus.com/code/2008/12/rails-2-day-3.html)

If anyone has any corrections, let me know. I was wondering whether  
when writing a real application, do you usually write your whole spec  
with lots of scenarios at once and then get them the execute one at a  
time? or do you write and code one scenario at a time?

Thanks,  
Sarah

p.s. what is the relationship between cucumber and RSpec?

---

<div class="post-metadata">

**Author:** ![sackerson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/59ef9b/32.png) [@sackerson](https://www.ruby-forum.com/u/sackerson)\
**Post date:** [December 22, 2008, 10:49am UTC](https://www.ruby-forum.com/t/newbie-cucumber-tutorial/156182/2 "2008-12-22T10:49:40Z")

</div>

Great stuff.

One thing I’d point out is the missing (and extremely important) step  
3.5 in Rick Denatale’s TDD steps: _Refactor to remove duplication_.

Not that there’s any refactoring necessary in your example, but it’s  
always worth reminding people they should check for it.

On 22 Dec 2008, at 09:01, Sarah A. wrote:

> I got started with cucumber and it sure is fun. I’ve written up my  
> initial experience in tutorial format here for any newbies who want to  
> follow in my tracks:  
> [rails 2 day 3: behavior-driven development](http://www.ultrasaurus.com/code/2008/12/rails-2-day-3.html)
> 
> If anyone has any corrections, let me know. I was wondering whether  
> when writing a real application, do you usually write your whole spec  
> with lots of scenarios at once and then get them the execute one at a  
> time? or do you write and code one scenario at a time?

Mostly I pick off a simple scenario, write it into Cucumber, then dive  
into the code to make it work. Then when I’m back on the surface with  
the scenario passing, I think about the next simplest scenario, write  
that up into a feature, and I’m off again.

Sometimes I keep a list (you can use # to comment lines in  
the .feature file) of scenarios I’m going to write up later, so I have  
a kind of ‘todo list’ but I don’t usually flesh them out into  
executable scenarios with steps until I’m ready to work on them.

> p.s. what is the relationship between cucumber and RSpec?

Short version: RSpec is for unit testing, Cucumber is for acceptance  
testing.

Matt W.

> **[Tea-Driven Development – Matt Wynne taking it one tea at a time](http://blog.mattwynne.net)**

> **[Find the Best Concerts Near You, Tour Dates & Tickets Worldwide | Songkick](https://www.songkick.com)**
>
> Find live music near you, track your favorite artists, get instant concert alerts and buy tickets for all the best upcoming concerts.

---

<div class="post-metadata">

**Author:** ![sackerson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/59ef9b/32.png) [@sackerson](https://www.ruby-forum.com/u/sackerson)\
**Post date:** [December 22, 2008, 5:59pm UTC](https://www.ruby-forum.com/t/newbie-cucumber-tutorial/156182/3 "2008-12-22T17:59:51Z")

</div>

Aslak HellesÃ¸y wrote:

> - I released 0.1.13 yesterday. In the Rails installation wiki page I  
> recommend using my webrat gem. It lets you use response.should  
> have\_selector(…) (You’re not using it in your tutorial, but just in  
> case…)

Luckily I started with cucumber on Sunday just after your release, so  
I’m using that version. What does have\_selector do? (Is there an API  
reference somewhere that I missed?)

> - “Write a Spec” should be “Write a feature” (specs is confusing here  
> because that is what people use RSpec for. describe and it style). There  
> are  
> some other refs to “spec” which should be “feature”.

I changed it to “describe a feature,” since to me “write a feature”  
means writing the code. When I started using cucumber, I thought it was  
part of RSpec. The cucumber .feature files still feel like a spec to me  
and since I’ve never used RSpec, I don’t really appreciate the  
distinction.

> - Use bang! methods when creating records. Otherwise a failure to create  
> will silently pass without you knowing. Example:
> 
> task = Task.new(:description =\> desc) # Lose the semicolon  
> task.save!
> 
> Or simpler:  
> Task.create!(:description =\> desc)

Thanks for the tip. As you can see from my series of blog posts, I’m  
new to Ruby and Rails. I thought that ending a method with ! was a  
naming convention. Reading the humble ruby book it says “Another  
convention to follow is that if the method modifies its receiver in  
place (i.e. the method modifies the object that called it), then it  
should end in an exclamation point” and the Rails doc show examples of  
create without !  
[ActiveRecord::Base](http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M001969) but if  
I make the change you suggest the test passes. I’ve updated the  
tutorial and my code, but I’m confused.

Fun stuff. Thanks for making this!

Sarah

---

<div class="post-metadata">

**Author:** ![sackerson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/59ef9b/32.png) [@sackerson](https://www.ruby-forum.com/u/sackerson)\
**Post date:** [December 22, 2008, 12:15pm UTC](https://www.ruby-forum.com/t/newbie-cucumber-tutorial/156182/4 "2008-12-22T12:15:18Z")

</div>

On Mon, Dec 22, 2008 at 10:01 AM, Sarah A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I got started with cucumber and it sure is fun. I’ve written up my  
> initial experience in tutorial format here for any newbies who want to  
> follow in my tracks:  
> [rails 2 day 3: behavior-driven development](http://www.ultrasaurus.com/code/2008/12/rails-2-day-3.html)
> 
> If anyone has any corrections, let me know. I was wondering whether  
> when writing a real application, do you usually write your whole spec  
> with lots of scenarios at once and then get them the execute one at a  
> time? or do you write and code one scenario at a time?

Hi Sarah,

Thanks for the great writeup. It is simple to follow and explains what’s  
going on really well. I have a few comments:

- I released 0.1.13 yesterday. In the Rails installation wiki page I  
recommend using my webrat gem. It lets you use response.should  
have\_selector(…) (You’re not using it in your tutorial, but just in  
case…)
- “Write a Spec” should be “Write a feature” (specs is confusing here  
because that is what people use RSpec for. describe and it style). There  
are  
some other refs to “spec” which should be “feature”.
- Use bang! methods when creating records. Otherwise a failure to create  
will silently pass without you knowing. Example:

task = Task.new(:description =\> desc) # Lose the semicolon  
task.save!

Or simpler:  
Task.create!(:description =\> desc)

Cheers,  
Aslak

---

<div class="post-metadata">

**Author:** ![sackerson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/59ef9b/32.png) [@sackerson](https://www.ruby-forum.com/u/sackerson)\
**Post date:** [December 22, 2008, 6:25pm UTC](https://www.ruby-forum.com/t/newbie-cucumber-tutorial/156182/5 "2008-12-22T18:25:37Z")

</div>

On Mon, Dec 22, 2008 at 5:59 PM, Sarah A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Aslak HellesÃ¸y wrote:
> 
> > - I released 0.1.13 yesterday. In the Rails installation wiki page I  
> > recommend using my webrat gem. It lets you use response.should  
> > have\_selector(…) (You’re not using it in your tutorial, but just in  
> > case…)
> 
> Luckily I started with cucumber on Sunday just after your release, so  
> I’m using that version. What does have\_selector do? (Is there an API  
> reference somewhere that I missed?)

That’s part of the WebRat API:  
[http://gitrdoc.com/brynary/webrat/tree/master/](http://gitrdoc.com/brynary/webrat/tree/master/)

> > - “Write a Spec” should be “Write a feature” (specs is confusing here  
> > because that is what people use RSpec for. describe and it style). There  
> > are  
> > some other refs to “spec” which should be “feature”.
> 
> I changed it to “describe a feature,” since to me “write a feature”  
> means writing the code.

“Cucumber Feature Description” is probably a better term.

> When I started using cucumber, I thought it was  
> part of RSpec.

It’s not part of RSpec, but grew out of it.

> The cucumber .feature files still feel like a spec to me  
> and since I’ve never used RSpec, I don’t really appreciate the  
> distinction.

They are two different projects. RSpec is for lower levels (objects). A  
“test” in RSpec is called a spec. A “test” in Cucumber is called a  
Feature.

> Thanks for the tip. As you can see from my series of blog posts, I’m  
> new to Ruby and Rails. I thought that ending a method with ! was a  
> naming convention. Reading the humble ruby book it says “Another  
> convention to follow is that if the method modifies its receiver in  
> place (i.e. the method modifies the object that called it), then it  
> should end in an exclamation point” and the Rails doc show examples of  
> create without !  
> [ActiveRecord::Base](http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M001969) but if  
> I make the change you suggest the test passes. I’ve updated the  
> tutorial and my code, but I’m confused.

The #create! method is documented here:

> **[ActiveRecord::Validations::ClassMethods](https://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html#M001902)**

Essentially, #create will never raise an error no matter what you pass  
it,  
and you actually want exceptions for bad input in your tests (step  
definitions).  
Therefore - use #create! (or #save!). In your app, use the non-bang  
methods.

Cheers,  
Aslak

---

<div class="post-metadata">

**Author:** ![sackerson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/59ef9b/32.png) [@sackerson](https://www.ruby-forum.com/u/sackerson)\
**Post date:** [December 22, 2008, 8:09pm UTC](https://www.ruby-forum.com/t/newbie-cucumber-tutorial/156182/6 "2008-12-22T20:09:19Z")

</div>

I realize this is off-topic for the RSpec forum and cucumber tutorial,  
but I’m hoping you’ll enlighten me on this point which is, I guess, more  
of a Ruby language question…

Aslak HellesÃ¸y wrote:

> > As you can see from my series of blog posts, I’m  
> > new to Ruby and Rails. I thought that ending a method with ! was a  
> > naming convention. Reading the humble ruby book it says “Another  
> > convention to follow is that if the method modifies its receiver in  
> > place (i.e. the method modifies the object that called it), then it  
> > should end in an exclamation point” and the Rails doc show examples of  
> > create without !  
> > [ActiveRecord::Base](http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M001969) but if  
> > I make the change you suggest the test passes. I’ve updated the  
> > tutorial and my code, but I’m confused.
> 
> The #create! method is documented here:  
> [ActiveRecord::Validations::ClassMethods](http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html#M001902)
> 
> Essentially, #create will never raise an error no matter what you pass  
> it,  
> and you actually want exceptions for bad input in your tests (step  
> definitions).  
> Therefore - use #create! (or #save!). In your app, use the non-bang  
> methods.

My Task model is simply defined (by the generate scaffold script) as:  
class Task \< ActiveRecord::Base  
end

How is it that I can call Task.create! which is a method of  
ActiveRecord::Validations ?

Thanks in advance,  
Sarah

---

<div class="post-metadata">

**Author:** ![sackerson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/59ef9b/32.png) [@sackerson](https://www.ruby-forum.com/u/sackerson)\
**Post date:** [December 22, 2008, 8:31pm UTC](https://www.ruby-forum.com/t/newbie-cucumber-tutorial/156182/7 "2008-12-22T20:31:10Z")

</div>

On Mon, Dec 22, 2008 at 8:09 PM, Sarah A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > > should end in an exclamation point" and the Rails doc show examples of
> 
> How is it that I can call Task.create! which is a method of  
> ActiveRecord::Validations ?

Because ActiveRecord::Base includes the ActiveRecord::Validations  
module.  
It’s not clear from the RDoc though…

> From active\_record.rb:

ActiveRecord::Base.class\_eval do  
extend ActiveRecord::QueryCache  
include ActiveRecord::Validations  
include ActiveRecord::Locking::Optimistic  
include ActiveRecord::Locking::Pessimistic  
…lots more

Aslak

---

<div class="post-metadata">

**Author:** ![sackerson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/59ef9b/32.png) [@sackerson](https://www.ruby-forum.com/u/sackerson)\
**Post date:** [December 22, 2008, 8:31pm UTC](https://www.ruby-forum.com/t/newbie-cucumber-tutorial/156182/8 "2008-12-22T20:31:09Z")

</div>

* * *

rspec-users mailing list  
removed\_email\_address@domain.invalid  
[http://rubyforge.org/mailman/listinfo/rspec-users](http://rubyforge.org/mailman/listinfo/rspec-users)

---

<div class="post-metadata">

**Author:** ![sackerson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/59ef9b/32.png) [@sackerson](https://www.ruby-forum.com/u/sackerson)\
**Post date:** [December 22, 2008, 9:39pm UTC](https://www.ruby-forum.com/t/newbie-cucumber-tutorial/156182/9 "2008-12-22T21:39:18Z")

</div>

On Mon, Dec 22, 2008 at 11:29 AM, Caius D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> rescue\_from handlers or within the controller actions themselves.  
> rspec-users mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [http://rubyforge.org/mailman/listinfo/rspec-users](http://rubyforge.org/mailman/listinfo/rspec-users)

I’m a “use the non-bang methods in the app, bang methods in tests”  
guy. In the app, I don’t want an exception to be raised, because  
validation failures aren’t exceptional. if @post.save is good enough  
to know what happened. In tests though, I want it to fail fast and  
noisily, so I use bang methods.

Pat

---

<div class="post-metadata">

**Author:** ![sackerson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/59ef9b/32.png) [@sackerson](https://www.ruby-forum.com/u/sackerson)\
**Post date:** [December 22, 2008, 11:55pm UTC](https://www.ruby-forum.com/t/newbie-cucumber-tutorial/156182/10 "2008-12-22T23:55:55Z")

</div>

+1 @ Pat

I was going to respond in more detail, but I do exactly what Pat does –  
bang in steps, no bang in Rails apps. The Rails scaffolding boiler  
plate  
generates no bangs.  
Steve

---

<div class="post-metadata">

**Author:** ![sackerson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/59ef9b/32.png) [@sackerson](https://www.ruby-forum.com/u/sackerson)\
**Post date:** [December 23, 2008, 1:03am UTC](https://www.ruby-forum.com/t/newbie-cucumber-tutorial/156182/11 "2008-12-23T01:03:51Z")

</div>

On 22 Dec 2008, at 22:57, Sarah A. wrote:

> Oddly, I didn’t see any content to the post by Caius D. via  
> ruby-forum (  
> [http://www.ruby-forum.com/topic/174015?reply\_to=762550#762530](http://www.ruby-forum.com/topic/174015?reply_to=762550#762530) )

How weird, wonder if its because I signed the email with my GPG key.  
Have sent this one unsigned so lets see if the content shows up for  
this one…

## C

Caius D.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
+44 (0) 7960 268 100

> **[Caius Durling » Profile](http://caius.name/)**

---

<div class="post-metadata">

**Author:** ![sackerson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/59ef9b/32.png) [@sackerson](https://www.ruby-forum.com/u/sackerson)\
**Post date:** [December 22, 2008, 11:57pm UTC](https://www.ruby-forum.com/t/newbie-cucumber-tutorial/156182/12 "2008-12-22T23:57:00Z")

</div>

You guys are awesome. For posterity, I’ve written up a bit about the  
bang vs. non-bang as an aside in the tutorial:  
[http://www.ultrasaurus.com/code/2008/12/rails-2-day-3.html#syntax](http://www.ultrasaurus.com/code/2008/12/rails-2-day-3.html#syntax)

I’ll have to read up on modules. (I’ve only just finished ch 3 of the  
humble ruby book.)

Oddly, I didn’t see any content to the post by Caius D. via  
ruby-forum (  
[http://www.ruby-forum.com/topic/174015?reply\_to=762550#762530](http://www.ruby-forum.com/topic/174015?reply_to=762550#762530) )

Warm regards and happy thank yous,  
Sarah

---

<div class="post-metadata">

**Author:** ![sackerson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/59ef9b/32.png) [@sackerson](https://www.ruby-forum.com/u/sackerson)\
**Post date:** [December 23, 2008, 9:00pm UTC](https://www.ruby-forum.com/t/newbie-cucumber-tutorial/156182/13 "2008-12-23T21:00:00Z")

</div>

On 22 Dec 2008, at 19:09, Sarah A. wrote:

> ActiveRecord::Validations ?  
> 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?

I highly recommend reading David A Black’s ‘Ruby for Rails’ at this  
point in your learnings. It did me a world of good when I was  
similarly new to Ruby. It’s a really good deep grounding in Ruby IMO.

[1][Jay Fields' Thoughts: Ruby: instance and class methods from a module](http://blog.jayfields.com/2006/12/ruby-instance-and-class-methods-from.html)

Matt W.

> **[Tea-Driven Development – Matt Wynne taking it one tea at a time](http://blog.mattwynne.net)**

> **[Find the Best Concerts Near You, Tour Dates & Tickets Worldwide | Songkick](https://www.songkick.com)**
>
> Find live music near you, track your favorite artists, get instant concert alerts and buy tickets for all the best upcoming concerts.

---

<div class="post-metadata">

**Author:** ![sackerson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/59ef9b/32.png) [@sackerson](https://www.ruby-forum.com/u/sackerson)\
**Post date:** [December 24, 2008, 12:12am UTC](https://www.ruby-forum.com/t/newbie-cucumber-tutorial/156182/14 "2008-12-24T00:12:50Z")

</div>

Very nice indeed…

In your blog you said:

“Note that one of the steps is already defined in webrat. Isn’t that  
cool? As you get the hang of this, you reuse certain word patterns  
which map to specific tests. But we’re getting ahead of ourselves. We  
need to dive into the creation of “steps” which make up our executable  
spec. Cucumber gives a some handy snippets to get us started (in the  
output of “rake features” above). We’ll paste these into a new file  
that we’ll create in the “features/step\_definitions” directory”

Why did one of the steps already get defined in webrat? Does it parse  
“see” as indicative of UI? or what…?

Thanks very much.

Tim

---

<div class="post-metadata">

**Author:** ![sackerson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/59ef9b/32.png) [@sackerson](https://www.ruby-forum.com/u/sackerson)\
**Post date:** [December 24, 2008, 12:42am UTC](https://www.ruby-forum.com/t/newbie-cucumber-tutorial/156182/15 "2008-12-24T00:42:08Z")

</div>

On Wed, Dec 24, 2008 at 12:10 AM, Tim W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> output of “rake features” above). We’ll paste these into a new file  
> that we’ll create in the “features/step\_definitions” directory"
> 
> Why did one of the steps already get defined in webrat? Does it parse  
> “see” as indicative of UI? or what…?

I try to only use magic when reality doesn’t work 😉  
When you run “script/generate cucumber” you get a webrat\_steps.rb file  
that  
contains these common steps.

Aslak

---

<div class="post-metadata">

**Author:** ![sackerson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/59ef9b/32.png) [@sackerson](https://www.ruby-forum.com/u/sackerson)\
**Post date:** [December 24, 2008, 12:58am UTC](https://www.ruby-forum.com/t/newbie-cucumber-tutorial/156182/16 "2008-12-24T00:58:31Z")

</div>

Ahh…reality is overrated.

Thanks again for your help guys. Making a lot of sense. If I could get  
rcumber working that’s be awesome.

Tim

On Tue, Dec 23, 2008 at 4:39 PM, aslak hellesoy

---

<div class="post-metadata">

**Author:** ![sackerson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/59ef9b/32.png) [@sackerson](https://www.ruby-forum.com/u/sackerson)\
**Post date:** [December 24, 2008, 8:10am UTC](https://www.ruby-forum.com/t/newbie-cucumber-tutorial/156182/17 "2008-12-24T08:10:03Z")

</div>

On 23 Dec 2008, at 18:26, Sarah A. wrote:

> > includes the module ActiveRecord::Validations, that module extends  
> > [http://dev.rubyonrails.org/browser/branches/2-0-stable/activerecord/lib/active\_record/base.rb](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](http://dev.rubyonrails.org/browser/branches/2-0-stable/activerecord/lib/active_record.rb)  
> > On line 42: require ‘active\_record/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

---

<div class="post-metadata">

**Author:** ![sackerson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/59ef9b/32.png) [@sackerson](https://www.ruby-forum.com/u/sackerson)\
**Post date:** [December 24, 2008, 8:14am UTC](https://www.ruby-forum.com/t/newbie-cucumber-tutorial/156182/18 "2008-12-24T08:14:44Z")

</div>

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

> > > 
> > 
> > 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](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](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

---

<div class="post-metadata">

**Author:** ![sackerson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/59ef9b/32.png) [@sackerson](https://www.ruby-forum.com/u/sackerson)\
**Post date:** [December 24, 2008, 3:26am UTC](https://www.ruby-forum.com/t/newbie-cucumber-tutorial/156182/19 "2008-12-24T03:26:37Z")

</div>

Matt W. 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](http://dev.rubyonrails.org/browser/branches/2-0-stable/activerecord/lib/active_record/base.rb)

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? 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?”

> I highly recommend reading David A Black’s ‘Ruby for Rails’ at this  
> point in your learnings. It did me a world of good when I was  
> similarly new to Ruby. It’s a really good deep grounding in Ruby IMO.

Just ordered a second-hand copy online. Thanks for the recommendation.

> [1][http://blog.jayfields.com/2006/12/ruby-instance-and-class-methods-from.html](http://blog.jayfields.com/2006/12/ruby-instance-and-class-methods-from.html)  
> Nice reference, but I didn’t fully follow that either and posted a  
> similar question there.

Sarah

p.s Good question Tim on the webrat magic. I added a note to the  
tutorial that explains where the step was defined.

---

<div class="post-metadata">

**Author:** ![sackerson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/59ef9b/32.png) [@sackerson](https://www.ruby-forum.com/u/sackerson)\
**Post date:** [December 24, 2008, 10:41am UTC](https://www.ruby-forum.com/t/newbie-cucumber-tutorial/156182/20 "2008-12-24T10:41:03Z")

</div>

On 24 Dec 2008, at 02:26, Sarah A. wrote:

> > includes the module ActiveRecord::Validations, that module extends  
> > [http://dev.rubyonrails.org/browser/branches/2-0-stable/activerecord/lib/active\_record/base.rb](http://dev.rubyonrails.org/browser/branches/2-0-stable/activerecord/lib/active_record/base.rb)  
> > You almost got it. Try here instead:  
> > [http://dev.rubyonrails.org/browser/branches/2-0-stable/activerecord/lib/active\_record.rb](http://dev.rubyonrails.org/browser/branches/2-0-stable/activerecord/lib/active_record.rb)

> 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?

Yes, that’s basically what the self.included trick does. As I say,  
it’s debatable whether this is a neat trick or a horrible hack. If you  
were reading the code that included the module,. might have only  
expected that would add instance methods, rather than class methods as  
well.

> Is that some kind of trick to create multiple  
> inheritance?

Not really in that particular case. Ruby modules generally allow you  
to do multiple inheritance by ‘mixing in’ behaviour to classes, but  
that would be when you use ‘include’ to bring the methods defined in  
the module into the class itself.

> 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?”

If you have hold of a class in ruby, there are ways to ask it what  
modules / classes it inherits from, but I can’t remember them off the  
top of my head, sorry. Try messing around in irb for this kind of thing.

Matt W.

> **[Tea-Driven Development – Matt Wynne taking it one tea at a time](http://blog.mattwynne.net)**

> **[Find the Best Concerts Near You, Tour Dates & Tickets Worldwide | Songkick](https://www.songkick.com)**
>
> Find live music near you, track your favorite artists, get instant concert alerts and buy tickets for all the best upcoming concerts.

[Next page](https://www.ruby-forum.com/t/newbie-cucumber-tutorial/156182.md?page=2)
