Pete On Software

RSS Feed

Archive for 'Ruby'

Rails for Zombies

Rails for ZombiesOn Thursday of this past week, Chris and I did a brown bag “lunch and learn” at work. Our original plans fell through, so we decided to hit up Rails for Zombies and see what it is about.

I really like the format of the tutorial and the way that the labs are constructed. If you follow the process exactly, you create an account so that they can track your progress so you can return at any time and pick up right where you left off. Then you watch several 5-10 minute videos and then complete an interactive session in your browser after each one.

Here is a screenshot of the first video’s title screen. It definitely helps you get a feel for how *in* to this the good folks at Envy Labs are.

Lab One Video

The goal of the project is that we are building a “Twitter for Zombies” so that they can be all social and stuff. All of the code that is written is around that goal. Here’s what it looks like when you get into the lab.

Lab One Exercise

You can see that after I write my code and submit it, I get instant actual IRB-like feedback. If I were to have made a syntax error, I’d get that back, as well. It seems to be doing true evaluation, as Chris entered some alternative, more advanced ways of performing the labs and it was all evaluated correctly, too.

Lab One Exercise Solved

There is definitely a benefit to not having to set up Ruby and Rails on your machine before you get some exposure to Rails. Just like if I was trying to pick this up by pairing with someone, you don’t start at the beginning with this project. There is no project creation, scaffold generation, database setup, etc. You start in on a project that is kind of already underway.

Several things you do are never really explained and some things you do are explained at the very end (like generating links in the view based on named routes). I supposed I could have suspended belief a little until I got to lab five to learn it, but I ended up asking Chris a lot of questions as we went along.

As I said, I really like this format for teaching and conveying new programming topics to people. I’m really impressed at the work that Envy Labs put in. At the same time, you could complete this entire lab and not know how to even begin your own Rails project. However, if you couple this training with all of the existing Rails tutorials that try to get you up and running in 10 minutes and I think that you could have the knowledge you need to get pretty far into making your own Rails app from your own concepts.

The Greed Ruby Koan

This post is an extension of my last post on the Ruby Koans. Today, I tackled the next Ruby Koan on the block, which was to score dice throws on a simplified version of the game Greed. The first thing I noticed was that the Koans that I got from EdgeCase had a typo in it. It suggested that the following was true:

That first assert should actually total 250, as the 3 2’s are worth 200 and the 5 is worth 50. I searched the web to make sure that I wasn’t crazy and the version of this file checked in to Google Code by Tim Wingfield agreed with me, so I forged on.

The hardest part of this one for me was figuring out the best way to even work this out in English. I play the Farkle game on Facebook quite a bit, so I was really comfortable with scoring the dice, but I just couldn’t figure out a good step-by-step way to do this. I eventually decided on a Hash to tally up the number of each dice present. This choice was made even easier when I found that Hash had an each_pair method that enumerated over the collection.

After that, I tried my best to implement the logic (this code passes the tests, I don’t promise it is bug-free) and have now put this up for criticism. Last time, I learned some great lessons from Nathan Kelley who shared his code in the comments of my last post. If you have any questions, comments, criticisms, or rebukes of this code, let me know and I’d love to hear them.

I definitely am guilty of writing C# in Ruby and had to undo a lot of code where I started to go down that path, so I welcome comments from Ruby enthusiasts on my code.

Ruby Koans

KoansI decided to tackle a little Ruby language learning this weekend. So yesterday, I downloaded the Ruby Koans and went to work. The Koans were created by EdgeCase to help people learn Ruby. Basically, they are a set of unit tests that exercise aspects of Ruby. To begin, all of the tests fail. You have to write code or jump back and forth to the Interactive Ruby Shell (IRB) and get the results of the statements to make the asserts pass.

I went through and did 105 unit tests yesterday, each of them having several parts to them. Having messed around a little with Rails, I was familiar with using IRB and Rake and a little of the syntax. In my opinion, the way this is set up and builds upon itself is really helpful and is a great way to teach Ruby to n00bs. I really salute Jim Weirich and Joe O’Brien for their work in getting these together.

One of the exercises is to create a triangle method that takes three parameters (the sizes of the sides). You then have to decide what kind of triangle it is. After those tests pass, your next assignment is to prevent 0 length or negative sides as well as making sure your triangle validates.

I wrote all the code and passed all the tests except for the triangle validation test. I was having a serious brain cramp and could not remember how you ensured that you’ve gotten the correct sides for any triangle (and not just a right triangle), so I looked for someone else’s example of this code. I found this code:

Okay, I see. The smallest two sides added together need to be greater than the third side. However, I thought that the author of this code greatly over-complicated this process. We are always dealing with a fixed number of sides and the trick is to put them in order and then add away. I figured Ruby had a simple array sort (and they do), so I would place the sides into an array, sort it, and then add the 0 index and 1 index spot and make sure that it was greater than the 2 index spot. Case Closed and (in my opinion) a bit easier to read. Forgiving of course, that I didn’t pass messages in my exceptions, I personally like my code better (of course 😉 ). I also appreciate that Ruby doesn’t mind me putting ( and ) around my if conditions. I just feel safer for them to be contained 😉

As I do these Koans, I’ve been making note of things I’d like to include in future blog entries about things I’ve enjoyed about the language and will be posting them in the near term.

Rails Install Issue

So, I am making a serious go of learning Rails. I have installed it on my Vista machine before, but never really given it a go. So, now that I’ve got a Mac, I decided to try to learn Rails on it. I downloaded the Ruby One-Click Installer and installed it. Per its suggestion, I ran

Then, I got this error

I did some Googling and found out that I had to just run this code

and select the correct packages to include. Then, I just reran

and I was money in the bank.

I hope this helps anyone else who encounters the same issue. Now, on to working through some tutorials.