본문 바로가기
조회 수 1560 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

+ - Up Down Comment Print
?

단축키

Prev이전 문서

Next다음 문서

+ - Up Down Comment Print

http://nikitasinghblogger.blogspot.sg/2013/08/difference-between-collect-select-map.html?m=1


To iterate over an array we generally use collect, select, map and each. As I am beginner I never actually thought what is the difference and I kept using EACH for every loop iteration. However, recently I researched a bit more about these loops and thought to share it . It might help some one I guess  :)

1) Map

Map takes the enumerable object and a block like this [1,2,3].map { |n| n*2 } and evaluates the block for each element and then return a new array with the calculated values.

so the outcome of  [1,2,3].map { |n| n*2 } will be [2,4,6]

If you are try to use map to select any specific values like where n >2 then it will evaluate each element and will output only the result which will be either true or false

so the outcome of  [1,2,3].map { |n| n>2 } will be [false, false, true]

2) Collect (= Map)

Collect is similar to Map which takes the enumerable object and a block like this [1,2,3].collect { |n| n*2 } and evaluates the block for each element and then return a new array with the calculated values.

so the outcome [1,2,3].collect{ |n| n*2 } of will be [2,4,6]

If you are try to use collect to select any specific values like where n >2 then it will evaluate each element and will output only the result which will be either true or false

so the outcome of  [1,2,3].collect { |n| n>2 } will be [false, false, true]

3) Select

Select evaluates the block with each element for which the block returns true.

so the outcome of  [1,2,3].select { |n| n*2 } will be [1,2,3]

If you are try to use select to get any specific values like where n >2 then it will evaluate each element but returns the original array

so the outcome of  [1,2,3].select{ |n| n>2 } will be [3]

4) Each

Each will evaluate the block with each array and will return the original array not the calculated one.
so the outcome of  [1,2,3].each{ |n| n*2 } will be [1,2,3]

If you are try to use each to select any specific values like where n >2 then it will evaluate each element but returns the original array

so the outcome of  [1,2,3].each { |n| n>2 } will be [1,2,3]

Title
List of Articles
번호 제목 글쓴이 날짜 조회 수
31 Different Ways to Set Attributes in ActiveRecord (attribute, create, update, validation) Hojung 2015.05.24 1638
30 Ruby Style Guide Hojung 2015.02.05 2016
29 Refresh content automatically after some period time on Rails Hojung 2015.01.22 2566
28 Install ruby-filemagic on Mac (brew, libmagic) Hojung 2014.12.03 1811
27 Byte manipulation in Ruby Hojung 2014.12.03 1723
26 How to use Github for your project file Hojung 2014.09.24 1394
25 Ruby map, each, collect, inject, reject, select quick reference Hojung 2014.06.10 2071
» Difference between collect, select, map and each in ruby Hojung 2014.06.10 1560
23 remove nil from array (compact, compact!) Hojung 2014.06.10 1864
22 remove child association model when parent removed (rails-dependent-destroy) Hojung 2014.06.03 1946
21 deploying cron jobs with whenever gem Hojung 2014.06.01 1975
20 Get list of a class' instance methods Hojung 2014.06.01 1651
19 Optional method parameters in Ruby Hojung 2014.05.29 1762
18 Best Ruby On Rails Content Management Systems (CMS) Hojung 2014.04.26 2945
17 The simple way to print exceptions in Ruby Hojung 2014.04.07 1770
16 Ruby Symbols Hojung 2014.04.05 6547
15 What is the difference between a Symbol and String? Hojung 2014.04.05 1737
14 install Rbenv, ruby and rails Hojung 2014.03.22 1955
13 HTTP Posts in Ruby Hojung 2014.03.14 1957
12 optparse: mandatory and optional arguments example Hojung 2014.03.14 2666
Board Pagination ‹ Prev 1 2 Next ›
/ 2

Designed by sketchbooks.co.kr / sketchbook5 board skin

나눔글꼴 설치 안내


이 PC에는 나눔글꼴이 설치되어 있지 않습니다.

이 사이트를 나눔글꼴로 보기 위해서는
나눔글꼴을 설치해야 합니다.

설치 취소

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5