# The last evaluated expression for this method?

**URL:** <https://www.ruby-forum.com/t/the-last-evaluated-expression-for-this-method/76512>\
**Category:** Ruby\
**Created:** [November 19, 2006, 10:16pm UTC](https://www.ruby-forum.com/t/the-last-evaluated-expression-for-this-method/76512 "2006-11-19T22:16:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![feralhound](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/6de8d8/32.png) [@feralhound](https://www.ruby-forum.com/u/feralhound)\
**Post date:** [November 19, 2006, 10:16pm UTC](https://www.ruby-forum.com/t/the-last-evaluated-expression-for-this-method/76512/1 "2006-11-19T22:16:15Z")

</div>

I wrote a simple version of the collect method for the Array class  
called my\_collect. Here is what I wrote:

class Array  
def my\_collect  
result = []  
self.each {|element| result.push(yield(element))}  
result  
end  
end

x = [1,2,3].my\_collect {|element| element + 1}  
print x

It prints out 234 correctly. However, I was puzzled by what x contained  
if I removed the return value ‘result’, that is:

class Array  
def my\_collect  
result = []  
self.each {|element| result.push(yield(element))}  
#Removed return value  
end  
end

x = [1,2,3].my\_collect {|element| element + 1}  
print x

It prints out 123. Why? The last evaluated expression would probably be  
result.push(yield(3)), and since push returns the resulting array,  
wouldn’t it be 234 in this case as well?

Thanks,  
FeralHound

---

<div class="post-metadata">

**Author:** ![feralhound](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/6de8d8/32.png) [@feralhound](https://www.ruby-forum.com/u/feralhound)\
**Post date:** [November 20, 2006, 2:45pm UTC](https://www.ruby-forum.com/t/the-last-evaluated-expression-for-this-method/76512/2 "2006-11-20T14:45:43Z")

</div>

Avi S g [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> ```
> #Removed return value
> 
> ```
> 
> end  
> end
> 
> x = [1,2,3].my\_collect {|element| element + 1}  
> print x
> 
> It prints out 123. Why? The last evaluated expression would probably  
> be result.push(yield(3)),

No. The last evaluated block of self.each would be  
result.push(yield(3)), but each does not return the last evaluated  
block, but rather the original array.

> and since push returns the resulting array, wouldn’t it be 234 in  
> this case as well?

No.

---

<div class="post-metadata">

**Author:** ![feralhound](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/6de8d8/32.png) [@feralhound](https://www.ruby-forum.com/u/feralhound)\
**Post date:** [November 20, 2006, 2:45pm UTC](https://www.ruby-forum.com/t/the-last-evaluated-expression-for-this-method/76512/3 "2006-11-20T14:45:43Z")

</div>

Alle 22:16, domenica 19 novembre 2006, Avi S g ha scritto:

> It prints out 123. Why? The last evaluated expression would probably be  
> result.push(yield(3)), and since push returns the resulting array,  
> wouldn’t it be 234 in this case as well?

The last evaluated expression is not yield, but [1,2,3].each, which  
returns  
self, i.e [1,2,3]

---

<div class="post-metadata">

**Author:** ![feralhound](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/6de8d8/32.png) [@feralhound](https://www.ruby-forum.com/u/feralhound)\
**Post date:** [November 20, 2006, 2:45pm UTC](https://www.ruby-forum.com/t/the-last-evaluated-expression-for-this-method/76512/4 "2006-11-20T14:45:44Z")

</div>

On Mon, Nov 20, 2006 at 06:16:17AM +0900, Avi S g wrote:

> ```
> #Removed return value
> 
> ```
> 
> end  
> end
> 
> x = [1,2,3].my\_collect {|element| element + 1}  
> print x
> 
> It prints out 123. Why? The last evaluated expression would probably be  
> result.push(yield(3)), and since push returns the resulting array,  
> wouldn’t it be 234 in this case as well?

x.each { } by convention returns x. In other words, it’s as if  
Array#each was defined like so:

class Array  
def my\_each  
i = 0  
while i \< self.length  
yield( self[i] )  
i += 1  
end  
self # last evaluated expression for each is here.  
end  
end
