[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

block variable not working how I expect it to work

James Dinkel

7/11/2008 4:29:00 PM

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 Dinkel
--
Posted via http://www.ruby-....

4 Answers

Martin DeMello

7/11/2008 5:11:00 PM

0

On Fri, Jul 11, 2008 at 9:28 AM, James Dinkel <jdinkel@gmail.com> wrote:
> 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?

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

Karl von Laudermann

7/11/2008 5:21:00 PM

0

On Jul 11, 1:11 pm, Martin DeMello <martindeme...@gmail.com> wrote:
> Therefore your code should be:
>
>  def myblock
>     testvar = ['cool', 'array']
>     testvar = yield testvar
>     print testvar
>  end
>
>   myblock do |stuff|
>     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

James Dinkel

7/11/2008 5:24:00 PM

0

Martin DeMello wrote:
> <snip>

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

--
Posted via http://www.ruby-....

Martin DeMello

7/11/2008 5:28:00 PM

0

On Fri, Jul 11, 2008 at 10:20 AM, Karl von Laudermann
<doodpants@mailinator.com> wrote:
> On Jul 11, 1:11 pm, Martin DeMello <martindeme...@gmail.com> wrote:
>> Therefore your code should be:
>>
>> def myblock
>> testvar = ['cool', 'array']
>> testvar = yield testvar
>> print testvar
>> end
>>
>> myblock do |stuff|
>> 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