# Value returned from code block

**URL:** <https://www.ruby-forum.com/t/value-returned-from-code-block/80388>\
**Category:** Ruby\
**Created:** [December 25, 2006, 6:11pm UTC](https://www.ruby-forum.com/t/value-returned-from-code-block/80388 "2006-12-25T18:11:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![spitfire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a5b964/32.png) [@spitfire](https://www.ruby-forum.com/u/spitfire)\
**Post date:** [December 25, 2006, 6:11pm UTC](https://www.ruby-forum.com/t/value-returned-from-code-block/80388/1 "2006-12-25T18:11:57Z")

</div>

I’ve read that in Ruby the value returned from a code block is the  
value of the last expression executed. Is it so? If Yes, I don’t  
understand the value returned by the following code block!

```
def clistIter
	clist = [1, 2, 3]
	clist.each do |elem|
		yield(elem)
	end
end

clistIter { |e| print e }

## output in irb:
## 123 => [1, 2, 3]
##
## returned value:
## => [1, 2, 3]

```

Can someone explain how come an array is returned as result?

---

<div class="post-metadata">

**Author:** ![spitfire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a5b964/32.png) [@spitfire](https://www.ruby-forum.com/u/spitfire)\
**Post date:** [December 25, 2006, 6:26pm UTC](https://www.ruby-forum.com/t/value-returned-from-code-block/80388/2 "2006-12-25T18:26:00Z")

</div>

> I’ve read that in Ruby the value returned from a code block is the  
> value of the last expression executed. Is it so? If Yes, I don’t  
> understand the value returned by the following code block!  
> \<…\>  
> ## returned value:  
> ## =\> [1, 2, 3]
> 
> Can someone explain how come an array is returned as result?

In ruby 1.8 assignment always returns rvalue.

Regards,  
Rimantas

---

<div class="post-metadata">

**Author:** ![spitfire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a5b964/32.png) [@spitfire](https://www.ruby-forum.com/u/spitfire)\
**Post date:** [December 25, 2006, 6:44pm UTC](https://www.ruby-forum.com/t/value-returned-from-code-block/80388/3 "2006-12-25T18:44:02Z")

</div>

> > Can someone explain how come an array is returned as result?
> 
> In ruby 1.8 assignment always returns rvalue.

I should have read more carefully. What I said is true, but has  
nothing to do with  
your case, sorry for the noise.

Regards,  
Rimantas

---

<div class="post-metadata">

**Author:** ![spitfire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a5b964/32.png) [@spitfire](https://www.ruby-forum.com/u/spitfire)\
**Post date:** [December 25, 2006, 6:26pm UTC](https://www.ruby-forum.com/t/value-returned-from-code-block/80388/4 "2006-12-25T18:26:01Z")

</div>

On 25.12.2006 18:08, Spitfire wrote:

> ```
> clistIter { |e| print e }
> 
> ## output in irb:
> ## 123 => [1, 2, 3]
> ##
> ## returned value:
> ## => [1, 2, 3]
> 
> ```
> 
> Can someone explain how come an array is returned as result?

Because #each ignores all return values from the block (it is invoked  
once per element visited) and chooses to return self. This is how it  
could look like internally (it’s coded in C of course):

> > class Array  
> > def demo\_each  
> > for i in 0…length  
> > yield self[i]  
> > end  
> > self  
> > end  
> > end  
> > =\> nil
> 
> > [1,2,3].demo\_each {|e| p e; 666}  
> > 1  
> > 2  
> > 3  
> > =\> [1, 2, 3]

Kind regards

```
robert
```
