# Newbie question

**URL:** <https://www.ruby-forum.com/t/newbie-question/118985>\
**Category:** Ruby\
**Created:** [November 1, 2007, 10:29pm UTC](https://www.ruby-forum.com/t/newbie-question/118985 "2007-11-01T22:29:12Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dalesmithtx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/54ee81/32.png) [@dalesmithtx](https://www.ruby-forum.com/u/dalesmithtx)\
**Post date:** [November 1, 2007, 10:29pm UTC](https://www.ruby-forum.com/t/newbie-question/118985/1 "2007-11-01T22:29:12Z")

</div>

First thing’s first - if there’s a newbie forum where a question like  
this would be more appropriate, somebody please point it out to me.

I’m new to Ruby, and I’m trying to pick it up specifically for Watir  
testing. I’ve installed Ruby from the One Click Installer for Windows  
three times now, but I keep running into the same issue, so at this  
point I figure it must be operator error. The versions I’ve used so far  
are:

ruby186-25.exe  
ruby185-24.exe  
ruby185-22.exe

Here’s the issue I keep running into:

C:\rubysandbox\>irb  
irb(main):001:0\> 1…10.each { |x| puts x }  
NoMethodError: undefined method ‘each’ for 10:Fixnum  
from (irb):1  
irb(main):002:0\>

Why do I keep getting a NoMethodError when I try to use the each()  
method on a number?

---

<div class="post-metadata">

**Author:** ![dalesmithtx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/54ee81/32.png) [@dalesmithtx](https://www.ruby-forum.com/u/dalesmithtx)\
**Post date:** [November 1, 2007, 10:39pm UTC](https://www.ruby-forum.com/t/newbie-question/118985/2 "2007-11-01T22:39:23Z")

</div>

Dale Smith wrote:

> Here’s the issue I keep running into:
> 
> C:\rubysandbox\>irb  
> irb(main):001:0\> 1…10.each { |x| puts x }  
> NoMethodError: undefined method ‘each’ for 10:Fixnum  
> from (irb):1  
> irb(main):002:0\>
> 
> Why do I keep getting a NoMethodError when I try to use the each()  
> method on a number?

You aren’t _trying_ to use each on a number. A number looks like:

1  
13.4  
5

You are trying to use each on a Range, which looks like:

1…10  
a…z

Unfortunately, ruby thinks you are trying to use each on the number 10.  
To fix the problem, you need to put parentheses around the range:

(1…10).each

Personally I always type:

1…10.e

and then remember I need the parentheses, which means using each() on a  
range requires too much typing. So, I try to do this instead:

10.times do |i|  
…  
…  
end

---

<div class="post-metadata">

**Author:** ![dalesmithtx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/54ee81/32.png) [@dalesmithtx](https://www.ruby-forum.com/u/dalesmithtx)\
**Post date:** [November 1, 2007, 10:41pm UTC](https://www.ruby-forum.com/t/newbie-question/118985/3 "2007-11-01T22:41:34Z")

</div>

7stud – wrote:

> So, I try to do this instead:
> 
> 10.times do |i|  
> …  
> …  
> end

I should mention that this code:

10.times do |i|

is not equivalent to:

(1…10).each do |i|

because times() starts counting at 0 and ends at 9.

---

<div class="post-metadata">

**Author:** ![dalesmithtx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/54ee81/32.png) [@dalesmithtx](https://www.ruby-forum.com/u/dalesmithtx)\
**Post date:** [November 1, 2007, 10:43pm UTC](https://www.ruby-forum.com/t/newbie-question/118985/4 "2007-11-01T22:43:47Z")

</div>

Dale Smith wrote:

> irb(main):001:0\> 1…10.each { |x| puts x }  
> NoMethodError: undefined method ‘each’ for 10:Fixnum  
> from (irb):1  
> irb(main):002:0\>

The problem is that `..' has lower precedence than`.’.

(1…10).each { |x| puts x }

I often forget the ( ) , and have to back up and insert them.

---

<div class="post-metadata">

**Author:** ![dalesmithtx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/54ee81/32.png) [@dalesmithtx](https://www.ruby-forum.com/u/dalesmithtx)\
**Post date:** [November 1, 2007, 10:47pm UTC](https://www.ruby-forum.com/t/newbie-question/118985/5 "2007-11-01T22:47:18Z")

</div>

Dale Smith wrote:

> ruby185-24.exe
> 
> Why do I keep getting a NoMethodError when I try to use the each()  
> method on a number?

#each generally applies to collection of things, so you don’t use it on  
a single number. I think what you want is

(1…10).each {|x| puts x}

By surrounding the range 1…10 with parentheses you’re telling Ruby that  
you want the #each method for the range 1…10, not for the single number  
10.

---

<div class="post-metadata">

**Author:** ![dalesmithtx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/54ee81/32.png) [@dalesmithtx](https://www.ruby-forum.com/u/dalesmithtx)\
**Post date:** [November 1, 2007, 10:50pm UTC](https://www.ruby-forum.com/t/newbie-question/118985/6 "2007-11-01T22:50:14Z")

</div>

> ## I’m new to Ruby, and I’m trying to pick it up specifically for Watir Here’s the issue I keep running into:
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

Because numbers (Fixnums) do not implement the each method as numbers  
are  
not collections - ranges do, though. To call the method on the range  
instead  
of the last Fixnum that forms the range, enclose it in parantheses:

$ irb  
irb(main):001:0\> 1…10.each {|x| puts x}  
NoMethodError: undefined method `each’ for 10:Fixnum  
from (irb):1  
irb(main):002:0\> (1…10).each {|x| puts x}  
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
=\> 1…10

HTH,

Felix
