# If statement and if modifier not equivalent?

**URL:** <https://www.ruby-forum.com/t/if-statement-and-if-modifier-not-equivalent/70424>\
**Category:** Ruby\
**Created:** [February 19, 2006, 12:09pm UTC](https://www.ruby-forum.com/t/if-statement-and-if-modifier-not-equivalent/70424 "2006-02-19T12:09:01Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![elucify](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f17d59/32.png) [@elucify](https://www.ruby-forum.com/u/elucify)\
**Post date:** [February 19, 2006, 12:09pm UTC](https://www.ruby-forum.com/t/if-statement-and-if-modifier-not-equivalent/70424/1 "2006-02-19T12:09:01Z")

</div>

I was under the impression that the if modifier and the  
if statememt were equivalent, but for the following code  
calling A.test raises an exception, while calling A.test2  
succeeds. This is with Ruby 1.8.4.

class A  
def A.test  
a if a=1  
end

def A.test2  
if a=1  
a  
end  
end  
end

---

<div class="post-metadata">

**Author:** ![elucify](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f17d59/32.png) [@elucify](https://www.ruby-forum.com/u/elucify)\
**Post date:** [February 19, 2006, 12:24pm UTC](https://www.ruby-forum.com/t/if-statement-and-if-modifier-not-equivalent/70424/2 "2006-02-19T12:24:06Z")

</div>

On Sunday 19 February 2006 12:08, Mark J. wrote:

> I was under the impression that the if modifier and the  
> if statememt were equivalent, but for the following code  
> calling A.test raises an exception, while calling A.test2  
> succeeds. This is with Ruby 1.8.4.  
> That’s because of the way Ruby decides wether a local variable exists or  
> not. Basically, a local variable is defined the first time it is  
> assigned  
> to, so in the first case  
> def A.test  
> a if a=1  
> end  
> a=1, which defines the local, is seen _after_ the ‘a’ statement. So, ‘a’  
> does not exist and boom

In the second case,

> def A.test2  
> if a=1  
> a  
> end  
> end  
> a=1 is seen before the ‘a’ statement, so it works

Regards,  
Sylvain

---

<div class="post-metadata">

**Author:** ![elucify](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f17d59/32.png) [@elucify](https://www.ruby-forum.com/u/elucify)\
**Post date:** [February 19, 2006, 1:00pm UTC](https://www.ruby-forum.com/t/if-statement-and-if-modifier-not-equivalent/70424/3 "2006-02-19T13:00:25Z")

</div>

Sylvain J. wrote:

> That’s because of the way Ruby decides wether a local variable exists or  
> not. Basically, a local variable is defined the first time it is assigned  
> to, so in the first case
> 
> > def A.test  
> > a if a=1  
> > end
> 
> a=1, which defines the local, is seen _after_ the ‘a’ statement. So, ‘a’  
> does not exist and boom

OK, thanks Sylvain. But I would have thought that any examination of  
the guarded statement would be delayed until after the condition had  
been evaluated. Is a parse like this possible?

---

<div class="post-metadata">

**Author:** ![elucify](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f17d59/32.png) [@elucify](https://www.ruby-forum.com/u/elucify)\
**Post date:** [February 19, 2006, 1:39pm UTC](https://www.ruby-forum.com/t/if-statement-and-if-modifier-not-equivalent/70424/4 "2006-02-19T13:39:54Z")

</div>

DÅ?a NedeÄ¾a 19 FebruÃ¡r 2006 12:58 Mark J. napÃ­sal:

> OK, thanks Sylvain. But I would have thought that any examination of  
> the guarded statement would be delayed until after the condition had  
> been evaluated. Is a parse like this possible?

Personally, I’d be surprised if the parser paid any attention to much of  
the  
semantics of the parsed text at all - that’s what the interpreter is  
supposed  
to do. The variable declaration checking at parse-time seems to be the  
exception here.

Syntax ge… gurus, feel free to elaborate.

David V.

---

<div class="post-metadata">

**Author:** ![elucify](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f17d59/32.png) [@elucify](https://www.ruby-forum.com/u/elucify)\
**Post date:** [February 19, 2006, 1:58pm UTC](https://www.ruby-forum.com/t/if-statement-and-if-modifier-not-equivalent/70424/5 "2006-02-19T13:58:05Z")

</div>

Hi,

In message “Re: If statement and if modifier not equivalent?”  
on Sun, 19 Feb 2006 21:39:04 +0900, David V.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

|DÅ?a NedeÄ¾a 19 FebruÃ¡r 2006 12:58 Mark J. napÃ­sal:  
|\> OK, thanks Sylvain. But I would have thought that any examination of  
|\> the guarded statement would be delayed until after the condition had  
|\> been evaluated. Is a parse like this possible?  
|  
|Personally, I’d be surprised if the parser paid any attention to much of the  
|semantics of the parsed text at all - that’s what the interpreter is supposed  
|to do. The variable declaration checking at parse-time seems to be the  
|exception here.  
|  
|Syntax ge… gurus, feel free to elaborate.

Agreed with you. But there’s a vague possibility that this issue will  
be solved in Ruby 2.0 in different manner.

```
						matz.

```

---

<div class="post-metadata">

**Author:** ![elucify](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f17d59/32.png) [@elucify](https://www.ruby-forum.com/u/elucify)\
**Post date:** [February 20, 2006, 3:26am UTC](https://www.ruby-forum.com/t/if-statement-and-if-modifier-not-equivalent/70424/6 "2006-02-20T03:26:22Z")

</div>

On 2/19/06, Mark J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> def A.test2  
> if a=1  
> a  
> end  
> end  
> end

Do you really want ‘if a = 1’ there, and not ‘if a == 1’?

---

<div class="post-metadata">

**Author:** ![elucify](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f17d59/32.png) [@elucify](https://www.ruby-forum.com/u/elucify)\
**Post date:** [February 20, 2006, 2:55pm UTC](https://www.ruby-forum.com/t/if-statement-and-if-modifier-not-equivalent/70424/7 "2006-02-20T14:55:57Z")

</div>

Which makes the assignment a side-effect of the conditional, and  
depending on the context can be an excellent example of bad  
side-effects, particularly from a software-engineering point of view,  
where the code has to be maintained over any sort of time, by a team.

I think of it as the imperative corollary to duck-typing: if it looks  
like a duck and sounds like a duck, it should _be_ a duck.

There’s a great public example of where this technique can break down  
from an attempted hack on the linux kernal a couple of years ago. It’s  
worth noting that it was only caught because of 1) very tight, very  
paranoid peer review, and 2) the secure nature of the project. It  
would have gone through unnoticed virtually anywhere else. In my past  
life doing telecoms programming, I can remember this exact pattern  
being responsible for bringing down a good portion of the US East  
Coast’s 1-800 (and 900, etc) phone numbers.

To each their own, and if it’s code for your own use it’s unlikely to  
matter, but assignment-as-conditional really, really smells bad to me.

> **[An Interview With Linus Torvalds: Linux and Git - Part 1 30 Years Of Linux](https://www.tag1consulting.com/blog/interview-linus-torvalds-linux-and-git)**
>
> Thirty years ago, Linus Torvalds was a 21 year old student at the University of Helsinki when he first released the Linux Kernel. His announcement started, “I’m doing a (free) operating system (just a hobby, won't be big and professional…)”. Three...

> - 
> 
> ```
> if ((options == ( __WCLONE|__ WALL)) && (current->uid = 0))
> 
> ```
> 
> - 
> 
> ```
> retval = -EINVAL;
> 
> ```

Associated commentary:

> > Setting current-\>uid to zero when options \_\_WCLONE and \_\_WALL are set? The  
> > retval is dead code because of the next line, but it looks like an attempt  
> > to backdoor the kernel, does it not?

---

<div class="post-metadata">

**Author:** ![elucify](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f17d59/32.png) [@elucify](https://www.ruby-forum.com/u/elucify)\
**Post date:** [February 20, 2006, 3:59am UTC](https://www.ruby-forum.com/t/if-statement-and-if-modifier-not-equivalent/70424/8 "2006-02-20T03:59:34Z")

</div>

On 2/19/06, Garance A Drosehn [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Do you really want ‘if a = 1’ there, and not ‘if a == 1’?

yes. I imagine so.

in a more complicated example

if ( a = get\_something\_that\_might\_be\_nil\_or\_false )  
puts a  
end

---

<div class="post-metadata">

**Author:** ![elucify](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f17d59/32.png) [@elucify](https://www.ruby-forum.com/u/elucify)\
**Post date:** [February 21, 2006, 1:45am UTC](https://www.ruby-forum.com/t/if-statement-and-if-modifier-not-equivalent/70424/9 "2006-02-21T01:45:00Z")

</div>

DÅ?a Pondelok 20 FebruÃ¡r 2006 14:53 removed\_email\_address@domain.invalid napÃ­sal:

> > It sure does. Note “current-\>uid = 0”, not “current-\>uid == 0”.  
> > Good eyes, I missed that. This function is sys\_wait4() so by passing in  
> > \_\_WCLONE|\_\_WALL you are root. How nice.

Ow.

David V.
