SHARE
TWEET

Code Review

a guest Jun 5th, 2015 245 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Dino
  2.   attr_reader :name, :era, :size
  3.  
  4.   def initialize(name, era, size)
  5.     @name = name
  6.     @era = era
  7.     @size = size
  8.   end
  9. end
  10.  
  11.  
  12.     def filter(options = [])
  13.       options.inject($DINOS) do |remaining, option|
  14.         remaining.select { |dino|
  15.           dino.send(option[0]).send(option[2] || :==, option[1])
  16.         }
  17.       end
  18.     end
  19.  
  20. def main
  21.   $DINOS = [
  22.     Dino.new("Stegosaur", :jurassic, 500),
  23.     Dino.new("T-rex", :cretaceous, 1000),
  24.     Dino.new("Apatosaur", :cretaceous, 700)
  25.   ]
  26.  
  27.   # Output shortened
  28.  
  29.   test_zero = filter()
  30.   p "Test zero" + test_zero.inspect # Stegosaur, T-Rex, Apatosaur
  31.  
  32.   test_one = filter( [ [:size, 750, :< ] ] )
  33.   p "Test one (size < 750): " + test_one.inspect # Stegosaur, Apatosaur
  34.  
  35.   test_two = filter( [ [:era, :cretaceous]])
  36.   p "Test two (era = cretaceous): " + test_two.inspect # T-rex, Apatosaur
  37.  
  38.   test_three = filter( [[:era, :cretaceous], [:size, 800, :<]])
  39.   p "Test three (era = cretaceous & size < 550): " + test_three.inspect # Apatosaur
  40.  
  41. end
  42.  
  43. main
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top