In Ruby when inheriting class beware of the expected value of self. The following example illustrates this caveat. class Log def self.priority Log.new(priority: true) end end   class Fishing < Log end   >> Fishing.priority => <#Log … > But… if you declare self.new the class with inherit from current class.   class Log def self.priority self.new(priority: true) end end   class Fishing < Log end   >> Fishing.priority #=> <#Fishing … > :)

Today in the Ruby style guide I came across this commit. This is a great example of how to make Ruby much faster. The expensive computation will only load if the key is missed when we use the block, but if we make it the default it eager loads running regardless if needed. require ‘benchmark’   def compute sleep 3 end hash = {:key => ‘value’} with_value = Benchmark.realtime do hash.fetch(:key, expensive_computation) end   with_block = Benchmark.realtime do hash.fetch(:key){ expensive_computation } end   puts with_value.to_i # ==> 3 puts with_block.to_i # ==> 0 :)

Ruby #sort method doesn’t do well with nil, so clean nil out of your arrays before attempting to sort. >> arr1 = [nil, "6:1", "1:2", "2:4", "4.3:0.92", "5.0:4", "2.0 to 3"] #=> [nil, "6:1", "1:2", "2:4", "4.3:0.92", "5.0:4", "2.0 to 3"]   >> puts Benchmark.measure { arr1.sort } #=> ArgumentError: comparison of NilClass with String failed from (irb):53:in `sort’   >> arr1 = ["6:1", "1:2", "2:4", "4.3:0.92", "5.0:4", "2.0 to 3"] #=> ["6:1", "1:2", "2:4", "4.3:0.92", "5.0:4", "2.0 to 3"]   >> puts Benchmark.measure { arr1.sort } #=> 0.000000 0.000000 0.000000 ( 0.000012) Use a delete(nil) before sorting. >> arr2 = [nil, "6:1", "1:2", "2:4", "4.3:0.92", "5.0:4", "2.0 to 3"] #=> [nil, "6:1", "1:2", "2:4", "4.3:0.92", "5.0:4", "2.0 to 3"][…]