Umlaut Aware Alphabetical Sorting

The simples solution i could come up with, convert all chars to their base (á -> a), using String.tr would be ideal for this task, but its not UTF8 aware…

If you dare to digg deeper, more information can be found on wiki and on coding horror

list.sort_by{|name| convert_umlaut_to_base(name)}

  def convert_umlaut_to_base(input)
    text = input.dup
    %w[áäa ÁÄA óöo ÓÖO íi ÍI úüu ÚÜU ée ÉE ßs].each do |set|
      text.gsub!(/[#{set[0..-2]}]/,set[-1..-1])
    end
    text
  end

Cucumber vs ActiveRecord 2.3 — Attempt to call private method (NoMethodError)

All of the sudden some columns began to misbehave in cucumber tests, claiming that “Attempt to call private method (NoMethodError)” on normal column attributes!

A simple fix for this, essentially saying that a method is not private when its a column:

#features/support/ar_private_fix.rb required from env.rb
unless ActiveRecord::Base.methods.include?('private_method_defined_with_fix?')
  class ActiveRecord::Base
    class << self
      def private_method_defined_with_fix?(method)
        return false if columns_hash.keys.include?(method.to_s)
        private_method_defined_without_fix?(method)
      end
      alias_method_chain :private_method_defined?, :fix
    end
  end
end

Micro Benchmark fastest threadsave Accessor

While working on my new Fast-Gettext i needed a really fast way load the current translations, since they are needed every time, and must be stored inside Thread.current for Thread-safety. So i came up with a small micro benchmark for fastest accessor.

This test is separate into single-access and generic access (just one value / n values)

Results

Ruby 1.8.7
generic:
  Symbol: 1.06s
  String concat: 1.73s
  String add: 1.69s
  String insert: 1.47s
single:
  Symbol: 0.67s
  String: 0.96s

So as we can see, stick to symbols!

Test

require 'benchmark'
BASELINE = 0
def test
  result = Benchmark.measure {1_000_000.times{ yield }}
  result.to_s.strip.split(' ').first.to_f - BASELINE
end

BASELINE = (test{})
Thread.current[:library_name]={}
other = "x"

puts "generic:"
puts "Symbol: #{test{Thread.current[:library_name][:just_a_symbol]}}s"
puts "String concat: #{test{Thread.current["xxxxxx"<<other.to_s]}}s"
puts "String add: #{test{Thread.current["xxxxxx"+other.to_s]}}s"
puts "String insert: #{test{Thread.current["xxxxxx#{other}"]}}s"

puts "single:"
puts "Symbol: #{test{Thread.current[:long_unique_symbol]}}s"
puts "String: #{test{Thread.current["xxxxxx"]}}s"

Whats your Namespace-Fooptrint?

Could it be that you pollute the global namespace ?

If you ask your users to ‘include’ your library this can happen easily!

Calculate footprint

initial = methods.count + Module.constants.count
require 'my_library'
include MyLibrary

puts "Namespace-Footprint"
puts methods.count + Module.constants.count - initial

How to clean up ?

A simple solution is adding a separate Inclusion module, that does not live in your main
namespace and redirect all methods you want to share with the user from your main namespace.

#my_library/inclusion.rb
module MyLibrary
  module Inclusion
    ...
  end
end

#my_library.rb
module MyLibrary
  ..
  Inclusion.public_instance_methods.each do |method|
    define_method method do |*args|
      Inclusion.send(method,*args)
    end
  end
end