This course will be retired on June 1, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
Ruby makes heavy use of blocks in the built in types and the standard library. In this video, we explore Arrays and Hashes.
Documentation Links
Code Samples
Given the following array:
array = [1, 2, 3]
Array#each
array.each { |item| print "-#{item}-" }
Prints out:
-1--2--3-
Array#select:
array.select { |item| item > 2 }
This returns a new array with the following:
[3]
Array#delete_if
array.delete_if { |item| item == 1 }
The array is now:
[2, 3]
Array#reject
array.reject { |item| item % 3 == 0 }
The above returns a new array: [1, 2]
Array#count
array.count
The above returns 3
. But count
can also be passed a block:
array.count { |item| item % 3 == 0 }
The above statement returns 1.
Hashes
Given the following hash:
hash = { 'name' => 'Jason', 'location' => 'Treehouse' }
Hash#each
hash.each do |key, value|
puts "key: #{key} value: #{value}"
end
Prints the following:
key: name value: Jason
key: location value: Treehouse
Hash#each_key:
hash.each_key{ |key| puts "key: #{key}" }
Prints the following:
key: name
key: location
Hash#each_value:
hash.each_value { |val| puts "val: #{val}" }
Prints the following:
val: Jason
val: Treehouse
Hash#keep_if
hash.keep_if{ |key, val| key == "name" }
The hash is now:
{ 'name' => 'Jason' }
Hash#reject
hash.reject { |key, val| key == "name" }
The hash is now:
{}
Hash#select
hash.select { |key, val| key == "name" }
Returns a new hash:
{ 'name' => 'Jason' }
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up