Inspired by my earlier post on the Accordion Guy blog titled The Way Movies Were in the Seventies, I present to you this image showing the way computers were in the seventies:
Illustrations courtesy of Miss Fipi Lele.
Inspired by my earlier post on the Accordion Guy blog titled The Way Movies Were in the Seventies, I present to you this image showing the way computers were in the seventies:
Illustrations courtesy of Miss Fipi Lele.
Can you identify the ’80s-era games depicted in Scott Campbell’s piece, Great Showdowns (of the 8-bit Era)?
While cleaning out my home office, I found a box books that I’ve been hanging on to for historical purposes and sentimental reasons.
First, this collection of Apple ][ manuals, back from when I had my first computer, an Apple //e. These manuals came with the original apple ][; my //e manuals must be kicking around somewhere…
And from my university days, these books: the Adele Goldberg Smalltalk-80 series, Computer Systems in Business, which is rather quaint when read with today’s eyes, and Inside Macintosh from the System 7/8 era:
I know I’ve got Lubomir Bic’s The Logical Design of Operating Systems — from which I learned concurrent programming — stashed away, but I can’t seem to find it at the moment.
I was inspired by yesterday’s posting of the office space used by people developing the Boeing 787 Dreamliner to post photos of b5media’s tech team office. This is the old b5media office; the rest of the team — operations, content, marketing and advertising — are in an office across the hall.
The space is looking pretty zen right now, but I kind of like it. We’ll fill it up eventually.
You can click any of the thumbnails below to see the full-size photo:
Welcome to another installment of Enumerating Enumerable, my series of articles in I attempt to do a better job of documenting Ruby’s Enumerable
module than Ruby-Doc.org. In this installment, I cover the first
method.
In case you missed any of the previous articles, they’re listed and linked below:
In the simplest possible terms | What are the first n items in the collection? |
---|---|
Ruby version | 1.8 and 1.9 |
Expects | An optional integer n that specifies the first n items of the collection to return. If this integer is not given, n is 1 by default. |
Returns | If first is applied to a collection containing m elements:
|
RubyDoc.org’s entry | Enumerable#first |
When used on an array without an argument, first
returns the first item in the array:
posts = ["First post!", "Second post!", "Third post!"] => ["First post!", "Second post!", "Third post!"] # What's the first item in posts? posts.first => "First post!" # Here's the equivalent using array notation: posts[0] => "First post!"
When used on an array with an integer argument n, first
returns an array containing the first n items in the original array:
# What are the first 2 items in posts? posts.first 2 => ["First post!", "Second post!"] # Note that when you provide an argument of 1, # the result is still an array -- with just one element. # If you want a scalar, don't use an argument. posts.first 1 => ["First post!"] # Here's the equivalent using array slice notation: posts[0..1] => ["First post!", "Second post!"] posts[0...2] => ["First post!", "Second post!"]
When used on an empty array, first
returns:
nil
if no argument n is provided[]
, if an argument n is provided[].first => nil [].first 2 => []
In Ruby 1.8 and previous versions, hash order is seemingly arbitrary. Starting with Ruby 1.9, hashes retain the order in which they were defined, which makes the first
method a little more applicable.
When used on a hash without an argument, first
returns the first item in the hash as a two-element array, with the key as the first element and the corresponding value as the second element.
# Let's see what stages of partying our friends are in party_stages = {"Alice" => :party_mineral, "Bob" => :party_animal, "Carol" => :party_reptile, "Dave" => :party_vegetable} => {"Alice"=>:party_mineral, "Bob"=>:party_animal, "Carol"=>:party_reptile, "Dave"=>:party_vegetable} # Who's the first partier and what state is s/he in? party_stages.first => ["Alice", :party_mineral]
When used on a hash with an integer argument n, first
returns an array containing the first n items in the hash, with each item represented as a two-element array:
party_stages.first 2 => [["Alice", :party_mineral], ["Bob", :party_animal]] # Note that when you provide an argument of 1, # the result is still an array -- with just one element. # If you want a scalar, don't use an argument. party_stages.first 1 => [["Alice", :party_mineral]]
When used on an empty hash, first
returns:
nil
if no argument n is provided[]
, if an argument n is provided{}.first => nil {}.first 2 => []
I used to envy the office space that local Ruby heroes Unspace have (they’re just down the street from b5), but now I envy the office space used by the people working on the Boeing 787 Dreamliner:
Click to see a larger version.
Photo courtesy of Miss Fipi Lele.
Once again, it’s Enumerating Enumerable, my series of articles in which I attempt to outdo Ruby-Doc.org’s documentation of Ruby’s Enumerable
module. In this article, I cover the find_index
method, which was introduced in Ruby 1.9.
In case you missed any of the previous articles, they’re listed and linked below:
In the simplest possible terms | What’s the index of the first item in the collection that meets the given criteria? |
---|---|
Ruby version | 1.9 |
Expects | A block containing the criteria. |
Returns |
|
RubyDoc.org’s entry | Enumerable#find_index |
When used on an array, find_index
passes each item in the array to the given block and either:
true
(that is, anything that isn’t false
or nil
) and returns the index of that item, ornil
if there is no item in the array that causes the block to return a value that evaluates to true
.Some examples:
# How about an array of the name of the first cosmonauts and astronauts, # listed in the chronological order of the missions? mission_leaders = ["Gagarin", "Shepard", "Grissom", "Titov", "Glenn", "Carpenter", "Nikolayev", "Popovich"] => ["Gagarin", "Shepard", "Grissom", "Titov", "Glenn", "Carpenter", "Nikolayev", "Popovich"] # Yuri Gagarin was the first in space mission_leaders.find_index{|leader| leader == "Gagarin"} => 0 # John Glenn was the fifth mission_leaders.find_index{|leader| leader == "Glenn"} => 4 # And James Tiberius Kirk is not listed in the array kirk_present = mission_leaders.find_index{|leader| leader == "Kirk"} => nil
When used on a hash, find_index
passes each key/value pair in the hash to the block, which you can “catch” as either:
As with arrays, find_index
:
true
(that is, anything that isn’t false
or nil
) and returns the index of that item, ornil
if there is no item in the array that causes the block to return a value that evaluates to true
.Some examples:
require 'date' => true # These are the names of the first manned spaceships and their launch dates launch_dates = {"Kedr" => Date.new(1961, 4, 12), "Freedom 7" => Date.new(1961, 5, 5), "Liberty Bell 7" => Date.new(1961, 7, 21), "Orel" => Date.new(1961, 8, 6), "Friendship 7" => Date.new(1962, 2, 20), "Aurora 7" => Date.new(1962, 5, 24), "Sokol" => Date.new(1962, 8, 11), "Berkut" => Date.new(1962, 8, 12)} => {"Kedr"=>#<Date: 4874803/2,0,2299161>, "Freedom 7"=>#<Date: 4874849/2,0,2299161>, "Liberty Bell 7"=>#<Date: 4875003/2,0,2299161>, "Orel"=>#<Date: 4875035/2,0,2299161>, "Friendship 7"=>#<Date: 4875431/2,0,2299161>, "Aurora 7"=>#<Date: 4875617/2,0,2299161>, "Sokol"=>#<Date: 4875775/2,0,2299161>, "Berkut"=>#<Date: 4875777/2,0,2299161>} # Where in the list is John Glenn's ship, the Friendship 7? launch_dates.find_index{|ship, date| ship == "Friendship 7"} => 4 # Where in the list is the first mission launched in August 1962? launch_dates.find_index{|ship, date| date.year == 1962 && date.month == 8} => 6 # The same thing, expressed a little differently launch_dates.find_index{|launch| launch[1].year == 1962 && launch[1].month == 8} => 6
Although Enumerable
has a method for checking whether an item is a member of a collection (the include?
method and its synonym, member?
), find_index
is a more powerful membership test for two reasons:
include?
/member?
only check membership by using the ==
operator, while find_index
lets you define a block to set up all sorts of tests. include?
/member?
asks “Is there an object X in the collection equal to my object Y?” while find_index
can be used to ask “Is there an object X in the collection that matches these criteria?”include?
/member?
returns true
if there is an object X in the collection that is equal to the given object Y. find_index
goes one step further: not only can it be used to report the equivalent of true
if there is an object X in the collection that is equal to the given object Y, it also reports its location in the collection.A quick example of this use in action:
# Once again, the mission leaders mission_leaders = ["Gagarin", "Shepard", "Grissom", "Titov", "Glenn", "Carpenter", "Nikolayev", "Popovich"] => ["Gagarin", "Shepard", "Grissom", "Titov", "Glenn", "Carpenter", "Nikolayev", "Popovich"] # Yuri Gagarin is in the list gagarin_in_list = mission_leaders.find_index {|leader| leader == "Gagarin"} => 0 # Captain James T. Kirk is not kirk_in_list = mission_leaders.find_index {|leader| leader == "Kirk"} => nil # gagarin_in_list is 0, which as a non-false and non-nil value evaluates as true. # We can use it as both a membership test *and* as his location in the list. p "Gagarin's there. He's number #{gagarin_in_list + 1} in the list." if gagarin_in_list "Gagarin's there. He's number 1 in the list." => "Gagarin's there. He's number 1 in the list." # kirk_in_list is nil, which is one of Ruby's two "false" values. # Let's use it with the "something OR something else" idiom that # many Ruby programmers like. kirk_in_list || (p "You only *think* he wasn't there.") "You only *think* he wasn't there." => "You only *think* he wasn't there."
Ruby-Doc.org’s documentation is generated from the comments in the C implementation of Ruby. It mentions a way of calling find_index
that is just like calling include?
/member?
:
# What the docs say (does not work yet!) ["Alice", "Bob", "Carol"].find_index("Bob") => 1 # What happens with the current version of Ruby 1.9 ["Alice", "Bob", "Carol"].find_index("Bob") ArgumentError: wrong number of arguments(1 for 0) ...
Ruby 1.9 is considered to be a work in progress, so I suppose it’ll get implemented in a later release.