# Breaking Down the Block

**URL:** <https://www.ruby-forum.com/t/breaking-down-the-block/222862>\
**Category:** Ruby\
**Created:** [September 13, 2012, 4:09am UTC](https://www.ruby-forum.com/t/breaking-down-the-block/222862 "2012-09-13T04:09:24Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![aris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aris](https://www.ruby-forum.com/u/aris)\
**Post date:** [September 13, 2012, 4:09am UTC](https://www.ruby-forum.com/t/breaking-down-the-block/222862/1 "2012-09-13T04:09:24Z")

</div>

Can someone please explain in plain english how this block treats the  
given variables (and values in the array)? I am struggling to understand  
the relationship of ‘t’ and ‘n’ with the array.

class Array  
def sum  
inject(0) {|t,n| t + n}  
end  
end  
puts [1,2,3].sum

Is ‘t’ effectively labeled as ‘0’ (zero), given the assignment on the  
inject method?  
What “is” ‘t’ exactly?

---

<div class="post-metadata">

**Author:** ![incagneato](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/db5fbb/32.png) [@incagneato](https://www.ruby-forum.com/u/incagneato)\
**Post date:** [September 13, 2012, 4:30am UTC](https://www.ruby-forum.com/t/breaking-down-the-block/222862/2 "2012-09-13T04:30:40Z")

</div>

`my_array.inject(0) {|t,n| t + n}` basically breaks down like:

```
t = 0
my_array.each {|n| t = (t + n) }
t

```

Similary, leaving out the inital value, `my_array.inject {|t,n| t + n}` basically breaks down like:

```
t = my_array[0]
my_array[1..-1].each {|n| t = (t + n) }
t

```

What you called ‘t’ (often called the ‘memory’) is the accumulation,  
or the result of the last iteration of the block.

On 13 September 2012 12:09, incag neato [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Is ‘t’ effectively labeled as ‘0’ (zero), given the assignment on the  
> inject method?  
> What “is” ‘t’ exactly?
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

–  
Matthew K., B.Sc (CompSci) (Hons)  
[http://matthew.kerwin.net.au/](http://matthew.kerwin.net.au/)  
ABN: 59-013-727-651

“You’ll never find a programming language that frees  
you from the burden of clarifying your ideas.” - xkcd

---

<div class="post-metadata">

**Author:** ![incagneato](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/db5fbb/32.png) [@incagneato](https://www.ruby-forum.com/u/incagneato)\
**Post date:** [September 13, 2012, 5:46am UTC](https://www.ruby-forum.com/t/breaking-down-the-block/222862/3 "2012-09-13T05:46:20Z")

</div>

incag neato wrote in post #1075712:

> Can someone please explain in plain english how this block treats the  
> given variables (and values in the array)? I am struggling to understand  
> the relationship of ‘t’ and ‘n’ with the array.
> 
> class Array  
> def sum  
> inject(0) {|t,n| t + n}  
> end  
> end  
> puts [1,2,3].sum
> 
> Is ‘t’ effectively labeled as ‘0’ (zero), given the assignment on the  
> inject method?  
> What “is” ‘t’ exactly?

Just forget inject() even exists. It’s really quite a pathetic method.

---

<div class="post-metadata">

**Author:** ![incagneato](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/db5fbb/32.png) [@incagneato](https://www.ruby-forum.com/u/incagneato)\
**Post date:** [September 13, 2012, 5:50am UTC](https://www.ruby-forum.com/t/breaking-down-the-block/222862/4 "2012-09-13T05:50:44Z")

</div>

Think of it this way:

inject(memo) { | memo, enum | block sets memo } =\> memo

So memo has three stages:

1. Before the first iteration, memo is set to the value of the parameter  
(in parentheses after “inject”).

2. After each iteration, the result of the block becomes the new value  
of memo.

3. When it’s all over, the value of memo is the result.

So it’s all about accumulating results into memo. (It is sometimes  
called the accumulator.)

In your example, the accumulation involves adding the successive values  
of enum to the accumulator. The successive values of enum are the values  
of the array. So the result is the sum of the array. (It’s the sum of  
the array plus the initial value of the memo, but that initial value is  
zero, so it really is just the sum.)

m.

---

<div class="post-metadata">

**Author:** ![incagneato](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/db5fbb/32.png) [@incagneato](https://www.ruby-forum.com/u/incagneato)\
**Post date:** [September 13, 2012, 5:55am UTC](https://www.ruby-forum.com/t/breaking-down-the-block/222862/5 "2012-09-13T05:55:16Z")

</div>

However, with better variable names, it might be clearer what’s going on  
with inject:

class Array  
def sum1  
inject(0) {|total, array\_element| total += array\_element}  
end

def sum2  
total = 0

```
self.each do |array_element|
  total += array_element
end

total

```

end

def multiply\_em  
inject(1) {|total, array\_element| total \*= array\_element}  
end

end

puts [1, 2, 3].sum1  
puts [1, 2, 3].sum2  
puts [1, 2, 3].multiply\_em

–output:–  
6  
6  
6

---

<div class="post-metadata">

**Author:** ![incagneato](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/db5fbb/32.png) [@incagneato](https://www.ruby-forum.com/u/incagneato)\
**Post date:** [September 13, 2012, 6:04am UTC](https://www.ruby-forum.com/t/breaking-down-the-block/222862/6 "2012-09-13T06:04:47Z")

</div>

Or even better:

class Array  
def sum1  
initial\_val\_for\_total = 0  
inject(initial\_val\_for\_total) {|total, array\_element| total +=  
array\_element}  
end  
end

---

<div class="post-metadata">

**Author:** ![incagneato](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/db5fbb/32.png) [@incagneato](https://www.ruby-forum.com/u/incagneato)\
**Post date:** [September 13, 2012, 6:13am UTC](https://www.ruby-forum.com/t/breaking-down-the-block/222862/7 "2012-09-13T06:13:31Z")

</div>

On Thu, Sep 13, 2012 at 12:46:23PM +0900, 7stud – wrote:

> Just forget inject() even exists. It’s really quite a pathetic method.

inject is just reduce/foldl from other languages. It is just sugar for  
building an accumulator in a foreach loop, but it has a long history.

---

<div class="post-metadata">

**Author:** ![incagneato](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/db5fbb/32.png) [@incagneato](https://www.ruby-forum.com/u/incagneato)\
**Post date:** [September 13, 2012, 9:42am UTC](https://www.ruby-forum.com/t/breaking-down-the-block/222862/8 "2012-09-13T09:42:21Z")

</div>

Sung P. wrote in post #1075726:

> On Thu, Sep 13, 2012 at 12:46:23PM +0900, 7stud – wrote:
> 
> > Just forget inject() even exists. It’s really quite a pathetic method.
> 
> inject is just reduce/foldl from other languages. It is just sugar for  
> building an accumulator in a foreach loop, but it has a long history.

It’s also a baby step towards “functional programming” - realising that  
you don’t actually need mutable variables to do useful computation,  
which in turn frees you from certain kinds of programming logic errors.

I really like how Ruby lets you do things like this on a small scale,  
without having to go full-out functional.

---

<div class="post-metadata">

**Author:** ![incagneato](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/db5fbb/32.png) [@incagneato](https://www.ruby-forum.com/u/incagneato)\
**Post date:** [September 13, 2012, 12:46pm UTC](https://www.ruby-forum.com/t/breaking-down-the-block/222862/9 "2012-09-13T12:46:46Z")

</div>

2012/9/13 7stud – [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Or even better:
> 
> class Array  
> def sum1  
> initial\_val\_for\_total = 0  
> inject(initial\_val\_for\_total) {|total, array\_element| total +=  
> array\_element}  
> end  
> end

Sorry, but that’s bad practice in best case. #inject doesn’t depend on  
the total being assigned to by the block; it depends on the value  
returned by the block. Your code works, but somebody may be tempted to  
do something like adding “p array\_element” at the end and suddenly it  
breaks for no obvious reason, if somebody believed the assignment was  
essential.

– Matma R.

---

<div class="post-metadata">

**Author:** ![incagneato](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/db5fbb/32.png) [@incagneato](https://www.ruby-forum.com/u/incagneato)\
**Post date:** [September 13, 2012, 12:55pm UTC](https://www.ruby-forum.com/t/breaking-down-the-block/222862/10 "2012-09-13T12:55:33Z")

</div>

On Thu, Sep 13, 2012 at 5:55 AM, 7stud – [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> However, with better variable names, it might be clearer what’s going on  
> with inject:
> 
> class Array  
> def sum1  
> inject(0) {|total, array\_element| total += array\_element}  
> end

The assignment is totally superfluous.

> def sum2  
> total = 0
> 
> ```
> self.each do |array_element|
> total += array_element
> end
> 
> total
> 
> ```
> 
> end

Here “self.” is superfluous. 😉

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![incagneato](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/db5fbb/32.png) [@incagneato](https://www.ruby-forum.com/u/incagneato)\
**Post date:** [September 14, 2012, 6:02am UTC](https://www.ruby-forum.com/t/breaking-down-the-block/222862/11 "2012-09-14T06:02:34Z")

</div>

Robert K. wrote in post #1075769:

> On Thu, Sep 13, 2012 at 5:55 AM, 7stud – [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > However, with better variable names, it might be clearer what’s going on  
> > with inject:
> > 
> > class Array  
> > def sum1  
> > inject(0) {|total, array\_element| total += array\_element}  
> > end
> 
> The assignment is totally superfluous.

Whoops. Yep, otherwise it wouldn’t be such a strange method.

> > def sum2  
> > total = 0
> > 
> > ```
> > self.each do |array_element|
> > total += array_element
> > end
> > 
> > total
> > 
> > ```
> > 
> > end
> 
> Here “self.” is superfluous. 😉

I write it anyway. In my opinion, it’s 10 times clearer with an  
explicit receiver.

---

<div class="post-metadata">

**Author:** ![incagneato](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/db5fbb/32.png) [@incagneato](https://www.ruby-forum.com/u/incagneato)\
**Post date:** [September 17, 2012, 3:06am UTC](https://www.ruby-forum.com/t/breaking-down-the-block/222862/12 "2012-09-17T03:06:21Z")

</div>

This is quite helpful. If we ever meet I owe you each a beer.

In my newbie opinion, Matt N. did a fantastic job above:

> Think of it this way:

> inject(memo) { | memo, enum | block sets memo } =\> memo

> So memo has three stages:

> 1. Before the first iteration, memo is set to the value of the parameter  
> (in parentheses after “inject”).

> 1. After each iteration, the result of the block becomes the new value  
> of memo.

> 1. When it’s all over, the value of memo is the result.

> So it’s all about accumulating results into memo. (It is sometimes  
> called the accumulator.)

What I’m really trying to get at with this question however, is  
digesting something more like the following (and yes, I know it gets  
slightly more complicated when I introduce a hash..and a map).

def mode(array)  
recur = array.inject(Hash.new(0)) {|k,v| k[v]+=1; k}.sort\_by { | (a,b)  
| -b }  
recur.select { | (a,b) | b == recur[0][1] }.map{ |(a,b) | a }  
end

This is meant to find the mode of an array, but I can’t understand it  
clearly. HELP!

---

<div class="post-metadata">

**Author:** ![incagneato](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/db5fbb/32.png) [@incagneato](https://www.ruby-forum.com/u/incagneato)\
**Post date:** [September 17, 2012, 6:25am UTC](https://www.ruby-forum.com/t/breaking-down-the-block/222862/13 "2012-09-17T06:25:53Z")

</div>

z = y.select {|a, b| b == y[0][1]}  
p z

–output:–  
[[30, 2]]

In this example, the conditional b == y[0][1] is equivalent to b == 2.  
If instead you had results like this,

[[10, 3], [30, 3], [20, 1], [40, 1]]

…then the conditional would be b == 3, which just selects all the  
numbers that tie for the most appearances in the original array.

z = z.map {|a, b| a}  
p z

–output:–  
[30]

Finally, map selects the first element of all the sub arrays, where the  
first element is a number that appeared the most times in the original  
array.

---

<div class="post-metadata">

**Author:** ![incagneato](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/db5fbb/32.png) [@incagneato](https://www.ruby-forum.com/u/incagneato)\
**Post date:** [September 17, 2012, 6:03am UTC](https://www.ruby-forum.com/t/breaking-down-the-block/222862/14 "2012-09-17T06:03:07Z")

</div>

> recur = array.inject(Hash.new(0)) {|k,v| k[v]+=1; k}

Horrible variable names are the culprit again.

hash = {}  
p hash[10]

–output:–  
nil

hash = Hash.new(0)  
p hash[10]

–output:–  
0

array = [10, 20, 30, 30, 40]

result = array.inject(Hash.new(0)) {|hash, array\_element|  
hash[array\_element] += 1; hash}

p result

–output:–  
{10=\>1, 20=\>1, 30=\>2, 40=\>1}

x = result.sort\_by {|key, val| val}  
p x

–output:–  
[[10, 1], [20, 1], [40, 1], [30, 2]]

y = result.sort\_by {|key, val| -val}  
p y

–output:–  
[[30, 2], [10, 1], [20, 1], [40, 1]]

---

<div class="post-metadata">

**Author:** ![incagneato](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/db5fbb/32.png) [@incagneato](https://www.ruby-forum.com/u/incagneato)\
**Post date:** [September 17, 2012, 6:52am UTC](https://www.ruby-forum.com/t/breaking-down-the-block/222862/15 "2012-09-17T06:52:30Z")

</div>

def mode(array)  
freq\_for = Hash.new(0)  
array.each {|number| freq\_for[number] += 1}

nums\_with\_frequs = freq\_for.sort\_by {|key, val| -val}  
top\_count = nums\_with\_frequs[0][1]  
most\_seen = nums\_with\_frequs.take\_while {|number, freq| freq ==  
top\_count}

most\_seen.map {|number, freq| number}  
end

p mode([10, 10, 10, 20, 30, 30, 40, 40, 40])

–output:–  
[10, 40]

Which do you prefer? Saving a few lines of code, or writing clear code?

---

<div class="post-metadata">

**Author:** ![incagneato](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/db5fbb/32.png) [@incagneato](https://www.ruby-forum.com/u/incagneato)\
**Post date:** [September 17, 2012, 10:34pm UTC](https://www.ruby-forum.com/t/breaking-down-the-block/222862/16 "2012-09-17T22:34:01Z")

</div>

incag neato wrote in post #1076331:

> When I call “top\_count = nums\_with\_frequs[0][1]” am I referring to the  
> first key and value in the sorted hash “freq\_for” (i.e. the var  
> “nums\_with\_frequs”)?
> 
> I’ve played around with it but I don’t see how to use “b == 3” to arrive  
> at the tied values.

In my example:

nums\_with\_frequs = [[30,2]]

…which you could have easily determined yourself by printing it out.

---

<div class="post-metadata">

**Author:** ![incagneato](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/db5fbb/32.png) [@incagneato](https://www.ruby-forum.com/u/incagneato)\
**Post date:** [September 18, 2012, 5:55am UTC](https://www.ruby-forum.com/t/breaking-down-the-block/222862/17 "2012-09-18T05:55:33Z")

</div>

Sorry, didn’t word my question very clearly.  
In your previous example, you printed the double mode, [10, 40].  
How could you make the program print [40, 10]?

(It’s the “b == 3” comment you made that I’m curious about.)  
And how it would pertain to this code of yours:

def mode(array)  
freq\_for = Hash.new(0)  
array.each {|number| freq\_for[number] += 1}

nums\_with\_frequs = freq\_for.sort\_by {|key, val| -val}  
top\_count = nums\_with\_frequs[0][1]  
most\_seen = nums\_with\_frequs.take\_while {|number, freq| freq ==  
top\_count}

most\_seen.map {|number, freq| number}  
end

p mode([10, 10, 10, 20, 30, 30, 40, 40, 40])

–output:–  
[10, 40]

---

<div class="post-metadata">

**Author:** ![incagneato](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/db5fbb/32.png) [@incagneato](https://www.ruby-forum.com/u/incagneato)\
**Post date:** [September 17, 2012, 7:29pm UTC](https://www.ruby-forum.com/t/breaking-down-the-block/222862/18 "2012-09-17T19:29:18Z")

</div>

Wow, so helpful. Thanks for taking so much of your time to explain.  
That must’ve taken at least an hour.

Now, the one question I have left pertains to the following (the tied  
values (double mode)).

* * *

> 7stud – wrote in post #1076263:  
> z = y.select {|a, b| b == y[0][1]}  
> p z

> –output:–  
> [[30, 2]]

> In this example, the conditional b == y[0][1] is equivalent to b == 2.  
> If instead you had results like this,

> [[10, 3], [30, 3], [20, 1], [40, 1]]

> …then the conditional would be b == 3, which just selects all the  
> numbers that tie for the most appearances in the original array.

* * *

How is “b == y[0][1]” similar to “b == 2” ?  
Do you mean the value in the sorted hash?

When I call “top\_count = nums\_with\_frequs[0][1]” am I referring to the  
first key and value in the sorted hash “freq\_for” (i.e. the var  
“nums\_with\_frequs”)?

I’ve played around with it but I don’t see how to use “b == 3” to arrive  
at the tied values.

---

<div class="post-metadata">

**Author:** ![incagneato](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/db5fbb/32.png) [@incagneato](https://www.ruby-forum.com/u/incagneato)\
**Post date:** [September 18, 2012, 7:06am UTC](https://www.ruby-forum.com/t/breaking-down-the-block/222862/19 "2012-09-18T07:06:19Z")

</div>

> In your previous example, you printed the double mode, [10, 40].  
> How could you make the program print [40, 10]?

Start at the top of the thread. Read every post, and write down every  
method in every post. See if you can’t find a method that will do that.

---

<div class="post-metadata">

**Author:** ![incagneato](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/db5fbb/32.png) [@incagneato](https://www.ruby-forum.com/u/incagneato)\
**Post date:** [September 18, 2012, 7:13am UTC](https://www.ruby-forum.com/t/breaking-down-the-block/222862/20 "2012-09-18T07:13:02Z")

</div>

> (It’s the “b == 3” comment you made that I’m curious about.)

1. If x = 2, then the statement:

result = x + 4

is equivalent to:

result = 6.

1. 

> > the conditional b == y[0][1]  
> > If … you had results like this,
> > 
> > y = [[10, 3], [30, 3], [20, 1], [40, 1]]

What is y[0][1]? Now plug that value into this conditional:

b == y[0][1]

What do you get?
