Categories
Uncategorized

Enumerating Enumerable: Enumerable#entries / Enumerable#to_a

Enumerating Enumerable

We’re at the dirty dozen now — this is the twelfth entry in Enumerating Enumerable, my series of articles on the methods in that Ruby workhorse, the Enumerable module. In this article, I cover the entries method, also known as the to_a method. I myself prefer to_a to entries, as its meaning is more obvious.

In case you missed any of the previous articles, they're listed and linked below:

  1. all?
  2. any?
  3. collect / map
  4. count
  5. cycle
  6. detect / find
  7. drop
  8. drop_while
  9. each_cons
  10. each_slice
  11. each_with_index

Enumerable#entries / Enumerable#to_a Quick Summary

In the simplest possible terms Turns any collection into an array.
Ruby version 1.8 and 1.9
Expects Nothing.
Returns An array containing the collection's items.
RubyDoc.org's entry Enumerable#entries / Enumerable#to_a

Enumerable#entries / Enumerable#to_a and Arrays

Converting arrays into arrays isn't terribly useful, but it is possible. Note that the array returned is the same object as the original array!

names = ["alice", "bob", "carol"]
=> ["alice", "bob", "carol"]

new_names = names.to_a
=> ["alice", "bob", "carol"]

# Let's make a change to new_names
new_names[1] = "billy"
=> "billy"

new_names
=> ["alice", "billy", "carol"]

# entries / to_a, when used on an array returns the same object
# as the original array -- names and new_names are the same array!
names
=> ["alice", "billy", "carol"]

Enumerable#entries / Enumerable#to_a and Ranges

entries / to_a converts ranges into ascending-order arrays:

(1..10).to_a
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

 ("aa".."az").to_a
=> ["aa", "ab", "ac", "ad", "ae", "af", "ag", "ah", "ai", "aj", "ak", "al",
"am", "an", "ao", "ap", "aq", "ar", "as", "at", "au", "av", "aw", "ax", "ay", "az"]

Enumerable#entries / Enumerable#to_a and Hashes

When used on a hash, entries / to_a creates an array made up of the items in the hash, converting each hash item into a two-element array, where the first element is the key and the second element is the corresponding value.

names = {:sender => "Alice", :receiver => "Bob", :man_in_the_middle => "Carol"}
=> {:sender=>"Alice", :receiver=>"Bob", :man_in_the_middle=>"Carol"}

names.to_a
=> [[:sender, "Alice"], [:receiver, "Bob"], [:man_in_the_middle, "Carol"]]

Categories
Uncategorized

Build Status Interfaces

Having a continuous integration system is nice, but what’s even nicer is if that system has a really clear way of telling you whether the build is working. Last.fm weren’t happy with build status messages on the command line and went the extra mile to set up these illuminated bears:

Last.fm\'s red, yellow and green \"build bears\"

Last.fm’s Adrian Woodhead writes:

These 3 bears sit in a prominent position and watch our developer’s every move. When things are good we have a green bear gently glowing and purring, when changes are being processed a yellow bear joins the party, and if the build gets broken the growling evil red bear makes an appearance. The developer who broke things usually goes a similar shade of red while frantically trying to fix whatever was broken while the others chortle in the background.

I’ve been meaning to get back into a little hardware hacking (something I haven’t done since I was a teen) and learn how to build computer-driven gizmos like Last.fm’s bears. In the meantime, I’ll have to satisfy myself by page-slapping the development team at b5 with these images created by Big Swinging Developer

You Broke the Build!

\"You broke the build!\" graphic

“Builds on My Machine…”

\"Builds on my machine\" graphic

Where’s the Build?

\"Where\'s the build?\" graphic

Categories
Uncategorized

Enumerating Enumerable: Enumerable#each_with_index

Enumerating Enumerable

Here’s number 11 in the Enumerating Enumerable series, in which I’m trying to outdo RubyDoc.org in their documentation of Ruby’s key Enumerable module. In this article, I cover the each_with_index method.

In case you missed the earlier articles, they’re listed and linked below:

  1. all?
  2. any?
  3. collect / map
  4. count
  5. cycle
  6. detect / find
  7. drop
  8. drop_while
  9. each_cons
  10. each_slice

Enumerable#each_with_index Quick Summary

Graphic representation of the \"each_with_index\" method in Ruby\'s \"Enumerable\" module

In the simplest possible terms Think of each_with_index as a version of each that includes an extra piece of information: a number representing the current iteration’s element’s position in the collection.
Ruby version 1.8 and 1.9
Expects An optional block to receive the values from each iteration.
Returns
  • The original collection, if used with a block.
  • An Enumerator object that outputs the collection’s items and indexes, if used without a block.
RubyDoc.org’s entry Enumerable#each_with_index

Enumerable#each_with_index and Arrays

When used with on an array and given a block, each_with_index iterates through the array, passing each item in the array and the number representing its order in the array to the block. Once again, I’ll use the “Justice League” example:

justice_league = ["Aquaman", "Batman", "Black Canary", \
                  "Flash", "Green Arrow", "Green Lantern", \
                  "Martian Manhunter", "Superman", \
                  "Vixen", "Wonder Woman"]
justice_league = ["Aquaman", "Batman", "Black Canary", "Flash", "Green Arrow",
"Green Lantern", "Martian Manhunter", "Superman", "Vixen", "Wonder Woman"]

justice_league.each_with_index {|member, index| puts "#{index}: #{member}"}
=> 0: Aquaman
1: Batman
2: Black Canary
3: Flash
4: Green Arrow
5: Green Lantern
6: Martian Manhunter
7: Superman
8: Vixen
9: Wonder Woman
=> ["Aquaman", "Batman", "Black Canary", "Flash", "Green Arrow", "Green Lantern",
"Martian Manhunter", "Superman", "Vixen", "Wonder Woman"]

Note that unlike the each_cons and each_slice, each_with_index doesn’t return nil, but the original array, just like each does. each_cons and each_slice are newer methods that were introduced in Ruby 1.9; perhaps Matz and company decided that it made more sense for “each” methods to return nil from now on. each and each_with_index have return the original array as they always have, since making changes to these methods might break a lot of existing Ruby code. Hooray for those little inconsistencies that creep into every programming language!

Enumerable#each_with_index and Hashes

When used with on a hash and given a block, each_with_index iterates through the hash, converting each item in the hash into a two-element array (where the first element is the key and the second element is the corresponding value), and then passing that array and the number representing its order in the original array to the block. Once again, I’ll use the “Enterprise Crew” example:

enterprise_crew = {:captain => "Picard",
                   :first_officer => "Riker",
                   :science_officer => "Data",
                   :tactical_officer => "Worf",
                   :chief_engineer => "LaForge",
                   :chief_medical_officer => "Crusher",
                   :ships_counselor => "Troi",
                   :annoying_ensign => "Crusher",
                   :attractive_ensign => "Ro",
                   :expendable_crew_member => "Smith"}
=> {:captain=>"Picard", :first_officer=>"Riker", :science_officer=>"Data",
:tactical_officer=>"Worf", :chief_engineer=>"LaForge",
:chief_medical_officer=>"Crusher", :ships_counselor=>"Troi",
:annoying_ensign=>"Crusher", :attractive_ensign=>"Ro",
:expendable_crew_member=>"Smith"}

enterprise_crew.each_with_index {|member, index| puts "#{index}: #{member}"}
=> 0: [:captain, "Picard"]
1: [:first_officer, "Riker"]
2: [:science_officer, "Data"]
3: [:tactical_officer, "Worf"]
4: [:chief_engineer, "LaForge"]
5: [:chief_medical_officer, "Crusher"]
6: [:ships_counselor, "Troi"]
7: [:annoying_ensign, "Crusher"]
8: [:attractive_ensign, "Ro"]
9: [:expendable_crew_member, "Smith"]
=> {:captain=>"Picard", :first_officer=>"Riker", :science_officer=>"Data",
:tactical_officer=>"Worf", :chief_engineer=>"LaForge",
:chief_medical_officer=>"Crusher", :ships_counselor=>"Troi",
:annoying_ensign=>"Crusher", :attractive_ensign=>"Ro",
:expendable_crew_member=>"Smith"}

Enumerable#each_with_index Oddities

Blocks with a Single Parameter

What happens if you use each_with_index with a block that has only one parameter? You’d think that the block would accept the item and index passed to it as a two-element array, with the item as the first element and the corresponding index as the second element, but that’s not the case:

# For each item in justice_league, this line will output the following:
# {second letter of member's name} : {first letter of member's name} 
justice_league.each_with_index {|member| puts "#{member[1]}: #{member[0]}"}
=> q: A
a: B
l: B
l: F
r: G
r: G
a: M
u: S
i: V
o: W
=> ["Aquaman", "Batman", "Black Canary", "Flash", "Green Arrow", "Green Lantern"
, "Martian Manhunter", "Superman", "Vixen", "Wonder Woman"]

When each_with_index passes an item and its index to a block that takes only one parameter, the item goes into the paramter, and the index is discarded. Simply put, in this case, each_with_index behaves just like each.

Without a Block

What happens if you use each_with_index without a block to create an Enumerator object that spits out the next item when its next method is called? This:

hero = justice_league.each_with_index
=> #<Enumerable::Enumerator:0x159cb98>

hero.next
=> "Aquaman"

hero.next
=> "Batman"

hero.next
=> "Black Canary"

Note that calling next only yields the next item in the collection; the index information is lost. In this case, each_with_index behaves just like each.

Categories
Uncategorized

RubyFringe was Profitable, People are Happy, and the Sky Didn’t Fall. What Now?”

Collage of images from the RubyFringe summary article at \"Rethink\"

Over at Rethink, the blog of Accordion City-based development shop Unspace, Pete Forde shares his thoughts on the RubyFringe conference in an articles titled RubyFringe was Profitable, People are Happy, and the Sky Didn’t Fall. What Now?”.

The article covers all kinds of things including:

  • A loving poke at RailsConf (“A 400 person conference doesn’t become better with 1600 people, but if you’ve already done the hard work, why not scale up?”). That’s a reference to RailsConf 2006 and 2007.
  • The number of attendees (something that I’m going to cover in an article very soon)
  • Why they might not do another RubyFringe (think of all the movie sequels you’ve ever seen)
  • Women and tech conferences
  • You can hold a conference without sponsors (well, Engine Yard helped foot the bill for a party)
  • Consider going with just a single track
  • Just as Obie said that you shouldn’t undercharge for your services, you shouldn’t undercharge for a conference. Charge what it costs, and deliver real value
  • “Great food is important, because nobody can focus for fifteen hours on cold boxed lunches.” And RubyFringe had great food.
  • Care about the details! “This cannot be overstated, and the key word here is care.”

Meghann Millard of Unspace
Meghann Millard, RubyFringe cat herder supreme.

Pete said it in his article, and I feel it bears repeating: Meghann did an amazing job herding cats for RubyFringe, and if you attended RubyFringe and have a little cash to spare, it might be a nice idea to send her some flowers (or an Amazon gift certificate) for all the work she put in. I owe her big-time for thinking of me when she was looking for a host for the Friday night opening events as well as an emergency host when FAILCamp needed one. Thank you, Meghann! I salute you with a filet mignon on a flaming sword!

As for Pete thanking me for the RubyFringe guides and notes from the conference: it was my pleasure. I believed in the event from the get-go and was only too happy to apply the Burning Man ethos to this event (“There are no spectators, only participants”). Besides, that’s what we in the Accordion City tech community do!

If you’re thinking about putting together a tech conference, you should steal as many ideas as you can from RubyFringe, and Pete’s article is a good starting-off point.

Categories
Uncategorized

“Rails to Victory”

Here’s a still from what I assume is a propaganda film from World War II titled Rails to Victory. If any of you are planning to do presentations covering the topic of Rails and are looking for some graphics for your slides, you might want to consider this one:

Opening shot from the film \"Rails to Victory\"
Click the photo to see a larger version.
Photo courtesy of Miss Fipi Lele.