force unmount on mac os x 27. Jun 2008

When using Mac OS X you might have encountered this message.

The disk "foobar" is in use and could not be ejected.
Try quitting appplications and try again.

This message may occur even if you have all closed all programs. The guilty party for this behavious may be Leopard’s FSEvents, which sometimes has a lock on a volume. To force the unmount of a volume just open a Terminal and use umount.

sudo umount -f /Volumes/FooBar
 

tcpdump: packets dropped by kernel 25. Jun 2008

tcpdump is a really nice tool, but it may render useless with it’s default settings.

$ sudo tcpdump -i en1

In my case led to

80 packets captured
7705 packets received by filter
6794 packets dropped by kernel

To solve the issue I had to turn off address translation.

$ sudo tcpdump -i en1 -n

Now the results look as expected.

6941 packets captured
7011 packets received by filter
0 packets dropped by kernel
 

ordered hashes in ruby 1.8 19. Jun 2008

Ruby 1.8 doesn’t offer builtin ordered hashes, but there are several libraries out there which extend or replace Ruby’s Hash class. Among them are Ruby/RBTree, Ara T. Howard’s orderedhash and Ruby Facets. All three of these offer a hashlike syntax, letting you do things like

ordered_hash = Library['a', 1, 'b' ,2]

resulting in

{'a'=>1, 'b'=>2}

Sadly all three of these libraries have poor documentation. The one with most bearable in my eyes is Ruby Facet’s Dictionary, which also has a RubyGem available, so let’s have a look at it.

$ sudo gem install facets
Successfully installed facets-2.4.1
1 gem installed
Installing ri documentation for facets-2.4.1...
Installing RDoc documentation for facets-2.4.1...

Ruby Facets is a rather large library, so you’ll want to require just the Dictionary class.

>> require 'rubygems'
>> require 'facets/dictionary'

Let’s make an ordered hash with five elements and print it out.

>> dictionary = Dictionary[*(1..10)]
>> dictionary.each {|k,v| puts "#{k}=>#{v}"}
1=>2
3=>4
5=>6
7=>8
9=>10

Works fine. Now for the bonus round. Dictionary can also be used as a sorted hash by calling the order_by or more conveniently the order_by_key method. Once called this method ensures that all previously and subsequently inserted keys will be in sorting order.

>> array = Array.new(10){rand(100)}
>> dictionary = Dictionary[*array]
=> {1=>49, 48=>97, 17=>66, 6=>70, 52=>77}
>> dictionary.order_by_key
>> dictionary.each {|k,v| puts "#{k}=>#{v}"}
1=>49
6=>70
17=>66
48=>97
52=>77
 

ordered hashes in ruby 1.9 19. Jun 2008

If you create a hash in Ruby 1.8 and then browse it with the each-iterator you get the keys delivered in abitrary order.

>> hash = {:a=>1, :b=>2, :c=>3,}
>> hash.each {|key,value| puts "#{key}=>#{value}"}
c=>3
b=>2
a=>1

This might be okay for some cases, but in other cases you might want the insertation order preserved. Well, good news in Ruby 1.9 this works.

>> hash = {:a=>1, :b=>2, :c=>3,}
>> hash.each {|key,value| puts "#{key}=>#{value}"}
a=>1
b=>2
c=>3
 

slow iterators in ruby 1.9 on mac 17. Jun 2008

If you ever wondered why your Ruby programs are not faster but much slower with Ruby 1.9 you might be using each or times iterators on large collections.

10000000.times {}

In fact if you do a benchmark,

require 'benchmark'

n = 10000000

Benchmark.bm do |x|
  x.report { n.times {} }
end

you get stunning results.

$ ruby bench.rb
   user     system      total        real
   0.720000   0.000000   0.720000 (  0.721872)
$ ruby1.9 bench.rb 
   user     system      total        real
   16.670000   9.110000  25.780000 ( 25.923977)

When increasing the number of iterations by powers of ten it looks like the computational complexity of the times iterator is O(n) in Ruby 1.9. According to Antonio Cangiano this behaviour is specific to Mac OS X and does not appear on Linux.

 

1 2 3 4