Enumerables: A (Complete) Beginner’s Guide

array.enumerable_method{|thing| thing.method}

.each

nps = [“Zion”, “Glacier”, “Shenandoah”, “Great Smoky Mountains”, “Bryce Canyon”, “Grand Teton”, “Hot Springs”, “Joshua Tree”, “Grand Canyon”, “Rocky Mountain”]
nps.each{|park| puts "#{park} is beautiful!"}
Results of calling .each on the nps array

.map

nps.map{|park| park.downcase}
.map allows us to modify our array.

.select

nps.select{|park| park.length == 11}
.select allows us to filter values in an array.

.find

nps.find{|park| park.length == 11}
.find will only return one value.

.count

nps.count{|park| park.start_with? “G”}
.count… well counts.

.sort and .sort_by

nps.sort.reverse
.sort will put the array in alphabetical order. .reverse will reverse that.
nps.sort_by{|park| park.length}
Now all of my Parks are sorted from shortest to longest!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store