# N00b: code logic (probably a 1-min-question)

**URL:** <https://www.ruby-forum.com/t/n00b-code-logic-probably-a-1-min-question/167424>\
**Category:** Ruby\
**Created:** [May 7, 2009, 6:31pm UTC](https://www.ruby-forum.com/t/n00b-code-logic-probably-a-1-min-question/167424 "2009-05-07T18:31:27Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [May 7, 2009, 6:31pm UTC](https://www.ruby-forum.com/t/n00b-code-logic-probably-a-1-min-question/167424/1 "2009-05-07T18:31:27Z")

</div>

Hi there,

Why do I get ‘true’ when:

- I call: @agency.all\_agents\_done? and
- @gency has at least 3 Agents

# The Agency model is:

class Agency \< ActiveRecord::Base  
has\_many :agents

```
def all_agents_done? # (don't look for any real sense in this

```

method…)  
i=0  
self.agents do |agent| # (@gency has at least 3 Agents…)  
i=i+1 if (1 == 1)  
end

```
  if i == 0
    true
  else
    false
  end
end

```

end

Thanks for any hint!  
Tom

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [May 7, 2009, 8:50pm UTC](https://www.ruby-forum.com/t/n00b-code-logic-probably-a-1-min-question/167424/2 "2009-05-07T20:50:15Z")

</div>

On Thu, May 7, 2009 at 9:31 AM, Tom Ha [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ## Â class Agency \< ActiveRecord::Base Â Â Â Â true
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

In the question at the tops and the comment, you alternate between  
@agency and @gency – if this inconsistency exists in the method that  
has the instance variable(s) @agency and/or @gency that is calling  
this code, that could be part of the source of the problem.

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [May 7, 2009, 9:45pm UTC](https://www.ruby-forum.com/t/n00b-code-logic-probably-a-1-min-question/167424/3 "2009-05-07T21:45:24Z")

</div>

Sorry, that’s just a typo in my question, so the problem is still there  
and lies elsewhere, but I just don’t get it - everything seems to be  
correct…

Am I stupid?

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [May 7, 2009, 9:50pm UTC](https://www.ruby-forum.com/t/n00b-code-logic-probably-a-1-min-question/167424/4 "2009-05-07T21:50:48Z")

</div>

Christopher D. wrote:

> On Thu, May 7, 2009 at 9:31 AM, Tom Ha [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > ## Â class Agency \< ActiveRecord::Base Â Â Â Â true
> > 
> > Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> In the question at the tops and the comment, you alternate between  
> @agency and @gency – if this inconsistency exists in the method that  
> has the instance variable(s) @agency and/or @gency that is calling  
> this code, that could be part of the source of the problem.

i’m sorry, i know the comment says not too, but I have to look for sense  
in the method, and if i do, it appears that you are essentially asking  
if there are any agents at all?? if that is the case, why are you simply  
not writing:

def all\_agents\_done?  
self.agents.size == 0  
end

and why do you have this, “if (1 == 1)” condition? this is like asking  
“if true”

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [May 7, 2009, 10:18pm UTC](https://www.ruby-forum.com/t/n00b-code-logic-probably-a-1-min-question/167424/5 "2009-05-07T22:18:42Z")

</div>

Holy sh… I got it:

“self.agents do |agent| …” was missing the “.each”:

-\> “self.agents.each do |agent| …”

Guess I can answer that “am I stupid” question now… ☹

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [May 7, 2009, 10:06pm UTC](https://www.ruby-forum.com/t/n00b-code-logic-probably-a-1-min-question/167424/6 "2009-05-07T22:06:13Z")

</div>

Thanks for replying - well, there’s not much “sense” in the example  
because I took stuff out, to make it shorter.

Basically, the problem in the original example above is apparently, that  
@agency.agents has a ‘length’/‘size’ of 4, but the counter i does NOT  
get incremented in the loop "self.agents do |agent| … " (‘i’ stays  
0…).

But why? What am I missing? I just don’t get it…

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [May 7, 2009, 11:56pm UTC](https://www.ruby-forum.com/t/n00b-code-logic-probably-a-1-min-question/167424/7 "2009-05-07T23:56:31Z")

</div>

Ouch 🙂
