# How to write RoR code

**URL:** <https://www.ruby-forum.com/t/how-to-write-ror-code/100471>\
**Category:** Rails\
**Created:** [July 29, 2006, 4:54am UTC](https://www.ruby-forum.com/t/how-to-write-ror-code/100471 "2006-07-29T04:54:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jsn1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7ea924/32.png) [@jsn1](https://www.ruby-forum.com/u/jsn1)\
**Post date:** [July 29, 2006, 4:54am UTC](https://www.ruby-forum.com/t/how-to-write-ror-code/100471/1 "2006-07-29T04:54:19Z")

</div>

Hi all

I took a look to several examples of RoR on different web sites, and I  
found that it is hard to write a class, in RoR, because I do not  
understand all the codes used on the source code.

jsn

---

<div class="post-metadata">

**Author:** ![jsn1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7ea924/32.png) [@jsn1](https://www.ruby-forum.com/u/jsn1)\
**Post date:** [July 30, 2006, 1:40am UTC](https://www.ruby-forum.com/t/how-to-write-ror-code/100471/2 "2006-07-30T01:40:33Z")

</div>

I highly suggest the books that the others have mentioned, but if you  
are not ready to spend any money yet you can get started with the old  
version of Programming Ruby which is free online at  
[http://www.rubycentral.com/book/index.html](http://www.rubycentral.com/book/index.html)

I got started there and it really helped me to get a handle on how  
ruby works before I was willing to take the plunge and buy a bunch of  
books on RoR.

Read the chapter on classes  
([http://www.rubycentral.com/book/tut\_classes.html](http://www.rubycentral.com/book/tut_classes.html)) and that should  
give you an idea of how to write a class. Most rails classes look  
strange because they inherit from ActiveRecord::Base or  
ApplicationController so there is a lot going on in those parent  
classes that you don’t see, but you can look at the documentation at  
[http://rubyonrails.org/api/](http://rubyonrails.org/api/) to see what’s going on.

I hope this helps,  
Jake

---

<div class="post-metadata">

**Author:** ![jsn1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7ea924/32.png) [@jsn1](https://www.ruby-forum.com/u/jsn1)\
**Post date:** [July 30, 2006, 3:02am UTC](https://www.ruby-forum.com/t/how-to-write-ror-code/100471/3 "2006-07-30T03:02:23Z")

</div>

I’m brand new myself to Ruby and Rails and even programming. I  
started with AWDWR and ran into problems about understanding the code.  
I got David’s book Ruby for Rails (Manning) and have been going  
through that for the last week. I’m kind of curious to know if the  
book should be sufficient enough to get me going in using Rails ?  
A few things I noticed, and correct me if I’m wrong, but it seems a  
Rails web app would not be as “complex” as a Ruby program. Maybe this  
has something to do with the framework itself and Active Record, etc.  
On the other hand I think if your having to learn Ruby and Rails you  
now have another layer of complexity, terms and techniques to soak in.  
Another thing the I’m curious about and I realize everyone’s evolution  
is different but it seems that many of the Rails gurus have a pretty  
good background / experience(years) in Ruby alone. At the same time I  
imagine that people are coming to Ruby and Rails specifically because  
they want to learn and use Rails. I’m just wondering if that is really  
practical.

Stuart

---

<div class="post-metadata">

**Author:** ![jsn1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7ea924/32.png) [@jsn1](https://www.ruby-forum.com/u/jsn1)\
**Post date:** [July 30, 2006, 5:20am UTC](https://www.ruby-forum.com/t/how-to-write-ror-code/100471/4 "2006-07-30T05:20:19Z")

</div>

Stuart Fellowes wrote:

> Another thing the I’m curious about and I realize everyone’s evolution  
> is different but it seems that many of the Rails gurus have a pretty  
> good background / experience(years) in Ruby alone. At the same time I  
> imagine that people are coming to Ruby and Rails specifically because  
> they want to learn and use Rails. I’m just wondering if that is really  
> practical.

Stuart, your mileage may vary but I did not know Ruby when I discovered  
RoR 3 months ago. I knew Ruby was a language invented in Japan and used  
to do OO scripting. I thought it was “like Python only more obscure.”  
I never read a line of it before. I did not even do a demo application  
or create the examples in the book. I just watched the screencasts,  
skimmed the books and started re-writing portions of a large PHP app,  
learning as I went along. I am about a couple weeks away from  
production now.

This is possible if you have a good background in several different  
programming languages and web frameworks. The superiority of RoR is  
clear if you have been around and approach it with an open mind. Some  
additional time may be required to absorb new concepts if you are new to  
MVC (model/view/controller pattern), RoR’s philosophies (DRY, REST, TDD,  
etc.), or if you are accustomed to a single vendor environment.

Do not let the new language intimidate you. Ruby is very simple to get  
started and it reads very well. Definitely get the Pickaxe book as a  
Ruby tutorial and reference. The PDF version will come in handy. I use  
the reference in the appendix about every 10 minutes when I’m coding.  
Between that and the [api.rubyonrails.com](http://api.rubyonrails.com), you should have everything you  
need to get started. As you go along, you will discover the language  
has all kinds of neat features.

Personally, I like that so much is unfamiliar. It keeps things fresh  
and interesting.

Steven

---

<div class="post-metadata">

**Author:** ![jsn1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7ea924/32.png) [@jsn1](https://www.ruby-forum.com/u/jsn1)\
**Post date:** [July 30, 2006, 6:58pm UTC](https://www.ruby-forum.com/t/how-to-write-ror-code/100471/5 "2006-07-30T18:58:04Z")

</div>

While the advice all of you have given is quite good I think it’s  
missing  
the point a little. Let me explain:

It’s obviously trivial to literally write a class for rails

class Foo \< ActiveRecord::Base

end

What’s not trivial is understanding how this class, which is completely  
devoid of content, can possibly have so many variables and methods you  
can  
interact with. Which even inheritence doesn’t explain well. How can you  
possibly inherit a variable from a piece of code written long before  
yours  
that somehow magically has the name of the variables in your database.

The problem here, I suspect, is that the original poster doesn’t  
understand  
the magic that is Ruby’s ability to alter classes at runtime or the  
trickery  
of the method\_missing method. But, the thing is, you don’t NEED to  
understand that at all in order to use Rails. You just have to accept  
that  
code magic happens and that there will be a variable for every column in  
the  
corresponding table in the database and there will be all your standard  
CRUD  
methods for affecting the object itself.

Yes, these magical insertions at runtime make it harder to know what  
exactly  
is available for you to call but you’ll gradually understand it as you  
go.  
Just keep a browser window open with [http://api.rubyonrails.org](http://api.rubyonrails.org) open.

I realize programmers like to have everything spelled out for them in  
their  
code but sometimes it’s best to just have faith that things will work  
out  
while you work towards enlightenment.

-kate = masukomi
