From Sloppy Pheasant, 1 Year ago, written in Ruby.
This paste will go to meet its maker in 1 Second.
Embed
  1.     def deep_fetch(*keys)
  2.       deep_fetch!(*keys)
  3.     rescue NoMethodError, AttributeDoesNotExistError
  4.       nil
  5.     end
  6.  
  7.     def deep_fetch!(*keys)
  8.       keys.map!(&:to_s)
  9.  
  10.       keys.inject(attributes.to_hash) do |hash, key|
  11.         if hash.key?(key)
  12.           hash[key]
  13.         else
  14.           raise AttributeDoesNotExistError.new(keys, key)
  15.         end
  16.       end
  17.     end
  18.