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.
newbie (Guest)
on 2006-05-24 22: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
Jeremy T. (Guest)
on 2006-05-24 22: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 T.
removed_email_address@domain.invalid


"The proof is the proof that the proof has been proven and that's the
proof!" - Jean Chrétien
Jake McArthur (Guest)
on 2006-05-24 22: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
Logan C. (Guest)
on 2006-05-24 22:29
(Received via mailing list)
On May 24, 2006, at 2:19 PM, Jeremy T. wrote:

# "declare" firstif in an enclosing scope
firstif = nil
newbie (Guest)
on 2006-05-24 22: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 T.
> removed_email_address@domain.invalid
>
>
> "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
Tom R. (Guest)
on 2006-05-24 22:33
(Received via mailing list)
newbie <removed_email_address@domain.invalid> 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
Pit C. (Guest)
on 2006-05-25 00: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.