Ifs and scope


#1

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


#2

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


#3

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

#4

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 :wink:

that worked - thank you


#5

On May 24, 2006, at 2:19 PM, Jeremy T. wrote:

“declare” firstif in an enclosing scope

firstif = nil


#6

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


#7

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