08. Ruby Enumerable - Ruby Core Teaching

quanhoangd129 129 views 16 slides Jul 19, 2024
Slide 1
Slide 1 of 16
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16

About This Presentation

Ruby Enumerable


Slide Content

RUBY
ENUMERABLE
Hoang Quan(BlazingRockStorm)

Contents
OVERVIEW
EACH_WITH_INDEX
EACH METHOD
TRANSFORMING ARRAYS WITH MAP
THE SELECT AND INJECT METHODS

Overview

Overview
The Enumerable module provides a set of
methods to traverse, search, sort and
manipulate collections.
With Enumerable, you can write prettier,
fewer lines of code.

each method

each method
Loop using for

each method
Convert to each

each_with_index

each_with_in
dex
The Enumerable module has the convenient
each_with_index, which has an extra argument to
refer to the index:

Transforming arrays with
map

Transforming
arrays with
map
Create a transformed copy of array without
altering the original

Transforming
arrays with
map
modify an array, you can use map!

The select and inject
methods

select
This useful method takes in one argument. The block you pass it should be some
kind of true/false test. If the expression results in true for an element in an array,
that element is kept as part of the returned collection

inject
The inject method takes a collection and reduces it to a single value, such as a
sum of values:

Thank you!
ありがとうございます!