# Block variable not working how I expect it to work

**URL:** <https://www.ruby-forum.com/t/block-variable-not-working-how-i-expect-it-to-work/142806>\
**Category:** Ruby\
**Created:** [July 11, 2008, 6:33pm UTC](https://www.ruby-forum.com/t/block-variable-not-working-how-i-expect-it-to-work/142806 "2008-07-11T18:33:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jimdeuc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b77776/32.png) [@jimdeuc](https://www.ruby-forum.com/u/jimdeuc)\
**Post date:** [July 11, 2008, 6:33pm UTC](https://www.ruby-forum.com/t/block-variable-not-working-how-i-expect-it-to-work/142806/1 "2008-07-11T18:33:02Z")

</div>

Here is my ruby version:  
ruby 1.8.6 (2007-09-24 patchlevel 111) [i386-mswin32]

## Here is the block I wrote (this is actually a little test block for testing part of a larger block):

## def myblock testvar = [‘cool’, ’ array’] yield testvar print testvar end

## Now here is a snippet of code that isn’t giving the result I would expect:

## myblock do |stuff| stuff = ‘dumb string’ end

This outputs “cool array” when I would expect it to output “dumb string”  
since I changed the variable when executing the block. So my question  
is: how can I get ‘testvar’ to change to whatever the end result of  
‘stuff’ is when the block executes?

Thanks,

James D.

---

<div class="post-metadata">

**Author:** ![jimdeuc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b77776/32.png) [@jimdeuc](https://www.ruby-forum.com/u/jimdeuc)\
**Post date:** [July 11, 2008, 7:16pm UTC](https://www.ruby-forum.com/t/block-variable-not-working-how-i-expect-it-to-work/142806/2 "2008-07-11T19:16:14Z")

</div>

On Fri, Jul 11, 2008 at 9:28 AM, James D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end  
> This outputs “cool array” when I would expect it to output “dumb string”  
> since I changed the variable when executing the block. So my question  
> is: how can I get ‘testvar’ to change to whatever the end result of  
> ‘stuff’ is when the block executes?

In ruby, variables are just labels pasted onto objects. What gets  
passed around are the objects themselves, but the = operator pastes  
the variable onto a new object. So when you say

> yield testvar

you’re passing the _object_ testvar is currently labelling to the  
block. When you say

> myblock do |stuff|

myblock receives the object and pastes the label ‘stuff’ on it. Then  
when you say

> stuff = ‘dumb string’

you are creating a new object, ‘dumb string’, removing the ‘stuff’  
label from the original object, and pasting it onto your new object.  
Back in ‘myblock’, of course, ‘testvar’ is still pasted on the  
original object, and won’t have changed. What you essentially want to  
do is remove the variable ‘testvar’ from the first object and paste it  
onto something else that you obtain from the block. To do that, you’ll  
need to use the _return value_ of the block, not any of its variable  
assignments. Therefore your code should be:

def myblock  
testvar = [‘cool’, ‘array’]  
testvar = yield testvar  
print testvar  
end

myblock do |stuff|  
return “dumb string”  
end

martin

---

<div class="post-metadata">

**Author:** ![jimdeuc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b77776/32.png) [@jimdeuc](https://www.ruby-forum.com/u/jimdeuc)\
**Post date:** [July 11, 2008, 7:27pm UTC](https://www.ruby-forum.com/t/block-variable-not-working-how-i-expect-it-to-work/142806/3 "2008-07-11T19:27:45Z")

</div>

Martin DeMello wrote:

> 

Wow, I couldn’t have asked for a better explanation. I _suspected_ I  
may have to do something like “testvar = yield testvar” but it didn’t  
make any sense why I couldn’t just edit the variable directly. Now I  
know. Thanks!

James

---

<div class="post-metadata">

**Author:** ![jimdeuc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b77776/32.png) [@jimdeuc](https://www.ruby-forum.com/u/jimdeuc)\
**Post date:** [July 11, 2008, 7:26pm UTC](https://www.ruby-forum.com/t/block-variable-not-working-how-i-expect-it-to-work/142806/4 "2008-07-11T19:26:12Z")

</div>

On Jul 11, 1:11 pm, Martin DeMello [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end  
> I was going to post a response similar to yours but you beat me to it.  
> However, I tried your version, and the return statement is causing an  
> error. You instead just want to do:

myblock do |stuff|  
“dumb string”  
end

---

<div class="post-metadata">

**Author:** ![jimdeuc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b77776/32.png) [@jimdeuc](https://www.ruby-forum.com/u/jimdeuc)\
**Post date:** [July 11, 2008, 7:32pm UTC](https://www.ruby-forum.com/t/block-variable-not-working-how-i-expect-it-to-work/142806/5 "2008-07-11T19:32:38Z")

</div>

On Fri, Jul 11, 2008 at 10:20 AM, Karl von Laudermann  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > ```
> > return "dumb string"
> > 
> > ```
> > 
> > end
> 
> I was going to post a response similar to yours but you beat me to it.  
> However, I tried your version, and the return statement is causing an  
> error. You instead just want to do:
> 
> myblock do |stuff|  
> “dumb string”  
> end

oops, yes - that’ll teach me to type code straight into email. been  
doing too much javascript of late!

martin
