Archive for April, 2008

What is your Zombie Escape Plan?

April 16, 2008

Ok, so I was talking to my cool niece last month and she told me something that just cracked me up.

Are you ready?

Here goes:

Every teenage boy has a Zombie Escape Plan.

That’s what my niece told me. She was serious. And she thought it was just as weird as I do. (She doesn’t have one.)

Here’s how she found out about it. One day she was listening as two male friends of hers were comparing zombie escape plans. This was new to her. “Does every guy have a zombie escape plan?” she asked them.

“Well duh,” they both said, dead serious.

So she ran an unscientific survey of her teenage male friends to find out if it was true. And guess what.

It was.

Every boy she asked said yes, naturally he has a zombie escape plan.

By now I was busting up. I told her well, at least I have a fire escape ladder in my 2-story house. I could use that as my zombie escape plan too. Her dad (my brother-in-law) said no: that’s lame. A fire escape plan does not serve as a zombie escape plan. As evidence he pointed to his own zombie escape plan: he will dance a jig. Because everyone knows a zombie cannot resist dancing a jig if he sees someone else doing it. But it doesn’t make a very good fire escape plan.

My niece said her dad was right. One boy in her survey said his zombie escape plan involved climbing up on the roof, which is usually not a good idea in a fire. At this point her younger brother, who’s also a teenager, piped up and said that after all, his zombie escape plan is to use a flame thrower. And that never makes a very good fire escape plan.

When I got home I googled it and found that there is even a web site dedicated just to this (side note: what did we ever do before the web?): http://www.zombieescapeplan.com. Except it’s made by a girl. So I guess at least that’s good because when the zombies attack some of the girls will be prepared.

I need a good zombie escape plan. What’s yours? Please comment below so I can get some good ideas.

How to write case (switch) statements in Ruby

April 15, 2008

If you’re like me, when you started coding in Ruby last year you found the “case” statement intriguing. After years of writing in C++ and C# it was hard for you to remember Ruby’s case syntax because it can do so much more than switch statements in those languages.

So you wrote these notes to yourself as you discovered its capabilities. Except you’re not that much like me so you didn’t. But I did. I hope you find them useful.


switch/case syntaxes
(remember: Ruby uses "case" and "when"
where others use "switch" and "case"):

# Basically if/elsif/else (notice there's nothing
# after the word "case"):
[variable = ] case
when bool_condition 
 statements
when bool_condition
 statements
else
 statements
end

# It's common for the "else" to be a 1-line
# statement even when the cases are multi-line:
[variable = ] case
when bool_condition 
 statements
when bool_condition
 statements
else statement
end

# Case on an expression:
[variable = ] case expression
when nil
 statements execute if the expr was nil
when Type1 [ , Type2 ] # e.g. Symbol, String
 statements execute if the expr
  resulted in Type1 or Type2 etc.
when value1 [ , value2 ]
 statements execute if the expr
 equals value1 or value2 etc.
when /regexp1/ [ , /regexp2/ ]
 statements execute if the expr
 matches regexp1 or regexp 2 etc.
when min1..max1 [ , min2..max2 ]
 statements execute if the expr is in the range
 from min1 to max1 or min2 to max2 etc.
 (use 3 dots min...max to go up to max-1)
else
 statements
end

# When using case on an expression you can mix &
# match different types of expressions. E.g.,
[variable =] case expression
when nil, /regexp/, Type
 statements execute when the expression
 is nil or matches the regexp or results in Type
when min..max, /regexp2/
 statements execute when the expression is
 in the range from min to max or matches regexp2
end

# You can combine matches into an array and
# precede it with an asterisk. This is useful when
# the matches are defined at runtime, not when
# writing the code. The array can contain a
# combination of match expressions
# (strings, nil, regexp, ranges, etc.)
[variable =] case expression
when *array_1
 statements execute when the expression matches one
 of the elements of array_1
when *array_2
 statements execute when the expression matches one
 of the elements of array_2
end

# Compact syntax with 'then':
[variable =] case expression
when something then statement
when something then statement
else statement
end

# Compact syntax with semicolons:
[variable =] case expression
when something; statement
when something; statement
else statement # no semicolon required
end

# Compact syntax with colons:
[variable =] case expression
when something: statement
when something: statement
else statement # no colon required
end

# 1-line syntax:
[variable = ] case expr when {Type|value}
 statements
end

# Formatting: it's common to indent the "when"
# clauses and it's also common not to:
case 
  when 
  when
  else
end

case
when
when
else
end

hello, world

April 13, 2008

I am Brian Morearty. Welcome to my blog.

I’m a software engineer living with my wife and three cute kids in the San Francisco Bay Area.

I plan to blog about stuff like software development, things that strike me as funny, etc. The technology that currently has my interest is Ruby on Rails and Flex but that may change over time. For most of my career in software I’ve been working on Windows, and I’m also a fan of most Microsoft developer technology (WPF, ASP.NET, and so on.)