Forum: Ruby ifs and scope

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
F06f8fad11810690c6e743c0fffc4017?d=identicon&s=25 newbie (Guest)
on 2006-05-24 20:13
i am sorry if this is really newb, but i am new to programming and ruby
in general

in my previous thread "disappearing hash values" it was pointed out to
me that a new if is a new scope

how do i update a second if with the results from a first

ie

if (something)
    #do a bunch of stuff
    firstif = TRUE
end

if (somethingelse && firstif)
   # the above always fails since the second if doesn't see firstif)
   #do other stuff that is dependent on the first bunch of stuff
   firstif = FALSE
end


thanks
96950c5dec0af04a5c12dae973e96cc7?d=identicon&s=25 Jeremy Tregunna (Guest)
on 2006-05-24 20:22
(Received via mailing list)
On 24-May-06, at 2:13 PM, newbie wrote:

>
> if (something)
>     #do a bunch of stuff
>     firstif = TRUE
> end
>
> if (somethingelse && firstif)
>    # the above always fails since the second if doesn't see firstif)
>    #do other stuff that is dependent on the first bunch of stuff
>    firstif = FALSE
> end

Set your firstIf variable to a sentinel before running either if.
I.e., set it to false, then only if your first if statement passes,
will the second one be run.

--
Jeremy Tregunna
jtregunna@blurgle.ca


"The proof is the proof that the proof has been proven and that's the
proof!" - Jean Chrétien
49ab3ce5a4922b4747d1d6f330784629?d=identicon&s=25 Jake McArthur (Guest)
on 2006-05-24 20:26
(Received via mailing list)
On May 24, 2006, at 1:13 PM, newbie wrote:

> if (something)
>     #do a bunch of stuff
>     firstif = TRUE
> end
>
> if (somethingelse && firstif)
>    # the above always fails since the second if doesn't see firstif)
>    #do other stuff that is dependent on the first bunch of stuff
>    firstif = FALSE
> end

Two ways to do this. First way:

if (something)
     #do a bunch of stuff
     if (somethingelse)
         #do more stuff
     end
end

If you can't just nest them for one reason or another, here is
another way:

firstif
if (something)
     #do a bunch of stuff
     firstif = true
end

if (somethingelse && firstif)
    # the above always fails since the second if doesn't see firstif)
    #do other stuff that is dependent on the first bunch of stuff
    firstif = false
end

Notice that mentioning the variable outside of the scope makes it
available to both ifs.

- Jake McArthur
E34b5cae57e0dd170114dba444e37852?d=identicon&s=25 Logan Capaldo (Guest)
on 2006-05-24 20:29
(Received via mailing list)
On May 24, 2006, at 2:19 PM, Jeremy Tregunna wrote:

# "declare" firstif in an enclosing scope
firstif = nil
F06f8fad11810690c6e743c0fffc4017?d=identicon&s=25 newbie (Guest)
on 2006-05-24 20:30
> Set your firstIf variable to a sentinel before running either if.
> I.e., set it to false, then only if your first if statement passes,
> will the second one be run.
>
> --
> Jeremy Tregunna
> jtregunna@blurgle.ca
>
>
> "The proof is the proof that the proof has been proven and that's the
> proof!" - Jean Chrétien

the proof is in the pudding  ;)

that worked - thank you
188ff29b8682ec3a04e88d85a427300d?d=identicon&s=25 Tom Rauchenwald (Guest)
on 2006-05-24 20:33
(Received via mailing list)
newbie <a@a.org> writes:

>    # the above always fails since the second if doesn't see firstif)
>    #do other stuff that is dependent on the first bunch of stuff
>    firstif = FALSE
> end

Declare firstif outside of the scope of the if, for instance:
firstif=nil
if (something)
    #do a bunch of stuff
    firstif = true
end

or, in this case
firstif=if(something)
    #do stuff
    true
end
would also be possible.
>
> thanks

Tom
93d566cc26b230c553c197c4cd8ac6e4?d=identicon&s=25 Pit Capitain (Guest)
on 2006-05-24 22:02
(Received via mailing list)
newbie schrieb:
> in my previous thread "disappearing hash values" it was pointed out to
> me that a new if is a new scope

I don't think this is true. In Ruby 1.8.4:

   if 2 > 1
     v = true
   end

   p v  # => true

Regards,
Pit
This topic is locked and can not be replied to.