# Help : a scoping question

**URL:** <https://www.ruby-forum.com/t/help-a-scoping-question/174178>\
**Category:** Ruby\
**Created:** [August 21, 2009, 7:58pm UTC](https://www.ruby-forum.com/t/help-a-scoping-question/174178 "2009-08-21T19:58:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![micheldogger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b5e925/32.png) [@micheldogger](https://www.ruby-forum.com/u/micheldogger)\
**Post date:** [August 21, 2009, 7:58pm UTC](https://www.ruby-forum.com/t/help-a-scoping-question/174178/1 "2009-08-21T19:58:12Z")

</div>

A meta-programming question. More precisely a scoping problem.

In short, is there a ruby way to execute a parameter-less block with a  
given receiver ?

Here below my example : I am manipulating a pattern of rectangular boxes  
in a plane, with a tree structure given by enclosure, and want to  
describe them by a convenient DSL. I am keeping some parameters from the  
real code to give a flavor of the example.

class Box

HORIZ = 1  
VERT = 2  
FIXED = 4  
ADJUST = 8

def initialize(enclosing, flag, columns = [], height = 0)  
@enclosing = enclosing  
enclosing \<\< self if enclosing  
@enclosed = []  
…  
end

def \<\<(other)  
@enclosed \<\< other  
end

def box(params = {}, &block)  
newbox = Box.new(self, params[:opts], params[:colums] || [],  
params[:height] || 0)  
block.call(newbox) if block  
newbox  
end

…

end # class box

# duplication, part of the problem

def main\_box(params ={}, &block)  
newbox = Box.new(nil, params[:opts], params[:colums] || [],  
params[:height] || 0)  
block.call(newbox) if block  
newbox  
end

big\_box = main\_box :opts =\> VERT do |s|  
s.box :opts =\> HORIZ, :colums =\> [1,2,3,4,5,6,7,8,9], :height =\> 10  
s.box :opts =\> HORIZ do |b|  
b.box :opts =\> VERT, :colums =\> [7,8,9], :height =\> 90  
b.box :opts =\> VERT do |z|  
z.box :opts =\> HORIZ | ADJUST, :colums =\> [1,2,3,4,5,6]  
z.box :opts =\> HORIZ do |y|  
y.box :opts =\> VERT | FIXED, :colums =\> [4,5,6], :height =\> 70  
y.box :opts =\> VERT do |x|  
x.box :opts =\> HORIZ| FIXED, :colums =\> [1,2,3], :height =\> 20  
x.box :opts =\> HORIZ | ADJUST,:colums =\> [1,2,3]  
x.box :opts =\> HORIZ | FIXED, :colums =\> [1,2,3], :height =\>35  
end  
end  
end  
end  
end

The local variables in the blocks could be all the same, and are useless  
to the DSL, but they seem necessary to secure the scopes. My question is  
: is there a way to write the method ‘box’ so that it accepts the  
simplified DSL without variables :

… box params do  
box params do

Apologies for my English.

Michel D.

---

<div class="post-metadata">

**Author:** ![micheldogger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b5e925/32.png) [@micheldogger](https://www.ruby-forum.com/u/micheldogger)\
**Post date:** [August 21, 2009, 8:07pm UTC](https://www.ruby-forum.com/t/help-a-scoping-question/174178/2 "2009-08-21T20:07:17Z")

</div>

Michel D. wrote:

> In short, is there a ruby way to execute a parameter-less block with a  
> given receiver ?

pr = proc {p self}  
2.instance\_eval &pr  
=\> 2

Is that what you’re looking for?

---

<div class="post-metadata">

**Author:** ![micheldogger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b5e925/32.png) [@micheldogger](https://www.ruby-forum.com/u/micheldogger)\
**Post date:** [August 21, 2009, 9:05pm UTC](https://www.ruby-forum.com/t/help-a-scoping-question/174178/3 "2009-08-21T21:05:57Z")

</div>

Joel VanderWerf wrote:

> Michel D. wrote:
> 
> > In short, is there a ruby way to execute a parameter-less block with a  
> > given receiver ?
> 
> pr = proc {p self}  
> 2.instance\_eval &pr  
> =\> 2
> 
> Is that what you’re looking for?

Of course, you’re right : “newbox.instance\_eval &block if block” works.  
Many thanks !

M
