[prev in list] [next in list] [prev in thread] [next in thread]
List: ruby-talk
Subject: Re: Newbie question
From: 7stud -- <bbxx789_05ss () yahoo ! com>
Date: 2007-11-01 21:40:15
Message-ID: d038b364b267119a2977a2a67c10339b () ruby-forum ! com
[Download RAW message or body]
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
--
Posted via http://www.ruby-forum.com/.
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic