[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Scope of block

daniel

12/10/2006 8:18:00 PM

Hello i have a question about scopes of block
I want to define a block at one time.
But when it get's called it should have the scope (vars ed )
of the calling environement

Example :
------------------------------

class BlockTest

attr_accessor :myblock

def process(&block)
self.myblock = block
end
end


t = BlockTest.new
t.process {

"var = #{out_of_scope_var}"
}

out_of_scope_var = "daniel"
p t.myblock.call

Results in :

blocktest.rb:15: undefined local variable or method `out_of_scope_var'
for main:Object (NameError) from blocktest.rb:19

How can i call the proc so that the out_of_scope_var is known :

result would be :

so i would get :

daniel-wijnands-powerbook58-2:~/jim/vendor/plugins/widget_grid/lib
danielwijnands$ ruby blocktest.rb
"var = daniel"

11 Answers

Paul Lutus

12/10/2006 8:28:00 PM

0

daniel wrote:

> Hello i have a question about scopes of block
> I want to define a block at one time.
> But when it get's called it should have the scope (vars ed )
> of the calling environement

Perhaps you will be satisfied that the block receive values from the parent
environment. This is the normal way to solve the problem.

--------------------------------

#!/usr/bin/ruby -w

class BlockTest

attr_accessor :myblock
def process(&block)
self.myblock = block
end
end


t = BlockTest.new
t.process{ |v|
"var = #{v}"
}

v = "daniel"
p t.myblock.call(v)

--------------------------------

"var = daniel"

--
Paul Lutus
http://www.ara...

daniel

12/10/2006 9:03:00 PM

0


Paul Lutus schreef:

>
> Perhaps you will be satisfied that the block receive values from the parent
> environment. This is the normal way to solve the problem.
>

That is how it is solved now, but the proc needs to be flexible.
I call much proc's and i don't now what to pass.
It are not just vars that i want to accessible but also methods ed

Any idea ?

Paul Lutus

12/10/2006 9:22:00 PM

0

daniel wrote:

>
> Paul Lutus schreef:
>
>>
>> Perhaps you will be satisfied that the block receive values from the
>> parent environment. This is the normal way to solve the problem.
>>
>
> That is how it is solved now, but the proc needs to be flexible.
> I call much proc's and i don't now what to pass.
> It are not just vars that i want to accessible but also methods ed
>
> Any idea ?

Use instance variables.

-------------------------------------

#!/usr/bin/ruby -w

class BlockTest
attr_accessor :val
attr_accessor :myblock
def process(&block)
self.myblock = block
end
end


t = BlockTest.new
t.val = "Daniel"
t.process {
"var = #{t.val}"
}

p t.myblock.call

-------------------------------------

I understand this is not a particularly pretty example, it is just meant to
show one approach.

--
Paul Lutus
http://www.ara...

Gustav - Railist

12/10/2006 10:08:00 PM

0

daniel wrote:
> Hello i have a question about scopes of block
> I want to define a block at one time.
> But when it get's called it should have the scope (vars ed )
> of the calling environement
>
> Example :
> ------------------------------
>
> class BlockTest
>
> attr_accessor :myblock
>
> def process(&block)
> self.myblock = block
> end
> end
>
>
> t = BlockTest.new
> t.process {
>
> "var = #{out_of_scope_var}"
> }
>
> out_of_scope_var = "daniel"
> p t.myblock.call
>
> Results in :
>
> blocktest.rb:15: undefined local variable or method `out_of_scope_var'
> for main:Object (NameError) from blocktest.rb:19
>
> How can i call the proc so that the out_of_scope_var is known :
>
> result would be :
>
> so i would get :
>
> daniel-wijnands-powerbook58-2:~/jim/vendor/plugins/widget_grid/lib
> danielwijnands$ ruby blocktest.rb
> "var = daniel"
>
>
>
>
The following seems to work:
#########
class BlockTest
def process(&block)
@block=block
end

def myblock
instance_eval{@block}.call
end
end

out_of_scope_var = "out of scope, me"
t = BlockTest.new
t.process {"var = #{out_of_scope_var}"}

p t.myblock
#=> "var = out of scope, me"
###########
Hope this helps,
Gustav Paul

dblack

12/10/2006 10:14:00 PM

0

Gustav - Railist

12/10/2006 10:20:00 PM

0

dblack@wobblini.net wrote:
> Hi --
>
> On Mon, 11 Dec 2006, Gustav Paul wrote:
>
>> The following seems to work:
>> #########
>> class BlockTest
>> def process(&block)
>> @block=block
>> end
>>
>> def myblock
>> instance_eval{@block}.call
>
> No need for instance_eval there. @block already belongs to self, and
> the instance_eval just resets self to self :-)
>
>
> David
>

LOL!
doh!
:-]

daniel

12/10/2006 10:29:00 PM

0


Gustav Paul schreef:

> The following seems to work:
> #########
> class BlockTest
> def process(&block)
> @block=block
> end
>
> def myblock
> instance_eval{@block}.call
> end
> end
>
> out_of_scope_var = "out of scope, me"
> t = BlockTest.new
> t.process {"var = #{out_of_scope_var}"}
>
> p t.myblock
> #=> "var = out of scope, me"
> ###########
> Hope this helps,
> Gustav Paul

Hi paul, i know that's works, but i need it slightly differtent
because you set out_of_scope_var before you create the block
I want it to be accessible when you set it after you create the block
With your method it is in the scope of the block
I want the block to have access to the scope when you call it
So this should work :


class BlockTest
def process(&block)
@block=block
end
def myblock
instance_eval{@block}.call
end
end


t = BlockTest.new
t.process {"var = #{out_of_scope_var}"}



out_of_scope_var = "out of scope, me"
p t.myblock


But that gives :

blocktest.rb:40: undefined local variable or method `out_of_scope_var'
for main:Object (NameError)
from blocktest.rb:34:in `myblock'
from blocktest.rb:45

:( thanks anyway :)

noonknight@gmail.com

12/11/2006 1:38:00 PM

0

you could add an eval() into your example code, so that:

t = BlockTest.new
t.process { "var = #{out_of_scope_var}" }
out_of_scope_var = "daniel"
p t.myblock.call

would become:

t = BlockTest.new
t.process { "var = #{eval('out_of_scope_var'}" }
out_of_scope_var = "daniel"
p t.myblock.call

hope that helps. --noon

daniel wrote:
> Hello i have a question about scopes of block
> I want to define a block at one time.
> But when it get's called it should have the scope (vars ed )
> of the calling environement
>
> Example :
> ------------------------------
>
> class BlockTest
>
> attr_accessor :myblock
>
> def process(&block)
> self.myblock = block
> end
> end
>
>
> t = BlockTest.new
> t.process {
>
> "var = #{out_of_scope_var}"
> }
>
> out_of_scope_var = "daniel"
> p t.myblock.call
>
> Results in :
>
> blocktest.rb:15: undefined local variable or method `out_of_scope_var'
> for main:Object (NameError) from blocktest.rb:19
>
> How can i call the proc so that the out_of_scope_var is known :
>
> result would be :
>
> so i would get :
>
> daniel-wijnands-powerbook58-2:~/jim/vendor/plugins/widget_grid/lib
> danielwijnands$ ruby blocktest.rb
> "var = daniel"

noonknight@gmail.com

12/11/2006 2:26:00 PM

0

oops, i forgot to put the closing paren. should be:

t.process { "var = #{eval('out_of_scope_var')}" }

--noon

Mason Barge

2/5/2012 5:49:00 PM

0

On Sat, 4 Feb 2012 13:16:00 -0500, Michael Black <et472@ncf.ca> wrote:

>On Sat, 4 Feb 2012, Professor Bubba wrote:
>
>> In article <MPG.2996eca1d51d3f98a4b1@news.optonline.net>, Hunter
>> <buffhunter@my-deja.com> wrote:
>>
>>> Here in New York City as an example other than 30 Rockefeller Plaza was (and
>>> will be again) World Trade Center Plaza. Because of 9/11 1, 2 & 7 World Trade
>>> Center being the most famous buildings. No actual street address in the
>>> conventional sense. One World Trade Center *was* the Street address.
>>
>> IIRC the WTC also had its own Zip code, too.
>>
>I think that's a different matter. Some buildings are so dense, or get so
>much mail, that they get a whole zip code. It's not vanity, but based on
>how much mail they get.

Without disagreeing with anything that's been said, I'd add that even
dedicated ZIP Codes has a street address.

Certainly something like 30 Rockefeller Plaza or Building B, World Trade
Center would be a street address, although you would have to have a suite
number.

But in some other cases, such as IRS facilities, there is a street address
for non-postal delivery and/or to meet local requirements. You can send
mail to "Box 1234, IRS, 33333 Atlanta GA" but it will still have a street
address like "4567 Buford Highway".