Practical Object-Oriented Design in Ruby

This book is great:
http://www.poodr.com
POODR


Vim & Tmux video


Duplication is far cheaper than the wrong abstraction

Sandy Metz said that if you find yourself passing parameters and adding conditional paths through shared code, the abstraction is incorrect. Then you should go back:

  1. Re-introduce duplication by inlining the abstracted code back into every caller.
  2. Within each caller, examine the parameters that got passed in order figure out which bits of the inlined code you actually use.
  3. Delete the bits that aren’t needed for this particular caller.

Null Object Pattern

Bad:

ids = ['pig', '', 'sheep']

animals = ids.map {|id| Animal.find(id)}

animals.each do |animal|
  puts animal.nil? ? 'no animal' : animal.name
end

Good:

animals = ids.map{ |id| GuaranteedAnimal.find(id) }
animals.each{ |animal| puts animal.name }

class GuaranteedAnimal
  def self.find(id)
    Animal.find(id) || MissingAnimal.new
  end
end

class MissingAnimal
  def name
    'no animal'
  end
end

Seen in the video of Sandi Metz in RailsConf 2015:


Wikidata

wikidata-logoWikidata is the Wikipedia project for structured knowledge. For example it knows that the capital of Spain is Madrid. The ruby gem wikidata has a basic supportĀ to retrieve it:


require 'wikidata'

Wikidata::Item.find_by_title('Spain').claims_for_property_id('P36').first.mainsnak.value.entity.label