> For the complete documentation index, see [llms.txt](https://certil-remy.gitbook.io/learn/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://certil-remy.gitbook.io/learn/ruby/untitled-45.md).

# Hash Iteration with Collect

## Overview

We'll use `#collect` to iterate over a hash.

## Objectives

1. Identify when `#collect` is most often used when iterating over hashes.
2. Give examples of using `#collect` to iterate over a hash.

## Using `#collect`

*Note: `#collect` is actually an alias for `#map`. That means the two methods can be used interchangeably, and effect the same behavior*.

We use `#collect` to iterate over a collection of data, such as an array or a hash, and return a collection of the data therein.

We have seen it used with arrays to iterate over an array, operate on the data it contains, and return a collection with this altered data.

When working with hashes, we'll most often see `#collect` used to collect the values of the hash's keys and/or collect data that we've operated on over the course of an iteration.

Let's take a look at an example.

### `#collect`ing Hash Values

Let's use `#collect` to return all of the values of the keys in a given hash.

In this example, we are once again the managers at Chuck E. Cheese's. Chuck E. Cheese's is still a great place to have a birthday party, and, shockingly, the same three birthdays are still going on.

We will be operating on the following hash that tracks birthday kids and their associated ages:

```
birthday_kids = {
	"Timmy" => 9, 
	"Sarah" => 6, 
	"Amanda" => 27
}
```

Our managers have asked us to give them the list of ages of the birthday kids so they know how many candles to buy for the birthday cakes. Let's iterate over the `birthday_kids` hash and collect the ages.

```
birthday_kids = {
	"Timmy" => 9, 
	"Sarah" => 6, 
	"Amanda" => 27
}

birthday_kids.collect do |kids_name, age|
	age
end

# => [9, 6, 27]
```

Note that the return value is an *array* of the values we collected.

## Advanced Example

You might have noticed that our previous example didn't operate on the data we were collecting. Let's step it up a level. In this example, we'll iterate over the `birthday_kids` hash using `#collect` and return the age of each child, in dog years:

```
birthday_kids.collect do |name, age|
	age * 7
end
```

In this case, we are multiplying the value of each `age` by `7` and collecting the return values of that multiplication into a new array. The above method call would return:

View [Hash Iteration with #collect](https://learn.co/lessons/hash-iteration-collect) on Learn.co and start learning to code for free.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://certil-remy.gitbook.io/learn/ruby/untitled-45.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
