Archive for June, 2007

reCAPTCHA: stop spam. read books.

This is really cool. reCAPTCHA is a free CAPTCHA service that asks the user to type two words. They know the first word, but the second comes from a failed OCR scan. If you get the first word right, reCAPTCHA assumes you’re human, and they store your answer to the second word…they even run it by a few other humans to raise the confidence. Not only are you authenticated, you’re transcribing. According to reCAPTCHA, “in aggregate these little puzzles consume more than 150,000 hours of work each day”.

[For the record, I don't know anyone at reCAPTCHA. I found them while reading up on CAPTCHAs for work.]

Ruby’s other ternary operator

This may be a well-know Ruby trick, but I thought I’d share it anyway.

Ruby has the ternary operator:

a = true  ? 'a' : 'b' #=> "a"
b = false ? 'a' : 'b' #=> "b"

But it also has something else…

a = (true  && 'a') || b #=> "a"
b = (false && 'a') || b #=> "b"

All statements in Ruby return the value of the last expression evaluated, so true && 'a' returns ‘a’, and the ‘b’ is ignored (via boolean short-circuiting). false && 'a' evaluates to false, so Ruby moves on, and returns ‘b’.

This is a lot like the a ||= 'a' trick, which expands to a = a || 'a', giving the variable a default value, if it’s not already initialized.

If Ruby already has the ternary operator, why bother? Personally, I find this form more natural (which surprised me, after years of using the ternary operator in Java and C#). For some reason, it reads more like natural language to me.

var user_name = (user.authenticated? && user.name) || 'guest'

Does anyone else see it?


Say Hello

danbernier [at] gmail [dot] com
Twitter @danbernier
Hartford.rb
LinkedIn

Tweeting:

  • @dchec Invite them behind the counter, and move the event to a small town. You never know. 2 days ago
  • @kdaigle Where's the new digs again? Congrats on the move! 2 days ago
  • "Persuading the world to adopt a new kind of arithmetic is quixotic, like trying to replace the QWERTY keyboard." http://bit.ly/TWv5P DANG! 2 days ago
  • @soderberg Sessions Woods is beautiful in the spring -- when the swamps start flowering, it's pretty amazing. On of my favorite local hikes. 3 days ago
  • "Anything which has to call itself a science isn't." - Fred Brooks, "The Computer Scientist as a Toolsmith" http://bit.ly/vXLoj 4 days ago

I’m a BackPack fan

Backpack

Categories