Good repository descripton = more wachers

All my repos that have a bad or unclear description have few watchers. Why? Because many of them are not found through google, but from people that watch one of my plugins and have a look at the rest. Any repository that does not clearly state its use, within the 3 seconds of read it gets, is not looked at further.

  • Which architecture does it use ? – prefix with e.g. Rails: AR: jQuery:
  • What does it do? – clear, short explanation

Bad — When Active Record objects are saved from a form, empty fields are saved as empty strings instead of nil. This kills most validations.

Good — AR: store empty strings as NULL, making queries + conditions simpler and code more robust

And now ill continue renaming… ๐Ÿ˜‰

Lessons learned from upgrading Rails 2.1 to 2.3

Just my collection, so you do not need to find it out on your own ๐Ÿ˜‰

  • :expire now is called :expires_in, update your cache calls or timed expiring will fail
  • get rspec edge/ rspec-rails edge
    detailed instruction
    also possible and easy: sudo gem install dchelimsky-rspec dchelimsky-rspec-railsyou may need to add this when you are on 1.1.99.9 and get
    no such file to load — spec/rails/example/routing_example_group
    from github
  • get webrat 0.4.2 (atm only by installing from github clone + rake gem + sudo gem install)
  • truncate(‘xxx’,3) ==> truncate(‘xxx’,:length=>3)
  • Model.find_by_xxx will now call Model.find with :conditions=>{:xxx=>something}, this broke some of my stubs/mocks, e.g. User.stubs(:find) worked before but now came in conflict with user login that used find_by_id
  • render :inline seems not to be testable, all my request.body from render :inline are blank now (rspec 1.1.12)
  • stub_model install rspec-rails-mocha — ./script/plugin install git://github.com/mislav/rspec-rails-mocha.git
  • count returns OrderedHash, so tests now look like xxx.to_a.should == [[‘name’,2]]
  • String.chars does not have the [] method anymore, so e.g. only “xxx”.mb_chars[1..4] will work
  • @template will no longer work in vie tests, use response instead
  • have_tag returns
    undefined method `id2name’ for {:instance_writer=>false}:Hash
    , upgrade the money gem!
  • controller tests are no longer able to call actions that are not defined (e.g. no action was defined because it only renders a view and does nothing or because the action was included from a module)
  • HopToad will silently fail, use this branch instead: http://github.com/pzingg/hoptoad_notifier (as long as thoughbot does not update their own of course)
  • work systematically e.g. rake spec:models, then rake spec:controllers … or you may get mad ๐Ÿ˜‰

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