limiting the scope of a variable

T

Thomas Hafner

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
 
M

Matt Mower

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.
 
T

Thomas Hafner

Paul McMahon said:
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
 
R

Robert Klemme

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
# ...
}

:)

Kind regards

robert
 
A

ara.t.howard

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?


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/
 
T

Thomas Hafner

Simon Krahnke said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top