Help : a scoping question

Discussion in 'Ruby' started by Michel Demazure, Aug 21, 2009.

  1. 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[:eek:pts], 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[:eek:pts], params[:colums] || [],
    params[:height] || 0)
    block.call(newbox) if block
    newbox
    end

    big_box = main_box :eek:pts => VERT do |s|
    s.box :eek:pts => HORIZ, :colums => [1,2,3,4,5,6,7,8,9], :height => 10
    s.box :eek:pts => HORIZ do |b|
    b.box :eek:pts => VERT, :colums => [7,8,9], :height => 90
    b.box :eek:pts => VERT do |z|
    z.box :eek:pts => HORIZ | ADJUST, :colums => [1,2,3,4,5,6]
    z.box :eek:pts => HORIZ do |y|
    y.box :eek:pts => VERT | FIXED, :colums => [4,5,6], :height => 70
    y.box :eek:pts => VERT do |x|
    x.box :eek:pts => HORIZ| FIXED, :colums => [1,2,3], :height => 20
    x.box :eek:pts => HORIZ | ADJUST,:colums => [1,2,3]
    x.box :eek:pts => 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 Demazure
    --
    Posted via http://www.ruby-forum.com/.
     
    Michel Demazure, Aug 21, 2009
    #1
    1. Advertising

  2. Michel Demazure 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?

    --
    vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
     
    Joel VanderWerf, Aug 21, 2009
    #2
    1. Advertising

  3. Joel VanderWerf wrote:
    > Michel Demazure 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
    --
    Posted via http://www.ruby-forum.com/.
     
    Michel Demazure, Aug 21, 2009
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt S.
    Replies:
    0
    Views:
    411
    Matt S.
    May 4, 2004
  2. Russ Perry Jr
    Replies:
    2
    Views:
    578
    Russ Perry Jr
    May 19, 2004
  3. Brendan
    Replies:
    10
    Views:
    480
    Terry Reedy
    Apr 4, 2005
  4. bens
    Replies:
    3
    Views:
    329
  5. kj
    Replies:
    68
    Views:
    1,241
Loading...

Share This Page