photo
Jordan Sissel
geek

Tue, 06 May 2008

Riding on the failboat: Ruby, episode 1.

I'm quickly learning ruby at my new job. Today's POLA (principle of least astonishment) violation is blamed on my expectations of 'if' behavior in Ruby from what I know from Python, C, and other languages.

Non-nil values are considered true:

["0", [], {}, "", 0, true, false, nil].each { |x|
  bool = (x) ? true : false
  puts "#{x.inspect}: #{bool}"
}

"0": true
[]: true
{}: true
"": true
0: true
true: true
false: false
nil: false
I mostly expected every one of the outputs here except for the literal 0 value being true. Noted for future reference.

Additionally confusing, is that Integer() will barf on most non-number inputs, but for some reason "nil" means 0.

irb(main):002:0> Integer(nil)
=> 0
Unexpected.

Comments: 1 (view comments)
Tags: , ,
Permalink: /geekery/failboat-ruby-1
posted at: 19:00

Search this site

Navigation

Metadata

Home About Resume My Code

Articles

ARP Security Dynamic DNS with DHCP OpenLDAP+Kerberos+SASL PPP over SSH SSH Security: /bin/false Week of Unix Tools Work Efficiency

Projects

fex firefox tabsearch firefox urledit grok keynav liboverride newpsm (FreeBSD) nis2ldap pam_captcha poor man's backup Solaris audio utility xboxproxy xdotool xmlpresenter xpathtool misc scripts

Presentations

Yahoo! Hack Day '06 Unix Essentials Vi/Vim Essentials

Tag Cloud

Calendar

< May 2008
SuMoTuWeThFrSa
     1 2 3
4 5 6 7 8 910
11121314151617
18192021222324
25262728293031

Friends

BarCamp Kent Brewster Tantek Çelik John Resig Wesley Shields Tyler Shields

Technorati