I just started looking at Ruby, and I really like what I see. I'm just at
the 'toying' stage at the moment, trying to learn the syntax, and adapt to a
new way of looking at code. I've got the following snippet that gives me an
error. I'm fairly certain that this will turn out to be a pretty dumb
question, but if I don't ask it, I'll never learn. Why does this:
....
i=0
namelist.each do |aword|
if aword > namelist[i+1]
...
end
i += 1
end
....
give me the following error?
sortlist.rb:19:in '>': undefined method '>' for false:FalseClass
(NoMethodError)
I just started looking at Ruby, and I really like what I see. I'm just at
the 'toying' stage at the moment, trying to learn the syntax, and adapt to
a new way of looking at code. I've got the following snippet that gives me
an error. I'm fairly certain that this will turn out to be a pretty dumb
question, but if I don't ask it, I'll never learn. Why does this:
...
i=0
namelist.each do |aword|
if aword > namelist[i+1]
...
end
i += 1
end
...
give me the following error?
sortlist.rb:19:in '>': undefined method '>' for false:FalseClass
(NoMethodError)
from sortlist.rb:19
from:sortlist.rb:17:in 'each'
from:sortlist.rb:17
Thanks for any assistance,
randy
I don't know what's in your namelist array, but it looks to me like somehow
it's gotten a 'false' value as one of the entries.
Also, check out the each_with_index method in Enumerable.
Thanks for responding. namelist is just a list of words that I was then
going to sort. I realize Ruby has a sort method for arrays that takes care
of this very easily, (took me about half a second to figure that out and get
it to work, but I was just trying to hand code it to get more familiar with
the language. Sure it's nothing I would ever use again, but I find the more
I keep working with a language the quicker I begin to grasp it and can begin
to do something useful with it. Not sure if the each_with_index method would
help me here, but it seems apparent that I need to really study Enumerable.
Thanks,
randy
"Tim Hunter" <cyclists@nc.rr.com> wrote in message
news:gGN_c.5619$ay.769217@twister.southeast.rr.com...
···
rbrooks wrote:
Hello all,
I just started looking at Ruby, and I really like what I see. I'm just at
the 'toying' stage at the moment, trying to learn the syntax, and adapt
to
a new way of looking at code. I've got the following snippet that gives
me
an error. I'm fairly certain that this will turn out to be a pretty dumb
question, but if I don't ask it, I'll never learn. Why does this:
...
i=0
namelist.each do |aword|
if aword > namelist[i+1]
...
end
i += 1
end
...
give me the following error?
sortlist.rb:19:in '>': undefined method '>' for false:FalseClass
(NoMethodError)
from sortlist.rb:19
from:sortlist.rb:17:in 'each'
from:sortlist.rb:17
Thanks for any assistance,
randy
I don't know what's in your namelist array, but it looks to me like
somehow
it's gotten a 'false' value as one of the entries.
Also, check out the each_with_index method in Enumerable.
Thanks for responding. namelist is just a list of words that I was then
going to sort. I realize Ruby has a sort method for arrays that takes care
of this very easily, (took me about half a second to figure that out and
get it to work, but I was just trying to hand code it to get more familiar
with the language. Sure it's nothing I would ever use again, but I find
the more I keep working with a language the quicker I begin to grasp it
and can begin to do something useful with it. Not sure if the
each_with_index method would help me here, but it seems apparent that I
need to really study Enumerable.
Ruby gives you the error message when you reach the last entry in namelist
and you try to reference the last entry + 1.
each_with_index would allow you to write the loop without maintaining a
separate counter variable.
sortlist.rb:19:in '>': undefined method '>' for false:FalseClass
(NoMethodError)
which sounds like your first guess was correct - a 'false' got into the
array list somehow (namelist[i+1] is on the rhs of the >). Also
namelist[i+1] would be nil, not false, when it overran the bounds.
martin
···
Tim Hunter <cyclists@nc.rr.com> wrote:
Ruby gives you the error message when you reach the last entry in namelist
and you try to reference the last entry + 1.