Learning Ruby – Day 6

Day 6, How to deal with files and other data streams.

What’s a reliable way to get the last item in an array? ask for the -1 item, i.e. array[-1].
Finally, a logical reason to start arrays at 0 rather than 1 – so we can count backwards.

Looks like the “then” part of an “if…then” statement can come first. As in this example from the book, removing a leading zero from a decimal number.
avg[0,1] = '' if avg[0, 2] == "0."

This will take some getting accustomed to. I’m more familiar with it written this way:

if avg[0,2] == "0."
avg[0,1] == ''
end

To accommodate When Bad Things Happen, Ruby has a pretty cool construct:

begin
#...
rescue BadThing1Happened
#...
rescue BadThing2Happened
#...
end

Two cool things here; first – this seems like a simple way to write more readable code – error conditions or otherwise, second – the word “rescue”, makes you feel like a superhero for catching Bad Things. Incentive enough for writing better code.


This post documents my journey through Sam’s Teach Yourself Ruby in 21 days. I’ll be joining Al Abut in his effort to learn Ruby and blog along the way.


Learning Ruby – Day 5

Day 5 is all about objects and their methods.

Remember a few months back when Apple introduced the Shuffle? Everyone was up-in-arms about it’s lack of screen and how it was useless an mp3 player without a screen is. When in-fact, the lack of screen simplifies and improves the device a great deal.

Ruby is like that; elegant, well thought out, useful, challenging convention. Case in point:

Methods names can end with ‘ =’. Conceptually treating a method’s values as variables. It not only saves keystrokes in comparison to the traditional methodName(value) convention – it more accurately maps to what’s being described.

Add to that, a built-in method for creating read & write methods – attr_accessor, an I’m understanding why Ruby is called the Programmers’ Best Friend.


This post documents my journey through Sam’s Teach Yourself Ruby in 21 days. I’ll be joining Al Abut in his effort to learn Ruby and blog along the way.


Learning Ruby – Day 4

Day 4 – Iterators.

First, all chapters in programming books should start with quotes from Steve Martin .

Second, what’s a language without a convoluted loop syntax to geek out on? Geeez. I thought the purpose of learning to program was the same as learning Arabic from the Defense Language Institute – “because you can’t.” Ruby has iterators for logical, readable code? This means I might actually get something built rather than just debugging loops.

I’ve mentioned before how much I enjoy Slagell’s writing. I also like how the solutions to the exercises are listed immediately after the exercises. This isn’t typical, at in my collection of programming books. It makes getting un-stuck and getting the point much quicker.

Tip: Here’s how I’m remembering how the ‘step’ iterator works:

start.step(to, by)

an example to display all the odd numbers from 5 to -15

5.step(-15, -2)

Quite a few years back I was talking with a programmer/designer (in contrast to my designer/programmer) who didn’t like AppleScript because it was “too much like English”. With syntax like:

  • 3.times to do something, well, 3 times and
  • 10.downto(1) to count down from 10 to 1

I don’t think he’d like Ruby for the exact same reason – if not more so.


This post documents my journey through Sam’s Teach Yourself Ruby in 21 days. I’ll be joining Al Abut in his effort to learn Ruby and blog along the way.


PodSafeSound and the SpaceShots Rock

Tonight, listening to my “Getting Things Done” playlist while going through Day 4 of Ruby, the Spaceshots’ tune ‘I Promise the World’ came through the headphones.

I picked up that tune last November, while subscribed to BlogDigger’s mp3 feed (which I highly recommend for 2-3 days at a time). I search for ‘spaceshots’ at iTunes.

Did you mean “spaceshits”?

Umm. No.

Googling for them sends me to PodSafeSound.com – the independent resource for podsafe music. Yes, they have a feed and yes it includes the songs. That’s how to make my day.

Wait. It gets better. I head over to The Spaceshots site and they’re offering the entire album for download, free – in a single zipped file. Same goes for their side project, Alpha Pi.

These guys rock.

First Crack 34. Get Your Own Radio Show By Dave & Huna

Two years after their first demo, Dave and Huna got a real, over-the-air, AM call-in talk radio show on March 6, 2005. They tell you what it was like and how to do it.

More Stuff:

If you want to buy Huna’s car, send an email to firstcrack@gmail.com

Listen to Get Your Own Radio Show By Dave & Huna [31 min]

Got questions about coffee or comments about the show? Call: 206-20-BEAN-1

Like the show? Support the First Crack Podcast

Learning Ruby – Day 3

Day 3 is all about strings, arrays, hashes, and ranges – abstract, geeky terms for buckets

In Ruby, strings can be treated like arrays. Meaning it’s super easy to access substring. For example:

theString = "It's raining today"
theString[13, 5]
# returns ‘today’

Finally, I always thought substr() methods were an awkward solution for something so common.

Replacing substrings is just as easy:

theString["today"] = "still"
# theString is now “it’s raining still”

All this talk about strings reminds me, Ruby knows the alphabet. Makes me smile just thinking about it.
"a".next # returns “b”

I’ve written a couple plugins (WP-CaTT, WP-iCal) for WordPress. Developing each one of them has sent me digging through PHP’s function list more frequently than I’d like – looking for just the right function. I’ve rewritten Perl apps in PHP to make the more readable and maintainable. As I’m learning more about the Ruby syntax, PHP feels like Perl.

Here’s a Super Useful Thing: Ruby can grep arrays. Need the list of months you can eat mussels in, if months_in_year was an array of months, this would work:
months_in_year.grep(/r/) # returns all months with an “r” in their name

I’d like to thank Slagell for explaining the difference between arrays and hashes in a useful way.

Back in day one, we talked about the Principle of Least Surprise. So far, getting through the end-of-chapter exercises has been a matter of applying that principle.

I was scanning the pages in today’s chapter looking for the method to flatten an array. According to the Principle of Least Surprise the method would be ‘flatten’. It is. 🙂

Al Abut’s 3rd day


This post documents my journey through Sam’s Teach Yourself Ruby in 21 days. I’ll be joining Al Abut in his effort to learn Ruby and blog along the way.


First Crack 33. HourCar Brings Car Sharing to Twin Cities

Kurt Fischer, Program Manager for HourCar – Minnesota’s non-profit car sharing venture, and I sat down at Nina’s Coffee on Dale and Western in St. Paul. HourCar is planning to introduce car sharing to Minnesota in early May 2005. We talk about car sharing, who is a good candidate for car sharing, and how car sharing positively impacts a community.

If you’re in the Twin Cities, this might be the easiest way to get your hands on a 2005 Toyota Prius.

Nina’s is an excellent place, old building, great decor, very popular neighborhood hangout. Unfortunately no wireless and noisy.

Links Mentioned:

Listen to HourCar Brings Car Sharing to Twin Cities [32 min]

Got questions about coffee or comments about the show? Call: 206-20-BEAN-1

Like the show? Support the First Crack Podcast

Auto Insurance Companies Gamble Double or Nothing

This morning, NPR reported on a study finding auto insurance rates in No-Fault states 20% higher and rising more quickly than in “Fault” states.

In a No-Fault state, each driver’s insurance pays for their claim. Elsewhere the accident-causing driver’s insurance pays for both claim.

This means insurance companies prefer the double-or-nothing gamble over having to always pay something. Rather than having an incentive to make the roads safer as a whole (No-Fault), insurance companies are betting their customers to never be at fault. The end result – a greater chance the most dangerous drivers have no insurance (no one will carry them or they can’t afford it) and a lower chance your rates will go up. Seems to me, this keeps accident rates steady.