Join Kevin Skoglund for an in-depth discussion in this video Collect, part of Ruby Essential Training.
In this movie we are going to be taking a look at Ruby's Collect method, which…also makes use of a code block.…Collect has a synonym which is Map.…It's really up to you as to which one you use.…Sometimes I use map just when I am trying to conserve space, maybe keep it from…wrapping around the line or something like that, or maybe I am feeling lazy.…The Collect and Map are completely synonymous.…We can use either one, so use the one that feels most comfortable to you.…The Collect or Map method is going to really work the best with arrays,…hashes, and ranges.…An array is really the object that you will use it most often with, but hashes…and ranges also work.…
So I'll open up a new irb session, simple prompt.…Now, let's create ourselves an array to start with.…So array = 1, 2, 3, 4, 5, nice, simple array. array.collect and then our code block.…We are going to have a block variable declared.…Let's call it i for now.…So what Collect does is it iterates through the array, taking each item and…passing it up to i, then does whatever we tell it to do in our code block,…
- Using Ruby in the Interactive Ruby Shell and in standalone scripts
- Learning to write custom code blocks to find, merge, and sort
- Using modules for namespacing or as mix-ins
- Reading from and writing to files
- Creating a full Ruby project from start to finish
Skill Level Beginner
1. Getting Started with Ruby
2. Ruby Object Types
3. Control Structures
4. Code Blocks
8. Working with Files
9. Ruby Project: Creating the Food Finder
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.