# Ifs and scope

**URL:** <https://www.ruby-forum.com/t/ifs-and-scope/95218>\
**Category:** Ruby\
**Created:** [May 24, 2006, 8:13pm UTC](https://www.ruby-forum.com/t/ifs-and-scope/95218 "2006-05-24T20:13:45Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![newbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dc4da7/32.png) [@newbie](https://www.ruby-forum.com/u/newbie)\
**Post date:** [May 24, 2006, 8:13pm UTC](https://www.ruby-forum.com/t/ifs-and-scope/95218/1 "2006-05-24T20:13:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![newbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dc4da7/32.png) [@newbie](https://www.ruby-forum.com/u/newbie)\
**Post date:** [May 24, 2006, 8:22pm UTC](https://www.ruby-forum.com/t/ifs-and-scope/95218/2 "2006-05-24T20:22:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![newbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dc4da7/32.png) [@newbie](https://www.ruby-forum.com/u/newbie)\
**Post date:** [May 24, 2006, 8:26pm UTC](https://www.ruby-forum.com/t/ifs-and-scope/95218/3 "2006-05-24T20:26:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![newbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dc4da7/32.png) [@newbie](https://www.ruby-forum.com/u/newbie)\
**Post date:** [May 24, 2006, 8:30pm UTC](https://www.ruby-forum.com/t/ifs-and-scope/95218/4 "2006-05-24T20:30:13Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![newbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dc4da7/32.png) [@newbie](https://www.ruby-forum.com/u/newbie)\
**Post date:** [May 24, 2006, 8:29pm UTC](https://www.ruby-forum.com/t/ifs-and-scope/95218/5 "2006-05-24T20:29:48Z")

</div>

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

# “declare” firstif in an enclosing scope

firstif = nil

---

<div class="post-metadata">

**Author:** ![newbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dc4da7/32.png) [@newbie](https://www.ruby-forum.com/u/newbie)\
**Post date:** [May 24, 2006, 10:02pm UTC](https://www.ruby-forum.com/t/ifs-and-scope/95218/6 "2006-05-24T22:02:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![newbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dc4da7/32.png) [@newbie](https://www.ruby-forum.com/u/newbie)\
**Post date:** [May 24, 2006, 8:33pm UTC](https://www.ruby-forum.com/t/ifs-and-scope/95218/7 "2006-05-24T20:33:06Z")

</div>

newbie [removed\_email\_address@domain.invalid](mailto: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
