At the Rails Edge conference, one of the speakers mentioned how, as a young programmer, there was no better feeling than to write a program with an enormous LOC count. It made him proud. And I tend to agree. I remember first learning C/C++ and how good it felt to have a rather large program compile successfully. “The more the better,” I thought. Well, that’s hardly the case. Software development entails writing less and less LOC, intentionally. The creator(s) of Rails have made some logical assumptions that have allowed them to extract a lot of the annoying and incessant work, which provides a more focused development process.
As an example, here’s the controller portion for an edit method using the well-known MVC Java framework, Jakarta Struts:
public ActionForward edit(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
PersonForm personForm = (PersonForm) form;
if (personForm.getId() != null) {
PersonManager mgr = (PersonManager) getBean("personManager");
Person person = mgr.getPerson(personForm.getId());
personForm = (PersonForm) convert(person);
updateFormBean(mapping, request, personForm);
}
return mapping.findForward("edit");
}
And here’s the cleaner, simpler Rails version:
1 2 3 | def edit @person = Person.find(params[:id]) end |
Rails takes care of the tedious, repetitive, grunt work—much more than I realize am making note of, here. From this example, to cut the Struts method down to one line, a few assumptions had to be made:
- Most methods are usually public
- Put regularly used objects in scope
- No need for a “manager” (which caused two objects to represent
person, plus conversion code) - No mapping
- It’s a web application, so stuff will fail (remove the explicit
throws Exception)
That makes sense to me. While working with Ruby and Rails, I’ve enforced my desire to do more with less. It’s often more of a challenge to write less code to achieve the same results, but it’s much more fun.
01
Luke on Sat Feb 17 at 01:22PM
Do you mind if I ask a question or two about Ruby on Rails Ryan? I mean, I’m just teaching myself it finally using http://tryruby.hobix.com/ which is about 1000 times more useful than the main site, and it’s got me thinking. Okay …
1: Did you ever learn PHP? Because thats what I know fairly well at the moment, so do you think a transition to Ruby would be easy, especially in terms of high level functions, such as interacting with a database?
2: How did you learn? i.e. books, etc
3: How widespread is the use of Ruby on Rails in actual web design circles, as opposed to ASP or PHP, (might be too hopeful, but any idea about this in the UK)?
Any answers would be seriously appreciated mate, as I’m finally beginning to see what all the fuss is about from that site, so I thought I’d ask the one person who is always writing about it some advice. Cheers!
02
Ryan on Sat Feb 17 at 08:10PM
@Luke – I don’t mind at all. I love talking about the things I enjoy. Especially if it helps out someone else.
Hopefully that answered some of your questions, and feel free to ask any others that you have. I’ll do the best I can answering them, and if I can’t, maybe someone else will! I’m glad to hear you are diving into Ruby on Rails, I think you’ll be pleasantly surprised at how productive and fun it will be.
03
Luke on Sun Feb 18 at 12:18AM
Thank you very much Ryan, all that is really helpful. And looking what you saiding about Ruby being intuitive in your first answer, I know exactly what you mean, I was trying all sorts of commands of thinks, such as .include? on … I think it was a Hash? didnt work, and it really messed up the tutorial, but it was fun anyway to see if it did work.
Another advantage of learning this is now I can sort of understand what your Ruby posts are about haha! Thanks again!
04
Chris on Sun Feb 18 at 05:51AM
@Ryan – Well, I’m glad I can help :-)
@Luke – I agree with Ryan on learning Ruby on its own first – you’ll want to get a copy of the pickaxe – it’s how I learned Ruby. (You can even read the first edition online for free).
And don’t forget the
ricommand – it provides a handy reference at the command line for Ruby classes and methods.