Limiting the scope of a variable

Hello,

with a function like that

def let(*a)
yield(*a)
end

the scope of variables can be limited, e.g. like this:

let {
x = “foo”
# … do something with x …
# here’s the end of scope of x
}

let {
x = “bar” # this is another, different x
# … do something with x …
}

Is there already a standard means working like this? If it’s a
standard library function, what’s its name?

Regards
Thomas

Why not just use methods?

On Thu, May 1, 2008 at 8:30 AM, Thomas H.
[email protected] wrote:

def let(*a)
yield(*a)
end

the scope of variables can be limited, e.g. like this:

The Invocation Construction Kit (Ick) gem includes a “let” function
(among others). See:

http://ick.rubyforge.org/

“Ick provides the tools needed to easily build your own execution
abstractions like the “Maybe” monad or the four canonical block
evaluators.”

Regards,

Matt.

Paul McMahon [email protected] wrote/schrieb [email protected]:

Why not just use methods?

Inside the block, that is passed to function “let”, many variables
could be referenced which are defined outside that block. Using
methods, these variables had to be passed explicitely. Your question
is a special case of the more general question: Why using blocks at
all when programming Ruby? Every block could be replaced by a method.

Regards
Thomas

def let;yield;end

But you can’t pass arguments to the scope. Whatever that might be good
for.

mfg, simon … l

On 01.05.2008 11:13, Thomas H. wrote:

Paul McMahon [email protected] wrote/schrieb [email protected]:

Why not just use methods?

Inside the block, that is passed to function “let”, many variables
could be referenced which are defined outside that block. Using
methods, these variables had to be passed explicitely. Your question
is a special case of the more general question: Why using blocks at
all when programming Ruby? Every block could be replaced by a method.

I had read Paul’s reply a little differently: this was not about blocks
vs. methods but about refactoring code. If you find yourself needing to
separate a lot of scopes inside a method, chances are that your code
would benefit from refactoring into several methods.

And yes, in that case you do need to pass data explicitly that you need
inside but this has the advantage of making it clearer which state is
needed in each section.

Btw, Thomas, this definition serves the same purpose as yours and is
simpler:

def let;yield;end

You could as well do

1.times {
x=20

}

:slight_smile:

Kind regards

robert

On May 1, 2008, at 1:30 AM, Thomas H. wrote:

let {
Is there already a standard means working like this? If it’s a
standard library function, what’s its name?

the simplest way is to use instance_eval

cfp:~ > cat a.rb
instance_eval {
x = ‘foo’
p x
}

p x rescue :no_x

instance_eval {
x = ‘bar’
p x
}

p x rescue :no_x

cfp:~ > ruby a.rb
“foo”
“bar”

if you prefer you can do

class Object
alias_method “scope”, “instance_eval”
end

and then

scope{ x = 42; p x }

etc.

a @ http://codeforpeople.com/

Matt M. [email protected] wrote/schrieb
[email protected]:

The Invocation Construction Kit (Ick) gem includes a “let” function
(among others). See:

http://ick.rubyforge.org/

Sounds very promising, thanks! Seems that I’m not the first person
which is missing block structure in Ruby.

Regards
Thomas

Simon K. [email protected] wrote/schrieb
[email protected]:

But you can’t pass arguments to the scope. Whatever that might be good
for.

Yes, that was my intention, especially with regard to Ruby 1.9 and its
local block parameters - those preceded with “;”. I think the changes
on those parameters are limited to inside the block, aren’t they? Then
my Ruby-method “let” has much in common with Scheme’s syntactical form
“let”, therefore the name. But nevertheless I’d prefer one of Ruby’s
standard library function, if there’s one which behaves in a similar
way. Is there one?

Regards
Thomas

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs