Introduction
This year I’ve decided that I want to learn at least one new language and have decided that I’ll be learning Ruby. I’ve read great things about Ruby and Rails, all about how it’s made development fun again for devs, lets you actually get on with creating responsive web applications and is generally just so awesome. I’ll be writing up everything I discover, whether it’s finding things which are much easier than their PHP alternative, or getting frustrated by the fact that I can’t figure out how to do something I’d normally consider trivial in PHP.
Part I is based on me setting up Rails on my local Windows machine and then spending a couple of hours working my way through the Getting Started tutorial.
Ruby VS Ruby On Rails
One of the first things I asked myself was, “am I learning Ruby or Ruby on Rails?” and went off in search for an answer, which was “both”.
Ruby is the language. Rails is a framework written in Ruby which makes creating web applications really easy.
So me learning Rails is actually me learning how create Rails applications in Ruby. Got it?
Setup
The setup of Rails itself wasn’t too bad. I just downloaded the Windows installer from Ruby Installer and the DevKit from the same site, which allows me to compile and use gems natively. That was it for Rails installation, simple enough.
MySQL Setup
To create my first application I simply fired up Netbeans, which is my IDE of choice, and created a new Ruby on Rails Application. This was pretty cool, except for the fact that I couldn’t get MySQL working. I have MySQL running locally as part of WAMP, but installing the mysql gem for Ruby wasn’t enough. Whenever I tried running the app it’d complain and quit.
I started looking up solutions, and spent a lot of time searching for people with similar issues, but no matter what I tried I couldn’t get it working. I spent at least an hour looking for a fix and got really frustrated, it was quite a dark time, however I kept looking and finally found this solution on how to install MySQL on Windows 7 64-bit with Ruby. The tutorial was simple to follow, and as soon as I’d finished my app started working, good times.
Hello Ruby
Now that I had Rails setup and installed on my machine, it was time to start learning how to build things. I went to the getting started tutorial and worked my way through it. As I worked through it I spotted a LOT of things which seemed to make me love Ruby, and lot of things which went well and truly over my head.
Things I Loved
- Sprockets – In your application directory, Rails has assets directories for JavaScript, Stylesheets and Images. Whenever you use the CLI to create a new controller, it creates new .scss and .coffee files with the name of your controller. This means that you can separate out all the style and scripts for each component of your site really easily. Rails then compiles your Sass and CoffeeScript files when your run your application into a single CSS and a single JavaScript file.
- Response types – it was so stupidly easy to return content in a specific format based on the URL extension. For example:
myurl.com/posts – would be the expected HTML output all my blog posts
myurl.com/posts.json – would route to the same controller and action, but return all the blog posts as a JSON string
myurl.com/posts.xml – all the posts but now as valid XML - Rails IS an MVC framework – Rails is built to be an MVC framework. The moment you generate your first app (easy as: `rails new myappname`!) it creates a base application which follows the MVC pattern and easily allows you to extend it into your own.
- Syntax that “just makes sense”– here’s a sample of a Model in Ruby:
class Post < ActiveRecord::Base validates :name, :presence => true validates :title, :presence => true, :length => { :minimum => 5 } has_many :comments end
It amazes at me at how much you can understand just from looking at the class declaration. - It’s So Simple – whilst you could argue that I now have the benefit of a few years PHP experience, I still remember that when I first started with PHP, which was my first web language (I’d done Java and C++ before), I spent a whole summer just going over the basics and working my way up to creating a blog as part of working through a book. In fact, the blog was pretty poor an lacked a lot of key features like security and it was done in procedural programming. It wasn’t until about a year into PHP that I started working with frameworks and OOP properly. In comparison, it took me just a few hours to go from “I want to learn Ruby”, to having a working blog application using MVC conventions.
It’s Over My Head
When I said there were a few things that went well over my head, well…I lied, it was basically just the syntax. Whilst in some cases the syntax made a lot of sense, in other cases I was getting really confused e.g. when they were generating forms in the views and I had no idea what the hell was going on. At the time I decided the best thing to do was to carry on the guide and then go through everything and look up the bits I didn’t get.
This is actually as far as I’ve got to date, but see the next section on how I intend to learn the syntax.
Where To Next?
Whilst I’ve completed the basic blog tutorial, the next step is to learn how to make a full web application. I’ve looked around and found that the book Agile Web Development With Rails is THE book to learn Rails with according to a lot of people. I managed to find an early edition at work today and have brought it home to start looking through for the rest of this evening.
The book has an appendix which contains a quick-start guide to the Ruby language and syntax so I’m going to start with that to understand the things from the blog tutorial which I didn’t quite get, and then make my way through the book which should also clarify some of the things for me.
Verdict.
Simply put, I think Rails is ACE. So far it’s been great. Whilst there’s some bits I don’t quite get yet, well what can you expect, it’s been about 3 hours so far! Overall it seems to be a very powerful framework which not only enforces but also simplifies the best web programming conventions. Lovin’ it.