[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

n00b: code logic (probably a 1-min-question

Tom Ha

5/7/2009 4:32:00 PM

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
--
Posted via http://www.ruby-....

6 Answers

Christopher Dicely

5/7/2009 6:49:00 PM

0

On Thu, May 7, 2009 at 9:31 AM, Tom Ha <tom999@gmx.net> wrote:
> 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:
> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
>
> =C2=A0class Agency < ActiveRecord::Base
> =C2=A0 =C2=A0has_many :agents
>
> =C2=A0 =C2=A0def all_agents_done? =C2=A0 =C2=A0 # (don't look for any rea=
l sense in this
> method...)
> =C2=A0 =C2=A0 =C2=A0i=3D0
> =C2=A0 =C2=A0 =C2=A0self.agents do |agent| # (@gency has at least 3 Agent=
s...)
> =C2=A0 =C2=A0 =C2=A0 =C2=A0i=3Di+1 if (1 =3D=3D 1)
> =C2=A0 =C2=A0 =C2=A0end
>
> =C2=A0 =C2=A0 =C2=A0if i =3D=3D 0
> =C2=A0 =C2=A0 =C2=A0 =C2=A0true
> =C2=A0 =C2=A0 =C2=A0else
> =C2=A0 =C2=A0 =C2=A0 =C2=A0false
> =C2=A0 =C2=A0 =C2=A0end
> =C2=A0 =C2=A0end
> =C2=A0end
>
>
>
> Thanks for any hint!
> Tom
> --
> Posted via http://www.ruby-....


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.

Tom Ha

5/7/2009 7:46:00 PM

0

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?
--
Posted via http://www.ruby-....

Gabriel Saravia

5/7/2009 7:51:00 PM

0

Christopher Dicely wrote:
> On Thu, May 7, 2009 at 9:31 AM, Tom Ha <tom999@gmx.net> wrote:
>>  class Agency < ActiveRecord::Base
>>        true
>> --
>> Posted via http://www.ruby-....
>
>
> 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"
--
Posted via http://www.ruby-....

Tom Ha

5/7/2009 8:06:00 PM

0

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...
--
Posted via http://www.ruby-....

Tom Ha

5/7/2009 8:19:00 PM

0

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... :-(
--
Posted via http://www.ruby-....

list. rb

5/7/2009 9:56:00 PM

0

Ouch :-)





On May 7, 2009, at 4:18 PM, Tom Ha <tom999@gmx.net> wrote:

> 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... :-(
> --
> Posted via http://www.ruby-....
>