Posts tagged Koans

Ah, back to Ruby. Unfortunately, I had to take several days off for personal reasons…or rather, work invading on personal time. Ugh.

But enough of that nonsense.

This day of Ruby is a short one: exceptions and iterations.

Exceptions

I’ll be perfectly honest: I’m lost on this one. I understand parts of it (for example, that particular errors…like StandardError…are subclasses of other errors), but this is a good example where I think the Koans (or probably my brain) come a little short. If I were to stop here, I’d be completely lost when it comes to handling exceptions in Ruby.

Fortunately, the internet exists, and a quick Google reveals that there a dozens of good articles (like this one) about exception handling in Ruby. So I’ve got some reading to do. And you know what? That’s okay. Part of learning is getting frustrated, hitting a wall, and seeking out the answers. I expected this all along, so no biggie.

Iterations

The Iterations Koan is all about looping over arrays and collections (which have the same methods for iterations, btw). As one might exepct, you have most of the standard operations, like “each”, “find”, “select”, etc. The syntax is pretty straightforward:

Today’s excursion into Ruby is a short one. The 3 Koans (constants, control statements, and true/false) are pretty basic. However, upon completing them, I am now officially over 50% done with Ruby Koans!

Of course, I am by no means ready to really start doing anything with Ruby. However, I do feel like I am grasping basic concepts, and my journey through the Koans is becoming less of flailing blindly and more of thinking about what I’ve learned and trying to reason through the challenges I encounter. Granted, not impressive, but it is progress

Constants

The first Koan dealt with the concept of constants. The role of constants in programming is pretty well-understood, so there’s nothing difficult to understand about their usage in Ruby…in fact, the Koan dealt mostly with how to access constants (within and outside of their definitions relative to particular classes). I did find a nice, more in-depth explanation of the role and usage of constants within Ruby. Check it out when you have a chance.

Control Statements

Ah, what would programming be without control (if, else, for, while…) statements? Probably not worth doing!!

A couple cool things I want to point out about control statements in Ruby.

With one significant exception the array assignment koan was pretty straightforward. Dealing with arrays in most languages is pretty straightforward, and the idioms used in Ruby are much of what you’d expect.

A couple interesting things, though.

First, let’s say that you try to access a position in the array that doesn’t exist:

icecream, gummybears = ["Vanilla"]
favorite = gummybears

In this example, I’m trying access the “gummy bears” position within the array. However, it doesn’t exist. Instead of annoyingly erroring out, Ruby returns nil. Remember, nil is an object. So no error, but something useful to deal with. Nice.

But the most interesting part in this koan was the brief example of using the “splat operator.” Before deferring to others for an explanation, here’s the example:

Day 2 of my journey to learn Ruby covers 3 koans: nil, objects, and arrays. If anyone is interested in how I’m running the koan tests, I am simply loading the base-level path_to_enlightenment.rb file from Terminal, and I’m using Xcode to modify the individual koan files. Yes, I said it. I’m using Xcode.

Don’t be a hater. It was the program that opened when I double-clicked the first koan. It seems to work fine for what I’m doing. Will I try other editors? Probably. Are there better ones? I’m sure there are (VIM, maybe?), but I’ll never know which is the best until someone leaves a comment and tells me which one is best!

Alright, enough of that. On to the koans!

Crazy, Interesting Nil

When I saw nil, I thought, “oh, this is NULL”. A keyword, placeholder, something special that represents no-thing. In Ruby, though, nil is interesting, if for no other reason than that it is an object. Yep, an object…definitely not “NULL”.

As an object (e.g., nil.is_a?(Object) == true), nil has its own methods. Consider the following:

So, 11 days into the new year, and I’m actually still going pretty strong with my resolution to make time for learning new (to me) development skills. Yay!

Recently, I was working on a small project where I needed to emulate some of the color transformations from SASS in JavaScript. In order to do this, I needed to dig into the source code, written in Ruby. I was able to suss out what I needed easily enough, but the syntax and approach in Ruby intrigued me. So I figured, what the heck, let’s learn Ruby!

Getting Started

I wanted to start small and easy, so after installing Ruby, I followed the nice and accessible “Ruby in Twenty Minutes” tutorial at the main Ruby site. It’s nothing earth-shattering: your typical “Hello World” turned “Say Hello to Everyone” introduction. However, the tutorial is easy to follow, gives you a nice overview of the some of the syntax conventions that are to come, and (at least for me) leaves you wanting to continue with exploring Ruby.

Where to Go From Here

As with any language, the obligatory “Hello World,” while giving you a taste of the language, does very little in the way of actually teaching you what Ruby is More >