Skip to content

Instantly share code, notes, and snippets.

@kylekeesling
Last active November 6, 2015 15:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kylekeesling/91e500b05dec24d4b552 to your computer and use it in GitHub Desktop.
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
$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}"
@kylekeesling
Copy link
Author

Here are the Rubydocs for Enumerables - http://ruby-doc.org/core-2.0.0/Enumerable.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment