# Erratic result

**URL:** <https://www.ruby-forum.com/t/erratic-result/131131>\
**Category:** Ruby\
**Created:** [March 6, 2008, 9:50am UTC](https://www.ruby-forum.com/t/erratic-result/131131 "2008-03-06T09:50:03Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![coolgeng\_coolgeng](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eada6e/32.png) [@coolgeng\_coolgeng](https://www.ruby-forum.com/u/coolgeng_coolgeng)\
**Post date:** [March 6, 2008, 9:50am UTC](https://www.ruby-forum.com/t/erratic-result/131131/1 "2008-03-06T09:50:03Z")

</div>

Hi  
I do not know how to explain the different result between the two  
snippets:  
First snippet:  
def dial(digit)  
puts digit end  
digits = 0…9  
puts digits.each {|digit| dial(digit) }

```
  output:

```

0  
1  
2  
3  
4  
5  
6  
7  
8  
9  
0…9

Second snippet:  
(0…9).each do |digit|  
puts digit  
end

output:  
0  
1  
2  
3  
4  
5  
6  
7  
8  
9

Why did the first snippet will generate the word like “0…9” at last  
line?

---

<div class="post-metadata">

**Author:** ![coolgeng\_coolgeng](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eada6e/32.png) [@coolgeng\_coolgeng](https://www.ruby-forum.com/u/coolgeng_coolgeng)\
**Post date:** [March 6, 2008, 9:57am UTC](https://www.ruby-forum.com/t/erratic-result/131131/2 "2008-03-06T09:57:42Z")

</div>

coolgeng coolgeng wrote:

> 1  
> 2  
> 3  
> 4  
> 5  
> 6  
> 7  
> 8  
> 9  
> 0…9

You probably want this:

def dial(digit)  
puts digit  
end  
digits = 0…9  
digits.each {|digit| puts dial(digit) }

Best regards,

Jari W.

---

<div class="post-metadata">

**Author:** ![coolgeng\_coolgeng](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eada6e/32.png) [@coolgeng\_coolgeng](https://www.ruby-forum.com/u/coolgeng_coolgeng)\
**Post date:** [March 6, 2008, 10:03am UTC](https://www.ruby-forum.com/t/erratic-result/131131/3 "2008-03-06T10:03:01Z")

</div>

On Thu, Mar 6, 2008 at 9:49 AM, coolgeng coolgeng  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi  
> I do not know how to explain the different result between the two  
> snippets:  
> First snippet:  
> def dial(digit)  
> puts digit end  
> digits = 0…9  
> puts digits.each {|digit| dial(digit) }

> Why did the first snippet will generate the word like “0…9” at last line?

Because you are calling puts with the result of the each method, which  
in this is the range.  
Take into account that .each will run the block yielding each element,  
and then will return something.  
You can remove the last puts:

def dial(digit)  
puts digit  
end  
digits = 0…9  
digits.each {|digit| dial(digit) }

Jesus.

---

<div class="post-metadata">

**Author:** ![coolgeng\_coolgeng](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eada6e/32.png) [@coolgeng\_coolgeng](https://www.ruby-forum.com/u/coolgeng_coolgeng)\
**Post date:** [March 6, 2008, 10:08am UTC](https://www.ruby-forum.com/t/erratic-result/131131/4 "2008-03-06T10:08:56Z")

</div>

Yeah, it does work. But I really want to know what does it happen?  
such as “puts digits.each {|digit| dial(digit) }”  
or “digits.each {|digit| dial(digit) }”

the former will print the digits value at the last line,but the latter  
will  
not

On Thu, Mar 6, 2008 at 5:01 PM, JesÃºs Gabriel y GalÃ¡n \<

---

<div class="post-metadata">

**Author:** ![coolgeng\_coolgeng](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eada6e/32.png) [@coolgeng\_coolgeng](https://www.ruby-forum.com/u/coolgeng_coolgeng)\
**Post date:** [March 7, 2008, 4:40pm UTC](https://www.ruby-forum.com/t/erratic-result/131131/5 "2008-03-07T16:40:42Z")

</div>

On Thu, 06 Mar 2008 03:49:13 -0500, coolgeng coolgeng wrote:

> [Note: parts of this message were removed to make it a legal post.]
> 
> Hi  
> I do not know how to explain the different result between the two  
> snippets:

> def dial(digit)  
> puts digit end  
> digits = 0…9  
> puts digits.each {|digit| dial(digit) }

It’s that extra puts before digits.each  
By convention, #each returns self, so after using dial to call puts for  
each individual digit, you also call puts on the return value of  
digits.each, which is digits itself. puts calls #to\_s on digits, which  
for a Range gives you the source code representation of the range.

–Ken

---

<div class="post-metadata">

**Author:** ![coolgeng\_coolgeng](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eada6e/32.png) [@coolgeng\_coolgeng](https://www.ruby-forum.com/u/coolgeng_coolgeng)\
**Post date:** [March 6, 2008, 10:42am UTC](https://www.ruby-forum.com/t/erratic-result/131131/6 "2008-03-06T10:42:39Z")

</div>

On Thu, Mar 6, 2008 at 10:08 AM, coolgeng coolgeng  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Yeah, it does work. But I really want to know what does it happen?  
> such as “puts digits.each {|digit| dial(digit) }”

This:  
puts digits.each {|digit| dial(digit) }

is equivalent to this:

a = digits.each {|digit| dial(digit) }  
puts a

The first line is the loop and prints each digit.  
The second line prints the result of the each method.

> or “digits.each {|digit| dial(digit) }”

In this case you don’t do anything with the return value of the each  
method.

> the former will print the digits value at the last line,but the latter will  
> not

Jesus.
