# Why this works not right

**URL:** <https://www.ruby-forum.com/t/why-this-works-not-right/180645>\
**Category:** Ruby\
**Created:** [December 25, 2009, 3:12pm UTC](https://www.ruby-forum.com/t/why-this-works-not-right/180645 "2009-12-25T15:12:06Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eva](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/dbc845/32.png) [@Eva](https://www.ruby-forum.com/u/Eva)\
**Post date:** [December 25, 2009, 3:12pm UTC](https://www.ruby-forum.com/t/why-this-works-not-right/180645/1 "2009-12-25T15:12:06Z")

</div>

Hi,

Please take a look at below:

def mytest  
return [1…10]  
end

x = mytest  
x.each do |c| puts c end

this works not as I expected.  
I want the output of:  
1  
2  
3  
4  
5  
6  
7  
8  
9  
10

But if I change return [1…10] to return 1…10 it will work.  
So what’s the difference between 1…10 and [1…10]?  
THanks.

---

<div class="post-metadata">

**Author:** ![Eva](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/dbc845/32.png) [@Eva](https://www.ruby-forum.com/u/Eva)\
**Post date:** [December 25, 2009, 3:41pm UTC](https://www.ruby-forum.com/t/why-this-works-not-right/180645/2 "2009-12-25T15:41:14Z")

</div>

Eva wrote:

> Hi,
> 
> Please take a look at below:
> 
> def mytest  
> return [1…10]  
> end
> 
> x = mytest  
> x.each do |c| puts c end
> 
> this works not as I expected.  
> I want the output of:  
> 1  
> 2  
> 3  
> 4  
> 5  
> 6  
> 7  
> 8  
> 9  
> 10
> 
> But if I change return [1…10] to return 1…10 it will work.  
> So what’s the difference between 1…10 and [1…10]?

1…10 is a Range. [1…10] is an Array with one element (that happens to  
be a Range, but that’s irrelevant). Range#each returns each element in  
the range, so you get 1, 2, 3, etc. Array#each likewise returns each  
element in the array – but your array only has one element, the Range  
object itself!

If you want an Array with the elements 1 to 10, you need something like  
(1…10).to\_a .

> THanks.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![Eva](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/dbc845/32.png) [@Eva](https://www.ruby-forum.com/u/Eva)\
**Post date:** [December 25, 2009, 4:07pm UTC](https://www.ruby-forum.com/t/why-this-works-not-right/180645/3 "2009-12-25T16:07:19Z")

</div>

1…10 is a range, while [1…10] is an array with one element, a range  
from  
1…10. You can think of 1…10 as (1…10) and [1…10] as [(1…10)]

I am 95% sure that this is correct, someone please correct me if I’m  
wrong.  
irb seems to support this:

> def one\_to\_ten  
> 1…10  
> end  
> =\> nil  
> one\_to\_ten.class  
> =\> Range  
> [1…10].class  
> =\> Array

---

<div class="post-metadata">

**Author:** ![Eva](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/dbc845/32.png) [@Eva](https://www.ruby-forum.com/u/Eva)\
**Post date:** [December 25, 2009, 5:53pm UTC](https://www.ruby-forum.com/t/why-this-works-not-right/180645/4 "2009-12-25T17:53:04Z")

</div>

On Thu, 2009-12-24 at 11:47 +0900, Steve K. wrote:

> > one\_to\_ten.class  
> > =\> Range  
> > [1…10].class  
> > =\> Array

Thanks for the reply. I’m newbie to Ruby,so have another question, I  
want to make a function who returns the result which can be used as:

mytest do |a,b|c| do\_something end

How to write this mytest?

Regards,  
Eva

---

<div class="post-metadata">

**Author:** ![Eva](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/dbc845/32.png) [@Eva](https://www.ruby-forum.com/u/Eva)\
**Post date:** [December 25, 2009, 6:44pm UTC](https://www.ruby-forum.com/t/why-this-works-not-right/180645/5 "2009-12-25T18:44:03Z")

</div>

Eva wrote:

> > > 1…10  
> > > =\> Array  
> > > Regards,  
> > > Eva

The do…end is actually creating a block, which is passed into your  
method. There are two ways of having functions accept blocks:

#Explicitly  
def mytest &block  
block.call 1, 2, 3  
end

#Implicitly  
def mytest  
yield 1, 2, 3  
end

Either way can be called like

mytest do |a,b,c|  
do\_something\_with a, b, c  
end

You will probably want to read up on methods and blocks and how the two  
can be used together.

-Justin

---

<div class="post-metadata">

**Author:** ![Eva](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/dbc845/32.png) [@Eva](https://www.ruby-forum.com/u/Eva)\
**Post date:** [December 25, 2009, 6:54pm UTC](https://www.ruby-forum.com/t/why-this-works-not-right/180645/6 "2009-12-25T18:54:01Z")

</div>

Thanks all!  
Merry Christmas Ruby world.
