NL in Ruby

$ irb

1.9.3-p327 :001 > How many sane ways are there to use Ruby rescue methods. none?

 => false 

When [a,b].min != a < b ? a : b

$ irb

1.9.3p327 :001 > Float::NAN < 1.1

 => false 

1.9.3p327 :002 > [Float::NAN, 1.1].min

ArgumentError: comparison of Float with Float failed

It would be more robust if min used <….

It depends on what the meaning of “is a” is

$ irb

1.9.3-p327 :001 > require ‘net/smtp’

 => true 

1.9.3-p327 :002 > Net::SMTPFatalError.superclass

 => Net::ProtoFatalError 

1.9.3-p327 :003 > Net::SMTPFatalError.superclass.superclass

 => Net::ProtocolError 

1.9.3-p327 :004 > Net::SMTPFatalError.superclass.superclass.superclass

 => StandardError 

1.9.3-p327 :005 > Net::SMTPFatalError.new.is_a? Net::SMTPError

 => true 

1.9.3-p327 :006 > 

But Why Does It Round

Sleep returns “the actual number of seconds slept, rounded”.

$ irb

ruby-1.9.2-p180 :001 > sleep 3.5

 => 4 

ruby-1.9.2-p180 :002 > sleep 3.5

 => 3 

Why??

As Opposed to Mexican ASCII

1.9.2p180 :001 > 127.chr.encoding

 => #<Encoding:US-ASCII> 

1.9.2p180 :002 > 128.chr.encoding

 => #<Encoding:ASCII-8BIT> 

Who wants a consistent return type from built-in methods, anyway?

The definition of “time”

time ruby test/functional/my_controller_test.rb 

Loaded suite test/functional/my_controller_test

Finished in 1.316346093 seconds.

2.28 tests/s, 0.00 assertions/s

real 0m28.309s

user 0m26.170s

sys 0m1.740s

Rails claims 1 second, the clock claims 28 seconds. Well, that’s one approach to complaints that tests take too long to run.

RWTF Classic: The Seven Types of Blocks
#                                                     "return" returns from closure
#                                    True closure?    or declaring context...?         Arity check?
#                                    ---------------  -----------------------------    -------------------
# 1. block (called with yield)       N                declaring                        no
# 2. block (&b => f(&b) => yield)    N                declaring                        no
# 3. block (&b => b.call)            Y except return  declaring                        warn on too few
# 4. Proc.new                        Y except return  declaring                        warn on too few
# 5. proc                                    <<< alias for lambda in 1.8, Proc.new in 1.9 >>>
# 6. lambda                          Y                closure                          yes, except arity 1
# 7. method                          Y                closure                          yes

A classic. More detail at http://innig.net/software/ruby/closures-in-ruby !

assert_no_match

So in Test::Unit, the assert_no_match method is documented here.

http://ruby-doc.org/stdlib-1.9.3/libdoc/test/unit/rdoc/Test/Unit/Assertions.html

What about assert_match? It’s a method used in basically exactly the same way. But, it’s not mentioned here. Presumably documented somewhere, just not in the same place. Convenient!

String splitting

$ irb

ruby-1.9.2-p180 :001 > “fox”.split “f”

 => [“”, “ox”] 

ruby-1.9.2-p180 :002 > “fox”.split “x”

 => [“fo”] 

Of course, why would you expect string splitting to have left-right symmetry?

Naturally, if you want string splitting to behave symmetrically, you add a -1 parameter.

ruby-1.9.2-p180 :003 > “fox”.split(“x”, -1)

 => [“fo”, “”] 

Object’s blank?

A ArgumentError occurred in collections#show:

 invalid byte sequence in UTF-8

 activesupport (3.0.11) lib/active_support/core_ext/object/blank.rb:68:in `=~’

Thanks Rails. Now Object has buggy methods that crash.