Aliasing return false

Discussion in 'Ruby' started by Ari Brown, Sep 27, 2007.

  1. Ari Brown

    Ari Brown Guest

    Hey all,

    I'm writing a quick DSL, and in it, I end up having to use return
    false unless {} quite a bunch.

    Since I still need everything outside of the return false's, Stefan
    <apeiros>'s Stepper code is of no use, although it is still quite
    awesome.

    Is there a way to alias it from, say:

    return false unless { klass.method? }

    to:

    rule { klass.method? }

    Thanks,
    Ari
    --------------------------------------------|
    If you're not living on the edge,
    then you're just wasting space.
    Ari Brown, Sep 27, 2007
    #1
    1. Advertising

  2. On 9/26/07, Ari Brown <> wrote:
    > Hey all,
    >
    > I'm writing a quick DSL, and in it, I end up having to use return
    > false unless {} quite a bunch.
    >
    > Since I still need everything outside of the return false's, Stefan
    > <apeiros>'s Stepper code is of no use, although it is still quite
    > awesome.
    >
    > Is there a way to alias it from, say:
    >
    > return false unless { klass.method? }
    >
    > to:
    >
    > rule { klass.method? }
    >


    this is evil

    % cat rule.rb
    def rule(&b)
    r = b.call
    eval("return false", b) unless r
    end

    def a
    rule { false }
    0
    end

    def b
    rule { true }
    1
    end

    p a
    p b

    % ruby rule.rb
    false
    1
    Logan Capaldo, Sep 27, 2007
    #2
    1. Advertising

  3. Ari Brown

    Ari Brown Guest

    On Sep 26, 2007, at 7:24 PM, Logan Capaldo wrote:

    > this is evil


    Because it uses eval?

    >
    > % cat rule.rb
    > def rule(&b)
    > r = b.call
    > eval("return false", b) unless r
    > end
    >
    > def a
    > rule { false }
    > 0
    > end
    >
    > def b
    > rule { true }
    > 1
    > end
    >
    > p a
    > p b
    >
    > % ruby rule.rb
    > false
    > 1


    Damn you, evil geniuses! Damn you!

    I can't believe I didn't think of this. This is why you win and I don't.

    Thanks ridonculously,
    Ari
    -------------------------------------------|
    Nietzsche is my copilot
    Ari Brown, Sep 27, 2007
    #3
  4. On 9/26/07, Ari Brown <> wrote:
    >
    > On Sep 26, 2007, at 7:24 PM, Logan Capaldo wrote:
    >
    > > this is evil

    >
    > Because it uses eval?
    >

    Because it uses eval and assumes that returning from an activation
    frame below the one we are in will do the right thing. I don't know
    for instance, how this behaves in the presence of things like ensure
    blocks, etc.
    > >
    > > % cat rule.rb
    > > def rule(&b)
    > > r = b.call
    > > eval("return false", b) unless r
    > > end
    > >
    > > def a
    > > rule { false }
    > > 0
    > > end
    > >
    > > def b
    > > rule { true }
    > > 1
    > > end
    > >
    > > p a
    > > p b
    > >
    > > % ruby rule.rb
    > > false
    > > 1

    >
    > Damn you, evil geniuses! Damn you!
    >
    > I can't believe I didn't think of this. This is why you win and I don't.
    >
    > Thanks ridonculously,
    > Ari
    > -------------------------------------------|
    > Nietzsche is my copilot
    >
    >
    >
    >
    Logan Capaldo, Sep 27, 2007
    #4
  5. Ari Brown wrote:
    > Hey all,
    >
    > I'm writing a quick DSL, and in it, I end up having to use return false
    > unless {} quite a bunch.
    >
    > Since I still need everything outside of the return false's, Stefan
    > <apeiros>'s Stepper code is of no use, although it is still quite awesome.
    >
    > Is there a way to alias it from, say:
    >
    > return false unless { klass.method? }
    >
    > to:
    >
    > rule { klass.method? }



    class RuleException < Exception; end

    def rules
    yield
    rescue RuleException
    end

    def rule(&b)
    raise RuleException unless yield
    end

    rules do
    p 1
    rule {true}
    p 2
    rule {false}
    p 3
    end

    __END__

    Output:

    1
    2

    --
    vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
    Joel VanderWerf, Sep 27, 2007
    #5
  6. Joel VanderWerf wrote:
    > Ari Brown wrote:
    >> Hey all,
    >>
    >> I'm writing a quick DSL, and in it, I end up having to use return
    >> false unless {} quite a bunch.
    >>
    >> Since I still need everything outside of the return false's, Stefan
    >> <apeiros>'s Stepper code is of no use, although it is still quite
    >> awesome.
    >>
    >> Is there a way to alias it from, say:
    >>
    >> return false unless { klass.method? }
    >>
    >> to:
    >>
    >> rule { klass.method? }


    Ignore previous post, it didn't really explain how you would have to use
    this construct:

    class RuleException < Exception; end

    def rules
    yield
    rescue RuleException
    false # be explicit and return false instead of nil
    end

    def rule(&b)
    raise RuleException unless yield
    end

    def some_method(arg)
    rules do
    rule {/foo/ !~ arg}
    rule {/bar/ !~ arg}
    true # could move this line after the "yield"
    end
    end

    p some_method("aaaa")
    p some_method("bar")

    __END__

    Output:

    true
    false

    --
    vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
    Joel VanderWerf, Sep 27, 2007
    #6
    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. André
    Replies:
    3
    Views:
    1,558
  2. Rajarshi

    0 == False but [] != False?

    Rajarshi, May 24, 2007, in forum: Python
    Replies:
    20
    Views:
    688
    Erik Max Francis
    May 30, 2007
  3. w i l l
    Replies:
    4
    Views:
    230
    Dan Brussee
    Jul 4, 2003
  4. Replies:
    1
    Views:
    425
    Randy Webb
    Oct 6, 2005
  5. Replies:
    10
    Views:
    299
    Thomas 'PointedEars' Lahn
    Feb 16, 2006
Loading...

Share This Page