Categories
Uncategorized

Enumerating Enumerable: Enumerable#collect/Enumerable#map

Here we are the third installment of Enumerating Enumerable, my attempt to do a better job of documenting Ruby’s Enumerable module than RubyDoc.org does. In this installment, I cover Enumerable#collect and its syntactic sugar twin (and the one I prefer), Enumerable#map.

In case you missed the earlier installments, they’re listed (and linked) below:

  1. all?
  2. any?

Enumerable#collect/Enumerable#map Quick Summary

Graphic representation of Ruby\'s \"Enumerable#collect / Enumerable#map\" methods

In the simplest possible terms Create a new array by performing some operation on every item in the given collection. collect and map are synonyms — you can use either. I personally prefer map as it’s shorter and makes sense if you think of the method as being like functional mapping.
Ruby version 1.8 and 1.9
Expects A block containing the criteria. This block is optional, but you’re likely to use one in most cases.
Returns An array made up of items created by performing some operation on the given collection.
RubyDoc.org’s entry Enumerable#collect / Enumerable#map

Enumerable#collect/Enumerable#map and Arrays

When used on an array and a block is provided, collect/map passes each item to the block, where the operation in the block is performed on the item and the result is then added to the result array. Note the the result array has the same number of elements as the given array.

[1, 2, 3, 4].map {|number| number ** 2}
=> [1, 4, 9, 16]

["Aqua", "Bat", "Super", "Wonder Wo"].map {|adjective| adjective + "man"}
=> ["Aquaman", "Batman", "Superman", "Wonder Woman"]

When the block is omitted, collect/map uses this implied block: {|item| item}, which means when applied on an array without a block, collect/map is the identity function — the resulting array is the same as the given array.

[1, 2, 3, 4].map
=> [1, 2, 3, 4]

["Aqua", "Bat", "Super", "Wonder Wo"].map
=> ["Aqua", "Bat", "Super", "Wonder Wo"]

Enumerable#collect/Enumerable#map and Hashes

When used on a hash and a block is provided, collect and map pass each key/value pair in the hash to the block, which you can “catch” as either:

  1. A two-element array, with the key as element 0 and its corresponding value as element 1, or
  2. Two separate items, with the key as the first item and its corresponding value as the second item.

Each key/value pair is passed to the block, where the operation in the block is performed on the item and the result is then added to the result array. Note the the result array has the same number of elements as the given array.

burgers = {"Big Mac" => 300, "Whopper with cheese" => 450, "Wendy's Double with cheese" => 320}

# What if I had just half a burger?
burgers.map {|burger| burger[1] / 2}
=> [160, 150, 225]

burgers.map {|sandwich, calories| calories / 2}
=> [160, 150, 225]

burgers.map {|burger| "Have a tasty #{burger[0]}!"}
=> ["Have a tasty Wendy's Double with cheese!", "Have a tasty Big Mac!",
 "Have a tasty Whopper with cheese!"]

burgers.map {|sandwich, calories| "Have a tasty #{sandwich}!"}
=> ["Have a tasty Wendy's Double with cheese!", "Have a tasty Big Mac!",
 "Have a tasty Whopper with cheese!"]

burgers.map {|sandwich, calories| ["Half a #{sandwich}", calories / 2]}
=> [["Half a Wendy's Double with cheese", 160], ["Half a Big Mac", 150],
 ["Half a Whopper with cheese", 225]]

When the block is omitted, collect/map uses this implied block: {|item| item}, which means when applied on an hash without a block, collect/map returns an array containing a set of two-item arrays, one for each key/value pair in the hash. For each two-item array, item 0 is the key and item 1 is the corresponding value.

burgers = {"Big Mac" => 300, "Whopper with cheese" => 450, "Wendy's Double with cheese" => 320}

burgers.map
=> [["Wendy's Double with cheese", 320], ["Big Mac", 300], ["Whopper with cheese", 450]]

Special Case: Using Enumerable#collect/Enumerable#map on Empty Arrays and Hashes

When applied to an empty array or hash, with or without a block, collect and map always return an empty array.

# Let's try it with an empty array
[].map
=> []

[].map {|item| item * 2}
=> []

# Now let's try it with an empty hash
{}.map
=> []

{}.map {|sandwich, calories| "Have a tasty #{sandwich}!"}
=> []

Categories
Uncategorized

Enumerating Enumerable: Enumerable#any?

Welcome to the second installment of Enumerating Enumerable, my project to do a better job of documenting Ruby’s Enumerable module than RubyDoc.org. This installment will cover Enumerable#any?, which I like to think of as Enumerable.all?‘s more easy-going cousin (I covered Enumerable.all? in the previous installment).

Enumerable#any? Quick Summary

Graphic representing Ruby\'s Enumerable#any? method

In the simplest possible terms Do any of the items in the collection meet the given criteria?
Ruby version 1.8 and 1.9
Expects A block containing the criteria. This block is optional, but you’re likely to use one in most cases.
Returns true if any of the items in the collection meet the given criteria.

false if none of the items in the collection does not meet the given criteria.

RubyDoc.org’s entry Enumerable#any?

Enumerable#any? and Arrays

When used on an array and a block is provided, any? passes each item to the block. If the block returns true for any item during this process, any? returns true; otherwise, it returns false.

# "Fromage de Montagne de Savoie" is the longest-named cheese in this list
# at a whopping 29 characters
cheeses = ["feta", "cheddar", "stilton", "camembert", "mozzarella", "Fromage de Montagne de Savoie"]

cheeses.any? {|cheese| cheese.length >= 25}
=> true

cheeses.any? {|cheese| cheese.length >= 35}
=> false

When the block is omitted, any? uses this implied block: {|item| item}. Since everything in Ruby evaluates to true except for false and nil, using any? without a block is effectively a test to see if any of the items in the collection evaluate to true (or conversely, if all the values in the array evaluate to false or nil).

cheeses.any?
=> true

cheeses = [false, nil]
=> [false, nil]

cheeses.any?
=> false

# Remember that in Ruby, everything except for false and nil evaluates to true:
cheeses << 0
=> [false, nil, 0]

>> cheeses.any?
=> true

Enumerable#any? and Hashes

When used on a hash and a block is provided, any? passes each key/value pair in the hash to the block, which you can “catch” as either:

  1. A two-element array, with the key as element 0 and its corresponding value as element 1, or
  2. Two separate items, with the key as the first item and its corresponding value as the second item.

If the block returns true for any item during this process, any? returns true; otherwise, it returns false.

# Here's a hash where for each key/value pair, the key is a programming language and
# the corresponding value is the year when that language was first released
# The keys range in value from "Javascript" to "Ruby", and the values range from
# 1987 to 1996
languages = {"Javascript" => 1996, "PHP" => 1994, "Perl" => 1987, "Python" => 1991, "Ruby" => 1993}

languages.any? {|language| language[0] < "Pascal"}
=> true

languages.any? {|language, year_created| language < "Pascal"}
=> true

languages.any? {|language| language[0] < "Fortran"}
=> false

languages.any? {|language, year_created| language < "Fortran"}
=> false

languages.any? {|language| language[0] >= "Basic" and language[1] <= 1995}
=> true

languages.any? {|language, year_created| language >= "Basic" and year_created <= 1995}
=> true

languages.any? {|language| language[0] >= "Basic" and language[1] <= 1985}
=> false

languages.any? {|language, year_created| language >= "Basic" and year_created <= 1985}
=> false

Using any? without a block on a hash is meaningless, as it will always return true. When the block is omitted, any? uses this implied block: {|item| item}. In the case of a hash, item will always be a two-element array, which means that it will never evaluate as false nor nil.

And yes, even this hash, when run through any?, will still return true:

{false => false, nil => nil}.any?
=> true

Special Case: Using Enumerable#any? on Empty Arrays and Hashes

When applied to an empty array or hash, with or without a block, any? always returns false. That’s because with an empty collection, there are no values to process and return a true value.

Let’s look at the case of empty arrays:

cheeses = []
=> []

cheeses.any? {|cheese| cheese.length >= 25}
=> false

cheeses.any?
=> false

# Let's try applying "any?" to a non-empty array
# using a block that ALWAYS returns true:
["Gruyere"].any? {|cheese| true}
=> true

# ...but watch what happens when we try the same thing
# with an EMPTY array!
[].any? {|cheese| true}
=> false

…now let’s look at the case of empty hashes:

languages = {}
=> {}

languages.any? {|language| language[0] < "Pascal"}
=> false

languages.any? {|language, year_created| language < "Pascal"}
=> false

languages.any?
=> false

# Let's try applying "any?" to a non-empty hash
# using a block that ALWAYS returns true:
{"Lisp" => 1959}.any? {|language| true}
=> true

# ...but watch what happens when we try the same thing
# with an EMPTY hash!
{}.any? {|language| true}
=> false

Categories
Uncategorized

The Twitter “Fail Whale’s” Appearance on “Laugh Out Loud Cats”

The \"Laugh Out Loud\" cats riding on the Twitter Fail Whale as it\'s being carried aloft by the birds: \"I still prefer riding teh rails.\"
Click the comic to see it on its Flickr page.

[Found via Laughing Squid.]

Categories
Uncategorized

Ten Most Densely Populated Technology Startup Regions

Ten Most Densely Populated Technology Startup Regions is a list created by Valleywag by using the Google Maps mashup Startup Warrior. The regions listed are: Downtown Palo Alto, San Francisco around the 101 between McAllister and Grove, Mountain View near Google, Midtown Manhattan between 34th and 40th, Downtown Manhattan just west of South Street Seaport, Seattle near James Street, Santa Monica near where the Santa Monica Freeway turns into Palisades Beach Road, Sunnyvale near Yahoo!, Austin around Congress Avenue, Downtown Vancouver – especially around Gastown.

Categories
Uncategorized

Where Did All the Cigarettes Go? (Joey’s Unofficial RubyFringe Guide to Toronto)

Joey\'s Unofficial RubyFringe Guide to Toronto

We’re less than a month away from RubyFringe, the self-described “avant-garde conference for developers that are excited about emerging Ruby projects and technologies” being put on by my friends at Unspace. RubyFringe promises to be an offbeat conference organized by the offbeat people at Unspace, an offbeat software development shop, with offbeat speakers and MCs (I’m one of them) making some offbeat presentations, which will be followed by offbeat evening events. It stands to reason that it should come with an offbeat guide to its host city, and who better than Yours Truly, one of the city’s most notorious bloggers and a long-time resident, to write one?

From now until RubyFringe, I’ll be writing a series of articles posted under the banner of Joey’s Unofficial RubyFringe Guide to Toronto, which will cover interesting things to do and see here in Accordion City. It’ll mostly be dedicated to the areas in which RubyFringe and associated events will be taking place and provide useful information about Toronto for people who’ve never been here (or even Canada) before. I’ll also try to cover some interesting stuff that the tourist books and sites don’t. If you’re coming up here — for RubyFringe or some other reason — I hope you’ll find this guide useful.

I thought I’d start the series by covering a topic with which I have almost no familiarity: smoking. It’s a safe bet that at least a few smokers will be coming to the conference from outside Ontario: if you’re one of these people, this article’s for you.

The Rules for Smoking in Ontario

If you really feel like poring over a legal document, you can read the Smoke-Free Ontario Act. If you’d rather not slog through the legalese, they can be boiled down to these two rules:

  • You have to be at least 19 years old to purchase cigrarettes.
  • No smoking indoors in public places.

Canadian Cigarette Brands

You’re going to have to ask someone else about which Canadian brands to smoke. Beyond “quit now,” I can’t really make any recommendations. What I know about Canadian cigarettes versus American ones isn’t much:

  • I am told that American cigarettes are “raunchier” than Canadian cigarettes. Can any cross-border smokers comment on this?
  • If you’re really homesick for Marlboros, you can get “Rooftop” brand cigarettes, which are Marlboros with packaging that makes use of Marlboro’s “rooftop” design but not the word “Marlboro”. The cigarette marketing site Filter Tips explains these “no-name” Marlboros, if you’re interested.

Canadian Cigarette Warning Labels

If you’re a smoker coming in from the United States and don’t travel outside the country much, you might not be aware that your country has the teeniest cigarette warning labels in the world, despite being the first to put warnings on cigarette packs in the first place.

Here in Canada, cigarettes have to devote half the visible surface of cigarette packaging to health warnings, which have livelier copy and are backed with pictures. Here are my two favourite warnings: first, the “mouth cancer” one…

Canadian cigarette warning label: \"Cigarettes cause mouth diseases\"

…and the “trying to stick a marshmallow into a parking meter” one:

Canadian cigarette warning label: \"Tobacco use can make you impotent\"

If you’re going to ignore the warnings, you might as well be entertained by them, right?

Canadian Cigarette Displays

And finally, I’ll come to the title of this post, Where Did All the Cigarettes Go?

If you set foot into a convenience store here, the first thing you’ll notice after the bilingual packaging is that there are no cigarettes to be seen. What you might see is a blank wall behind the shopkeeper that is almost completely devoid of features or markings. It’s a cigarette cabinet:

Artcube cigarette cabinets
An Artcube cigarette cabinet.

This started only a couple of weeks ago in Ontario, when the law banning the open display of cigarettes in stores came into effect. This “out of sight, out of mind”-inspired law requires people who sell cigarettes to store them in featureless cabinets, and it seems that they’re not allowed to post anything on them, even if it’s not tobacco-related. If you wander into a convenience store and are wondering where the cancer sticks are, they’re in the blank cabinets.

Categories
Uncategorized

Enumerating Enumerable: Enumerable#all?

The Return of Enumerating Enumerable

Back in January, I wrote that although the Ruby documentation site RubyDoc.org was useful, I found its writing unclear or confusing and some of its entries lacking in important information. In the “do it yourself and share it afterwards” spirit of open source, I started cataloguing the methods in Ruby’s workhorse module, Enumerable in a series of articles called Enumerating Enumerable. Enumerable is a pretty good place to start: its methods are often used and RubyDoc.org’s writeups of its methods are sparse (and in some cases, barely intelligible), especially when it comes to applying them to hashes.

In observance of another spirit of open source — that part that makes me sometimes yell “Free as in crap!” — I dropped the ball. There’s nothing like a little company turbulence and a sudden and very complete change in jobs to completely throw a wrench in a not-for-profit, self-driven, spare-time scratch-an-itch project like Enumerating Enumerable. Each time I started to write a new installment of Enumerating Enumerable, something would come up and I’d say “I’ll write it later.” As you know, later often turns into never.

I’ve been meaning to bring programming articles back to Global Nerdy for some time. In spite of the fact that my career track has been taking me away from day-to-day programming, I still plan to keep my skills sharp with writing development articles and working on hobby coding projects. With the RubyFringe conference coming up (I’m MCing the first evening’s commencement event) and my feeling a bit Ruby-rusty, I thought “What better time than now to reboot the Enumerating Enumerable series?”

So here begins version 2.0 of Enumerating Enumerable. I’ll be working my way through Enumerable‘s methods in alphabetical order, from Enumerable#all? to Enumerable#zip, each method covered for both arrays and hashes as well as special cases, supplemented with easy-to-grasp tables and graphics. I hope you find it useful!

Enumerable#all? Quick Summary

Graphic representation of Ruby\'s Enumerable#all? method

In the simplest possible terms Do all items in the collection meet the given criteria?
Ruby version 1.8 and 1.9
Expects A block containing the criteria. This block is optional, but you’re likely to use one in most cases.
Returns true if all the items in the collection meet the given criteria.

false if at least one of the items in the collection does not meet the given criteria.

RubyDoc.org’s entry Enumerable.all?

Enumerable#all? and Arrays

When used on an array and a block is provided, all? passes each item to the block. If the block never returns false or nil during this process, all? returns true; otherwise, it returns false.

# "feta" is the shortest-named cheese in this list
cheeses = ["feta", "cheddar", "stilton", "camembert", "mozzarella", "Fromage de Montagne de Savoie"]

cheeses.all? {|cheese| cheese.length >= 4}
=> true

cheeses.all? {|cheese| cheese.length >= 5}
=> false

When the block is omitted, all? uses this implied block: {|item| item}. Since everything in Ruby evaluates to true except for false and nil, using all? without a block is effectively a test to see if all the items in the collection evaluate to true (or conversely, if there are any false or nil values in the array).

cheeses.all?
=> true

cheeses << false
=> ["feta", "cheddar", "stilton", "camembert", "mozzarella", "Fromage de Montagne de Savoie", false]

cheeses.all?
=> false

Enumerable#all? and Hashes

When used on a hash and a block is provided, all? passes each key/value pair as a two-element array to the block, which you can “catch” as either:

  1. A two-element array, with the key in element 0 and its corresponding value in element 1, or
  2. Two separate items, the first being the key, the second being the corresponding value.

If the block never returns false or nil during this process, all? returns true; otherwise, it returns false.

# Here's a hash where for each key/value pair, the key is a programming language and
# the corresponding value is the year when that language was first released
# The keys range in value from "Javascript" to "Ruby", and the values range from
# 1987 to 1996
languages = {"Javascript" => 1996, "PHP" => 1994, "Perl" => 1987, "Python" => 1991, "Ruby" => 1993}

languages.all? {|language| language[0] >= "Delphi"}
=> true

languages.all? {|language, year_created| language >= "Delphi"}
=> true

languages.all? {|language| language[0] >= "Visual Basic"}
=> false

languages.all? {|language, year_created| language >= "Visual Basic"}
=> false

languages.all? {|language| language[0] >= "Delphi" and language[1] <= 2000}
=> true

languages.all? {|language, year_created| language >= "Delphi" and year_created <= 2000}
=> true

languages.all? {|language| language[0] >= "Delphi" and language[1] > 2000}
=> false

languages.all? {|language, year_created| language >= "Delphi" and year_created > 2000}
=> false

Using all? without a block on a hash is meaningless, as it will always return true. When the block is omitted, all? uses this implied block: {|item| item}. In the case of a hash, item will always be a two-element array, which means that it will never evaluate as false nor nil.

And yes, even this hash, when run through all?, will still return true:

{false => false, nil => nil}.all?
=> true

Special Case: Using Enumerable#all? on Empty Arrays and Hashes

When applied to an empty array or hash, with or without a block, all? always returns true. That’s because with an empty collection, there are no values to process and return a false value.

Let’s look at the case of empty arrays:

cheeses = []
=> []

cheeses.all? {|cheese| cheese.length >= 4}
=> true

cheeses.all?
=> true

# Let's try applying "all?" to a non-empty array
# using a block that ALWAYS returns false:
["Gruyere"].all? {|cheese| false}
=> false

# ...but watch what happens when we try the same thing
# with an EMPTY array!
[].all? {|cheese| false}
=> true

…now let’s look at the case of empty hashes:

languages = {}
=> {}

languages.all? {|language| language[0] >= "Delphi"}
=> true

languages.all? {|language, year_created| language >= "Delphi"}
=> true

languages.all?
=> true

# Let's try applying "all?" to a non-empty hash
# using a block that ALWAYS returns false:
{"Lisp" => 1959}.all? {|language| false}
=> false

# ...but watch what happens when we try the same thing
# with an EMPTY hash!
{}.all? {|language| false}
=> true

Categories
Uncategorized

How Not to Approach an Investor

“After reviewing your public profile, blog, general google results, we’ve concluded that we can allow your firm the opportunity to review our company for investment.” Rick Segal (an investor in my company, b5media) tells a story that explains how not to approach an investor.