Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Adjusting the Scope of Blocks

Reply
Thread Tools

Adjusting the Scope of Blocks

 
 
Mark Cox
Guest
Posts: n/a
 
      12-09-2003
Hi,

Is there a way to give a block associated with a method call the same scope
as if it were executing inside the method.

ie.

class Foo
ACONST = 1

def input_flags
@FLAG_SEL = INPUT
yield
end

def output_flags
@FLAG_SEL = OUTPUT
yield
end

def set(flgs)
set_flags(@FLAG_SEL, flgs)
end
end

f = Foo.new

f.input_flags {
set ACONST
}

f.output_flags {
set ACONST
}

Thanks in advance.

Mark Cox
 
Reply With Quote
 
 
 
 
Gavin Sinclair
Guest
Posts: n/a
 
      12-09-2003
> Hi,
>
> Is there a way to give a block associated with a method call the same
> scope as if it were executing inside the method.
>
> ie.
>
> class Foo
> ACONST = 1
>
> def input_flags
> @FLAG_SEL = INPUT
> yield
> end
>
> def output_flags
> @FLAG_SEL = OUTPUT
> yield
> end
>
> def set(flgs)
> set_flags(@FLAG_SEL, flgs)
> end
> end
>
> f = Foo.new
>
> f.input_flags {
> set ACONST
> }
>
> f.output_flags {
> set ACONST
> }



I've never done it myself, but you might try:

class Foo
def input_flags(&block)
@FLAG_SEL = INPUT
instance_eval &block
end
end

Gavin





 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      12-09-2003

"Gavin Sinclair" <> schrieb im Newsbeitrag
news:49755.203.185.214.34.1070949030.squirrel@webm ail.imagineis.com...
> > Hi,
> >
> > Is there a way to give a block associated with a method call the same
> > scope as if it were executing inside the method.
> >
> > ie.
> >
> > class Foo
> > ACONST = 1
> >
> > def input_flags
> > @FLAG_SEL = INPUT
> > yield
> > end
> >
> > def output_flags
> > @FLAG_SEL = OUTPUT
> > yield
> > end
> >
> > def set(flgs)
> > set_flags(@FLAG_SEL, flgs)
> > end
> > end
> >
> > f = Foo.new
> >
> > f.input_flags {
> > set ACONST
> > }
> >
> > f.output_flags {
> > set ACONST
> > }

>
>
> I've never done it myself, but you might try:


This works as expected. Note however that with this approach method local
variables are not accessible.

> class Foo
> def input_flags(&block)
> @FLAG_SEL = INPUT
> instance_eval &block
> end
> end


You can of course yield all values that should be accessible for the
block.

Another approach is to use "eval" with a binding, although be warned that
this can have serious security implications:

def tester(code)
foo="bar"
b = binding
eval( code, b )
end

tester "puts foo"
=> prints "bar"

But why don't you just make "input_flags" and "output_flags" accessors to
member variables? Then you can do "foo.input_flags |= Foo::ACONST".

Or you define a special bit set type:

class Bitset
attr_accessor :val
def initialize(v=0); @val=0; end

def set(x); @val|=x; end
def toggle(x); @val^=x; end
def reset(x); set(x); toggle(x); end
def to_i; val; end
end

Kind regards

robert

 
Reply With Quote
 
Mark Cox
Guest
Posts: n/a
 
      12-10-2003
Thanks for your reply.

I've written a ruby binding for the low level terminal IO functions
for unix. There is about 60 to 80 constants so I was sort of hoping to
remove the need to do Foo::Const all the time to set flags.

I know I'm just lazy.

Mark.

Robert Klemme wrote:

>
> "Gavin Sinclair" <> schrieb im Newsbeitrag
> news:49755.203.185.214.34.1070949030.squirrel@webm ail.imagineis.com...
>> > Hi,
>> >
>> > Is there a way to give a block associated with a method call the same
>> > scope as if it were executing inside the method.
>> >
>> > ie.
>> >
>> > class Foo
>> > ACONST = 1
>> >
>> > def input_flags
>> > @FLAG_SEL = INPUT
>> > yield
>> > end
>> >
>> > def output_flags
>> > @FLAG_SEL = OUTPUT
>> > yield
>> > end
>> >
>> > def set(flgs)
>> > set_flags(@FLAG_SEL, flgs)
>> > end
>> > end
>> >
>> > f = Foo.new
>> >
>> > f.input_flags {
>> > set ACONST
>> > }
>> >
>> > f.output_flags {
>> > set ACONST
>> > }

>>
>>
>> I've never done it myself, but you might try:

>
> This works as expected. Note however that with this approach method local
> variables are not accessible.
>
>> class Foo
>> def input_flags(&block)
>> @FLAG_SEL = INPUT
>> instance_eval &block
>> end
>> end

>
> You can of course yield all values that should be accessible for the
> block.
>
> Another approach is to use "eval" with a binding, although be warned that
> this can have serious security implications:
>
> def tester(code)
> foo="bar"
> b = binding
> eval( code, b )
> end
>
> tester "puts foo"
> => prints "bar"
>
> But why don't you just make "input_flags" and "output_flags" accessors to
> member variables? Then you can do "foo.input_flags |= Foo::ACONST".
>
> Or you define a special bit set type:
>
> class Bitset
> attr_accessor :val
> def initialize(v=0); @val=0; end
>
> def set(x); @val|=x; end
> def toggle(x); @val^=x; end
> def reset(x); set(x); toggle(x); end
> def to_i; val; end
> end
>
> Kind regards
>
> robert


 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      12-10-2003

"Mark Cox" <> schrieb im Newsbeitrag
news:...
> Thanks for your reply.
>
> I've written a ruby binding for the low level terminal IO functions
> for unix. There is about 60 to 80 constants so I was sort of hoping to
> remove the need to do Foo::Const all the time to set flags.


You could do something like this:

class Foo
FOO = 1
BAR = 2

def set(*flag)
flag = flag[0] if flag.size == 1

case flag
when Symbol, String
flag = self.class.const_get(flag)
puts "set flag #{flag}"
when Numeric
flag = flag.to_i
puts "set flag #{flag}"
when Enumerable
flag.each {|f| set(f)}
end
end
end

f=Foo.new
f.set "BAR"
f.set :FOO
f.set ["FOO", "BAR"]
f.set "FOO", "BAR"

There's many routs to Rome...

> I know I'm just lazy.


You really are. Shame on you!

robert

>
> Mark.
>
> Robert Klemme wrote:
>
> >
> > "Gavin Sinclair" <> schrieb im Newsbeitrag
> > news:49755.203.185.214.34.1070949030.squirrel@webm ail.imagineis.com...
> >> > Hi,
> >> >
> >> > Is there a way to give a block associated with a method call the

same
> >> > scope as if it were executing inside the method.
> >> >
> >> > ie.
> >> >
> >> > class Foo
> >> > ACONST = 1
> >> >
> >> > def input_flags
> >> > @FLAG_SEL = INPUT
> >> > yield
> >> > end
> >> >
> >> > def output_flags
> >> > @FLAG_SEL = OUTPUT
> >> > yield
> >> > end
> >> >
> >> > def set(flgs)
> >> > set_flags(@FLAG_SEL, flgs)
> >> > end
> >> > end
> >> >
> >> > f = Foo.new
> >> >
> >> > f.input_flags {
> >> > set ACONST
> >> > }
> >> >
> >> > f.output_flags {
> >> > set ACONST
> >> > }
> >>
> >>
> >> I've never done it myself, but you might try:

> >
> > This works as expected. Note however that with this approach method

local
> > variables are not accessible.
> >
> >> class Foo
> >> def input_flags(&block)
> >> @FLAG_SEL = INPUT
> >> instance_eval &block
> >> end
> >> end

> >
> > You can of course yield all values that should be accessible for the
> > block.
> >
> > Another approach is to use "eval" with a binding, although be warned

that
> > this can have serious security implications:
> >
> > def tester(code)
> > foo="bar"
> > b = binding
> > eval( code, b )
> > end
> >
> > tester "puts foo"
> > => prints "bar"
> >
> > But why don't you just make "input_flags" and "output_flags" accessors

to
> > member variables? Then you can do "foo.input_flags |= Foo::ACONST".
> >
> > Or you define a special bit set type:
> >
> > class Bitset
> > attr_accessor :val
> > def initialize(v=0); @val=0; end
> >
> > def set(x); @val|=x; end
> > def toggle(x); @val^=x; end
> > def reset(x); set(x); toggle(x); end
> > def to_i; val; end
> > end
> >
> > Kind regards
> >
> > robert

>


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Blocks for scope control Arved Sandstrom Java 32 02-07-2012 02:24 AM
Methods and blocks - not that clear when blocks passed into Steven Taylor Ruby 9 04-27-2009 08:46 AM
"Building Blocks" are "Application Blocks" Arjen ASP .Net 3 02-27-2005 01:06 AM
procs/blocks - blocks with procs, blocks with blocks? matt Ruby 1 08-06-2004 01:33 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57