# Question on do block

**URL:** <https://www.ruby-forum.com/t/question-on-do-block/144651>\
**Category:** Ruby\
**Created:** [July 31, 2008, 8:10pm UTC](https://www.ruby-forum.com/t/question-on-do-block/144651 "2008-07-31T20:10:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![CompGeek78](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e480ec/32.png) [@CompGeek78](https://www.ruby-forum.com/u/CompGeek78)\
**Post date:** [July 31, 2008, 8:10pm UTC](https://www.ruby-forum.com/t/question-on-do-block/144651/1 "2008-07-31T20:10:40Z")

</div>

While going through some practice code from a book, I ran into an odd  
issue with this do block.

The book put the code this way and it worked fine.

def mtdarry  
10.times do |num|  
square = num \* num  
return num, square if num \> 7  
end  
end

num, square = mtdarry  
puts num  
puts square

This returns 8 and 64, which makes sense.

The problem I ran into is in changing the \> to a =.

def mtdarry  
10.times do |num|  
square = num \* num  
return num, square if num = 7  
end  
end

num, square = mtdarry  
puts num  
puts square

At this point, it outputs 7 and 0. Why does it not calculate the value  
of square properly?

---

<div class="post-metadata">

**Author:** ![CompGeek78](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e480ec/32.png) [@CompGeek78](https://www.ruby-forum.com/u/CompGeek78)\
**Post date:** [July 31, 2008, 8:15pm UTC](https://www.ruby-forum.com/t/question-on-do-block/144651/2 "2008-07-31T20:15:56Z")

</div>

On Thu, Jul 31, 2008 at 2:09 PM, CompGeek78 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> The problem I ran into is in changing the \> to a =.
> 
> def mtdarry  
> 10.times do |num|  
> square = num \* num  
> return num, square if num = 7  
> end  
> end

You want an equality check, not assignment.

> > a = 1  
> > =\> 1  
> > a == 1  
> > =\> true  
> > a == 2  
> > =\> false

num = 7 is always true, because all values except false and nil are  
true in the boolean sense in Ruby.  
num == 7 is only true when num is 7.

-greg

---

<div class="post-metadata">

**Author:** ![CompGeek78](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e480ec/32.png) [@CompGeek78](https://www.ruby-forum.com/u/CompGeek78)\
**Post date:** [July 31, 2008, 8:20pm UTC](https://www.ruby-forum.com/t/question-on-do-block/144651/3 "2008-07-31T20:20:38Z")

</div>

On Jul 31, 12:14 pm, Gregory B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> You want an equality check, not assignment.  
> true in the boolean sense in Ruby.  
> num == 7 is only true when num is 7.
> 
> -greg

Oh gads I’m an idiot…thanks.
