Posted 10.30.2012

I Decided to Look at Ruby on Rails

I’m a PHP girl. I’ve been writing PHP for the past 7 years. I’ve heard so much about Ruby lately that it’s hard not to wonder, “Is it really all it’s hyped up to be?” I’ve been reading plenty of blog posts (here and here) trying to decide if it’s worth investing the time to learn (yet) another programming language. Part of me is eager is to learn something new and is up for a new challenge. I like to think, the more tools I have in my toolbox, the better suited I’ll be for various projects. … So, this is my journey to learning to looking at Ruby and Ruby on Rails. Join me, if you want, we can struggle together.

…yes, the two (Ruby and Ruby on Rails) are different. Ruby is the programming language. Ruby on Rails is the framework that sits on top of Ruby. If I can put it in PHP terms, PHP is the language, CodeIgniter is the framework. Great, so, what’s a framework?

Well, for almost every project, there certain things you always have to do. Websites, for example, I (almost) always have to connect to the database. I could write that code every time, or I could use a framework that has built in functions that do the work for me. A framework gives me structure and a starting place.

While I’m eager to get my hands dirty and jump in with Ruby on Rails, I know I first need to learn Ruby before jumping in the deep end. Rails will make a whole lot more sense and I’ll be able to go a whole lot further in the long run, if I have a foundation first.

I’ve seen The Well Grounded Rubyist mentioned several times on various sites, so I decided to buy the book and start reading. Call me old school, but I prefer books over videos. I like being able to set my own pace, actually see the words, highlight and take notes. I’ve also looked at Code School, but for where I’m at right now, I feel like it moves a little fast and I’m having trouble connecting all the pieces together. I might find it more helpful, though, after I have a few chapters with this book under my belt.


Installing Ruby

I work off a MacBook Pro, running Snow Leopard (I know). It came with Ruby already installed, but it wasn’t the latest version.

I found an excellent blog post by David Benjamin explaining how to get everything up to speed.

I don’t want to be redundant, so I’ll let you click over (just this once) and read his post. –However, I will share a few of my hold ups:

I don’t have a lot of experience with the terminal.

I took a C++ class in college where we had to use the Terminal, so I at least have a basic understanding, but it’s been a while and really, the only commands I still remember are:

cd change directory
pwd display the current path
ln display the contents of the current directory

NetTuts has several posts that will help you get comfortable with the Terminal:

I use Sublime instead of TextMate.

So, instead of using the mate command (as listed in the instructions) to create a new file, I figured out how to call Sublime from the command line:

ln -s "/Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl" ~/bin/subl

So instead of saying mate ~/.profile, I used subl ~/.profile

I had trouble finding the bin folder.

I finally found it. If you go to the root directory > usr directory, it’s there.

Terminal - Finding the bin Folder

With those things out of the way, I was able to easily follow Dan’s instructions.


Understanding the difference between Ruby and ruby

(Apparently) the difference between Ruby and ruby is more than just an upper and lowercase letter. Ruby is the actual language, while ruby is the interpreter.

So, you’ll write Ruby files (with the extension .rb). Then, when you get ready to run the file, you’ll use ruby in the command line to interpret the file for you.


Your first Ruby Program

Create a folder called Ruby for all our code. Mine’s sitting in my Documents folder. Don’t worry, I didn’t do anything fancy like use the Terminal. I simply created it in finder. I’ll keep all my Ruby code there (crazy, I know).

In Sublime, I’ll create a new file and save it to that folder. I’ll call it “first_program.rb”

…BUT… if you did want to be fancy and feel really smart, you could do all this from the Terminal:

Terminal - mkdir for first Ruby program

Inside the file, I’ll type:

puts "I'm writing Ruby"

Save!

Now, in the Terminal, I’ll navigate to that folder. I’ll run the interpreter:

Terminal - ruby first_program.rb

Magic!

…If you don’t see this and got something like this, go back to your file in Sublime and make sure that you have code quotes " " and ' and not fancy ones.
Terminal - error when running ruby first_program.rb

OK, so what just happened in our code?! (It drives me crazy when authors say “Write this.” I write it and they say “Good job!” and I don’t even know what I wrote or what I did! For me, it’s just as much (if not more) about understanding the code, so I can use it in various circumstances in the future, as it is about completing the exercises.

In Ruby puts just means print. Send it to the screen. If you’re a PHP person (like I am), it’s just like echo. The only difference, is that puts adds a line break automatically to the end. So, none of this “\n” junk.

NOTE: Where did they get puts anyway? That doesn’t even sound like good English! Well, it’s an abbreviation for put string.

What if you want some of that “\n” that junk? Well, Ruby has another method you can use called print that doesn’t add “\n” to the end. I’ll show you what I mean. Open your first_program.rb back up. Update it to look like this:

puts "I’m writing Ruby"
print "This is my first program and "
print "it runs like a champ."

Save. Now, back in the Terminal, run your ruby first_program.rb command again.

NOTE: In the Terminal, you can hit the up arrow on your keyboard and it will automatically pull up the last command you entered. Keep hitting the up arrow and it will keep cycling back, showing the command before that. This makes it helpful, especially if you keep running the same commands over and over again.

See what I mean with the difference between puts and print?

Terminal - puts and print


OK, so, this might seem super simplistic, but the important part is we’ve started(!) and that’s something worth celebrating!

Below, I’ve listed a few tutorials that I’ve dabbled in, if you want to jump in a little deeper or move a little faster!

Tutorials