Last active
November 6, 2015 15:41
-
-
Save kylekeesling/91e500b05dec24d4b552 to your computer and use it in GitHub Desktop.
Example of a Complex Array of Hashes and how to use Enumerable methods on it
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$pets = [] | |
$pets << { | |
:name => 'Lallo', | |
:nocturnal => false, | |
:breed => 'Schnauzer', | |
:talents => ['napping', 'rolling over', 'playing dead'], | |
:legs => 4 | |
} | |
$pets << { | |
:name => 'Buckminster', | |
:nocturnal => false, | |
:breed => 'Scottish Fold Kitty', | |
:talents => ['napping', 'ignoring humans', 'singing'], | |
:legs => 4 | |
} | |
$pets << { | |
:name => 'Roro', | |
:nocturnal => true, | |
:breed => 'African Grey Parrot', | |
:talents => ['mimicry', 'singing'], | |
:legs => 2 | |
} | |
$pets << { | |
:name => 'Little Jade', | |
:nocturnal => true, | |
:breed => 'Box Turtle', | |
:talents => ['swimming', 'napping'], | |
:legs => 4 | |
} | |
$pets << { | |
:name => 'Cometa', | |
:nocturnal => false, | |
:breed => 'Slug', | |
:talents => ['ignoring humans', 'being squishy'], | |
:legs => 0 | |
} | |
# 2 different ways to find how many of our pets are nocturnal | |
puts "Number of Noctural Pets: #{$pets.reject{|p| p[:nocturnal] == false}.count}" | |
puts "Number of Noctural Pets: #{$pets.select{|p| p[:nocturnal] == true}.count}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here are the Rubydocs for Enumerables - http://ruby-doc.org/core-2.0.0/Enumerable.html