basic rails question -


  class person < activerecord::base     validates_uniqueness_of :user_name, :scope => :account_id   end 

my question above 3 lines of code. way i'm c++ programmer , new ruby , rails. i'm very confused line: validates_... what's that? variable definition? function call? or eles. me, it's weird line whithin class definition.

it's function call.

the function defined in module (activerecord::validations). modules in ruby objects , can have functions , variables attached them.

you can include module , it's members. here it's done in parent class: activerecord::base. if @ it's definition starts like:

module activerecord   class base     include activerecord::naming     # ...     include activerecord::validations     #...   end end 

there noticeable differences in way c++ , ruby handles class declaration. firstly, there no compilation in ruby. class definition can changed during runtime. , it's default way of declaring class. in other words: classes in ruby open.

secondly, class body can have executable code. in c++ can't invoke function class definition. wouldn't make sense. yet in ruby makes, since able metaprogramming. way can invoke function constructs members. that's writing functions can write functions. can big productivity boost , source of worst errors @ same time.

i'd highly recommend consulting ruby metaprogramming: declaratively adding methods class entry.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -