# What are they, kind of method invoking?

**URL:** <https://www.ruby-forum.com/t/what-are-they-kind-of-method-invoking/108904>\
**Category:** Rails\
**Created:** [July 9, 2007, 9:43am UTC](https://www.ruby-forum.com/t/what-are-they-kind-of-method-invoking/108904 "2007-07-09T09:43:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 9, 2007, 9:43am UTC](https://www.ruby-forum.com/t/what-are-they-kind-of-method-invoking/108904/1 "2007-07-09T09:43:28Z")

</div>

We do(declare?) fields validations like this:

class Dream \< ActiveRecord::Base  
validates\_presence\_of :title, :description  
validates\_length\_of :description, :minimum =\> 10  
end

I know how to use them, and have already used them in my applications,  
but I’m curious about some syntax problem.

I’m new to Ruby and Rails, It may be more a Ruby question than Rails’.  
My question is:  
What are those lines of code ?  
validates\_presence\_of :title, :description  
validates\_length\_of :description, :minimum =\> 10  
Declaration ? Method calling ?

If they’re declaration, how could they come with some parameters ?  
Looks like they’re callings, but how could they exist here in a class  
variable definition area , who are calling them and when are they  
being called ?

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 9, 2007, 10:16am UTC](https://www.ruby-forum.com/t/what-are-they-kind-of-method-invoking/108904/2 "2007-07-09T10:16:31Z")

</div>

On 7/9/07, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> but I’m curious about some syntax problem.  
> variable definition area , who are calling them and when are they  
> being called ?

Believe it or not, those are method calls.

The first thing to remember in Ruby is that a method call does _not_  
require parentheses, so the following two lines are equivalent:

```
validates_presence_of :title, :description
validates_presence_of(:title, :description)

```

The second thing to remember is that Ruby classes are “open” … that  
is, at run time, you can add new method definitions, or change  
existing ones … and that’s the sort of thing that goes on in a lot  
of the magic that Rails supports. This syntactical quirk of Ruby,  
plus a pretty elegant design of helper methods in Rails, gives you a  
feel that you are using a “domain specific language” to specify the  
characteristics of your model objects, even though … deep down …  
it’s just a bunch of Ruby methods provided by Rails.

This doesn’t quite cover _all_ the magic that is going on, however …  
you’ll notice that your ActiveRecord classes can deal with method  
calls like name() or name=() if the underlying table has a “name”  
column. These capabilities are not actually provided by specific  
methods … to understand the nitty gritty details, you’ll have to  
become familiar with Ruby concepts like the method\_missing() method.  
But it’s all open source, and there’s lots of people willing to help  
you figure that stuff out if you want to.

In the mean time, it’s sure fun (coming from a Java background like I  
do) to _not_ have to write getter and setter methods, or any of that  
other stuff that Java frameworks require :-).

Craig McClanahan

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 9, 2007, 10:11am UTC](https://www.ruby-forum.com/t/what-are-they-kind-of-method-invoking/108904/3 "2007-07-09T10:11:30Z")

</div>

On 7/9/07, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> but I’m curious about some syntax problem.  
> variable definition area , who are calling them and when are they  
> being called ?
> 
> >

The validates\_blah\_of methods are class methods. They’re a part of  
the ActiveRecord::Validations::ClassMethods module which gets mixed  
into ActiveRecord::Base. Check out  
[http://api.rubyonrails.com/classes/ActiveRecord/Validations/ClassMethods.html](http://api.rubyonrails.com/classes/ActiveRecord/Validations/ClassMethods.html)  
for a bit more info.

They could have been written validates\_presence\_of(:title,  
:description) as well, but Ruby lets you omit parentheses. In this  
case, omitting the parens makes the code look prettier 🙂 I guess it  
makes it look more declarative or built in rather than being a call.

I’d suggest reading the Pickaxe to fully understand the difference  
between class and instance methods (which will answer your question  
“who’s calling them?”)

Pat

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 20, 2007, 4:59pm UTC](https://www.ruby-forum.com/t/what-are-they-kind-of-method-invoking/108904/4 "2007-07-20T16:59:30Z")

</div>

Thank you guys, your answers are really helpfull, but I still need to  
know more about Ruby to fully understand it.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 20, 2007, 5:22pm UTC](https://www.ruby-forum.com/t/what-are-they-kind-of-method-invoking/108904/5 "2007-07-20T17:22:33Z")

</div>

I found the pre 0.9 way of doing this kind of validations pretty much  
answered my question.  
(pre 0.9):

An example: (validating something I’m not sure.)

class Post \< ActiveRecord::Base  
protected  
def validate  
errors.add\_on\_empty([“title”, “body”])  
errors.add(“title”, “must be at most 10 characters”) if  
title.size \> 10  
end  
end

You see, It is justing adding some error messages to a container.  
and :  
validates\_presence\_of :title, :description  
validates\_length\_of :description, :minimum =\> 10  
as I understand, nearly do the same thing but in Ruby way that is :  
calling a class method whenever a calss instance is created.
