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

단축키

Prev이전 문서

Next다음 문서

+ - Up Down Comment Print
?

단축키

Prev이전 문서

Next다음 문서

+ - Up Down Comment Print

http://queirozf.com/reminders/ruby-map-each-collect-inject-reject-select-quick-reference


  • map

    performs an action on each array element. The original array is not modified. Returns the modified array.

    irb> [1,2,3,4,5,6,7,8,9,10].map{|e| e*3}
    => [3, 6, 9, 12, 15, 18, 21, 24, 27, 30]
  • each

    executes an action using as parameter each element of the array. Returns the unmodified array.

    irb> [1,2,3,4,5,6,7,8,9,10].each{|e| print e.to_s+"!"}
    1!2!3!4!5!6!7!8!9!10!
    =>[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  • collect

    synonym for map

  • inject

    takes an accumulator (sum) and changes it as many times as there are elements in the array. Returns the final value of the accumulator.

    irb> [1,2,3,4,5,6,7,8,9,10].inject{|sum,e|sum += e}
    => 55
  • select

    runs an expression for each array element and, if it is true, that element gets added to the output which is returned.

    irb> [1,2,3,4,5,6,7,8,9,10].select{|el| el%2==0}
    => [2,4,6,8,10]
  • reject

    The opposite of select: runs an expression for each array element and includes that element in the output if the expression is false

    irb> [1,2,3,4,5,6,7,8,9,10].reject{|e| e==2 || e==8}
    => [1, 3, 4, 5, 6, 7, 9, 10]



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

Designed by sketchbooks.co.kr / sketchbook5 board skin

나눔글꼴 설치 안내


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

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

설치 취소

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5