March 2007 Archives

More on IE and window.event

| 6 Comments

Julien Royer sent me an email today following up on this post. Try the following code in IE:

<script>
window.onload = function() {
  alert(window.event == window.event)
}
</script>

When I run this in IE6 on a Windows XP machine, I get false. This is just weird. The window.event object thinks that it is not equal to itself.

This probably does not have any impact on real-world code. I bring it up, however, because in that last post about IE and events, my commenters and I decided that the event object passed to an event handler registered with attachEvent is not the same as window.event. The code shown here calls that into question. Perhaps they are the same objects, but the weird behavior of window.event causes them to appear unequal.

Ruby exception-handling quiz

Test your knowledge of Ruby's rescue/else/ensure exception handling construct! Fire up irb to check your answers.

1) What is the value of x?

x = begin 1; rescue 2; else 3;  ensure 4; end

2. What does this method return?

def test
  return 1
rescue
  return 2
else
  return 3
end

3. What does this method return?

def test
  return 1
ensure
  return 2
end

4. How about this one?

def test
  1
ensure
  2
end

5. What kind of exception does this method raise?

def test
  raise ArgumentError
rescue ArgumentError
  raise TypeError
end

6. What kind of exception does this method raise?

def test
  raise ArgumentError
ensure
  raise TypeError
end

7. What does this method do?

def test
  3.times do |x|
    begin
      puts x
      raise "error"
    ensure
      next
    end
  end
end

rescue clauses on Ruby class and module definitions

I've been studying exceptions in Ruby, and have discovered some syntax that is not documented in the Pickaxe, nor anywhere obvious that I can find with Google.

Everyone knows about rescue clauses with begin/end. And most Ruby programmers probably know that you can use a rescue clause at the end of a method definition. What I was surprised to discover (by reading the parse.y grammar file in the source distribution) is that rescue clauses are also allowed at the end of class and module definitions.

This, for example, is legal Ruby:

class Test
  def t
    1
  end
  raise "foo"
rescue 
  p $!
else
  p "Class Test defined without exceptions"
ensure
  p "Exiting class Test"
end

p Test.new.t

I'm not sure when this would be useful, but it is legal, and it ought to be documented!

4 years in Iraq

| 6 Comments

This is a letter to the editor of my local paper. Feel free to edit and send it to your paper, too!

To The Editor:

Monday, March 19th is the fourth anniversary of the US invasion and occupation of Iraq. We've now fought in Iraq longer than we fought in WWII. The war has cost $410 billion. That's $280 million a day--more than the daily cost of Vietnam, adjusted for inflation. (These costs are over and above ordinary maintenance costs for the military and do not include future costs for the care of wounded veterans nor future interest payments on the borrowed money to pay for the war.) Most tragically, the war has cost the lives of more than 3,200 US soldiers. Over the last year there have been about 2.5 casualties on an average day. Every day this war continues, two or three more soldiers will die, leaving behind their wives and husbands, their mothers and fathers, their daughters and sons.

Iraq is a quagmire worse than Vietnam. In Vietnam, we knew who we were fighting: the Communists. And the Domino Theory, as flawed as it was, told us why we were fighting. In Iraq, we don't know who we're fighting: is it the Sunnis, the Shiites, the Baathists, the "insurgents", Al-Qaeda in Iraq, or some combination of them? And we don't know why we're fighting: are we trying to stop the spread of WMDs, to promote democracy, to prevent a civil war, or just because we don't know how to get out of the hole we've dug?

Please speak out. It is time to end this senseless war!

Update: comments now closed due to comment spam

Metaweb and Freebase.com

| 2 Comments

There is buzz today about Metaweb decloaking and launching Freebase.com. Both Tim O'Reilly and The New York Times are covering it.

It's a bit of a tease, actually, since Freebase.com is in alpha release and you have to put your name on a list for an invitation code. (Like the early days of gmail). But if you do get in, you might be interested in the documentation I wrote for Metaweb: Developing Metaweb-Enabled Web Applications.

The sign-up for the invitation code is in the lower-right corner at Freebase.com

Books

Comprehensive coverage of Ruby 1.8 and 1.9

"The New Most Important Ruby Book"
Peter Cooper,
rubyinside.com

Completely updated for Ajax and Web 2.0

"A must-have reference"
Brendan Eich,
creator of JavaScript

The classic Java quick-reference