views:

70

answers:

4

I have faced the plain simple situation, that i can't figure out.

I have to deside, whether to show the "more" link, depending of the articles count

The code is plain simple

@no_more = offset + length >= Article.count

@no_more variable equals to nil sometimes. I see in debugger, that offset=0, length=10 and Article.count=12

But the expression above gives nil

Even this, does't helps:

@no_more = false if @no_more.nil?

@no_more will still be nil

What is a reason of such a magic can be ?

A: 

You need parenteze

@no_more = ((offset + length) >= Article.count)

The precedence can't works, so ruby interprete in dab way your code

shingara
I tried, what you suggested, but i get the same: @no_more equals to nil.
AntonAL
+1  A: 

what about not-not assignment?

ruby-1.8.7-p299 > @no_more
 => nil 
ruby-1.8.7-p299 > !!@no_more
 => false 
ruby-1.8.7-p299 > @no_more = true
 => true 
ruby-1.8.7-p299 > !!@no_more
 => true
Jed Schneider
A: 

Make sure that you are not resetting the @no_more variable somewhere else in your controller or view, especially if you are doing a redirect.

Tim
No, i have traced the program in debugger. Current position of the program pointer points to @no_more variable, mentioned first time (first code fragment in my question), and in the next step it still equals to nil, after the condition (second code fragment)
AntonAL
A: 

Try printing @no_more.class and check if it really is a NilClass and not a FalseClass.

I say this because I had the same problem the other day. This was with the debugger in netbeans (and jruby for that matter), but the debugger did not seem to understand the FalseClass.

for example this code:

p = false
puts p.class

For me this ofcourse printed FalseClass, but the debugger insisted p was a NilClass. I confirmed with "kind_of?" that it really was a FalseClass.

You could try checking the same things.

@no_more.kind_of?(FalseClass)

Just thought I'd mention this in case it's the same problem as I had. It kept me busy half the night trying to figure out wtf was going on.

Rutger