Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Dino
- attr_reader :name, :era, :size
- def initialize(name, era, size)
- @name = name
- @era = era
- @size = size
- end
- end
- def filter(options = [])
- options.inject($DINOS) do |remaining, option|
- remaining.select { |dino|
- dino.send(option[0]).send(option[2] || :==, option[1])
- }
- end
- end
- def main
- $DINOS = [
- Dino.new("Stegosaur", :jurassic, 500),
- Dino.new("T-rex", :cretaceous, 1000),
- Dino.new("Apatosaur", :cretaceous, 700)
- ]
- # Output shortened
- test_zero = filter()
- p "Test zero" + test_zero.inspect # Stegosaur, T-Rex, Apatosaur
- test_one = filter( [ [:size, 750, :< ] ] )
- p "Test one (size < 750): " + test_one.inspect # Stegosaur, Apatosaur
- test_two = filter( [ [:era, :cretaceous]])
- p "Test two (era = cretaceous): " + test_two.inspect # T-rex, Apatosaur
- test_three = filter( [[:era, :cretaceous], [:size, 800, :<]])
- p "Test three (era = cretaceous & size < 550): " + test_three.inspect # Apatosaur
- end
- main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement