What is different between each and the collection method in Ruby

From this code, I do not know the difference between the two methods, collect and each .

 a = ["L","Z","J"].collect{|x| puts x.succ} #=> M AA K print a.class #=> Array b = ["L","Z","J"].each{|x| puts x.succ} #=> M AA K print b.class #=> Array 
+56
ruby each collect
Mar 18 2018-11-11T00:
source share
7 answers

Array#each takes an array and applies the given block to all elements. It does not affect the array or creates a new object. This is just a way to iterate over elements. He also returns himself.

  arr=[1,2,3,4] arr.each {|x| puts x*2} 

Prints 2,4,6,8 and returns [1,2,3,4] no matter what

Array#collect same as Array#map and applies this block of code to all elements and returns a new array. just put 'Projects each element of the sequence into a new shape

  arr.collect {|x| x*2} 

Returns [2,4,6,8]

And in your code

  a = ["L","Z","J"].collect{|x| puts x.succ} #=> M AA K 

a is an array, but itโ€™s actually an array from Nil [nil, nil, nil] , because puts x.succ returns nil (even if it prints M AA K).

AND

  b = ["L","Z","J"].each{|x| puts x.succ} #=> M AA K 

also an array. But its value is ["L", "Z", "J"], because it returns self.

+98
Mar 18 2018-11-11T00:
source share

Array#each simply takes each element and puts it in a block, and then returns the original array. Array#collect takes each element and places it in a new array, which is returned:

 [1, 2, 3].each { |x| x + 1 } #=> [1, 2, 3] [1, 2, 3].collect { |x| x + 1 } #=> [2, 3, 4] 
+35
Mar 18 '11 at 4:10
source share

each is when you want to iterate over an array and do whatever you want in each iteration. In most (imperative) languages, this "one size fits all" hammers, which programmers strive for when you need to process a list.

For more functional languages, you only do such a generic iteration if you cannot do it otherwise. In most cases, a card or abbreviation will be more appropriate (collect and enter in ruby).

collect is when you want to turn one array into another array

inject is when you want to turn an array into a single value

+5
Mar 18 '11 at 4:27
source share

Here are two pieces of source code, according to docs ...

 VALUE rb_ary_each(VALUE ary) { long i; RETURN_ENUMERATOR(ary, 0, 0); for (i=0; i<RARRAY_LEN(ary); i++) { rb_yield(RARRAY_PTR(ary)[i]); } return ary; } # .... .... .... .... .... .... .... .... .... .... .... .... static VALUE rb_ary_collect(VALUE ary) { long i; VALUE collect; RETURN_ENUMERATOR(ary, 0, 0); collect = rb_ary_new2(RARRAY_LEN(ary)); for (i = 0; i < RARRAY_LEN(ary); i++) { rb_ary_push(collect, rb_yield(RARRAY_PTR(ary)[i])); } return collect; } 

rb_yield() returns the value returned by the block ( see also this metaprogramming blog post ).

So, each simply returns and returns the original array, and collect creates a new array and pushes the block results to it; then it returns this new array.

Source Fragments: each , collect

+2
Mar 18 '11 at 4:18
source share

The difference is that it returns. In the above example, a == [nil,nil,nil] (puts x.succ value), and b == ["L", "Z", "J"] (source array)

From ruby-doc collects the following:

Calls a block once for each element independently. Creates a new array containing the values โ€‹โ€‹returned by the block.

Each always returns the original array. Has the meaning?

+1
Mar 18 '11 at 4:14
source share

Each of them is a method defined by all classes that include the Enumerable module. Object.each returns an Enumerable::Enumerator object. This is what other Enumerable methods use to iterate through an object. each methods of each class behave differently.

In the Array class, when a block is passed to each , it executes block instructions for each element, but returns self.This is useful when you donโ€™t need an array, but you probably just want to select elements from the array and use arguments as other methods. inspect and map returns a new array with returned block execution values โ€‹โ€‹for each element. You can use map! and collect! to perform operations with the original array.

0
Nov 01 '11 at 10:17
source share

I think an easier way to understand this would be as follows:

 nums = [1, 1, 2, 3, 5] square = nums.each { |num| num ** 2 } # => [1, 1, 2, 3, 5] 

Instead, if you use collect:

 square = nums.collect { |num| num ** 2 } # => [1, 1, 4, 9, 25] 

And plus, you can use .collect! to modify the original array.

0
Oct 21 '14 at 6:09
source share



All Articles