# Aliasing return false

**URL:** <https://www.ruby-forum.com/t/aliasing-return-false/115630>\
**Category:** Ruby\
**Created:** [September 27, 2007, 1:09am UTC](https://www.ruby-forum.com/t/aliasing-return-false/115630 "2007-09-27T01:09:22Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ari\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a698b9/32.png) [@Ari\_B](https://www.ruby-forum.com/u/Ari_B)\
**Post date:** [September 27, 2007, 1:09am UTC](https://www.ruby-forum.com/t/aliasing-return-false/115630/1 "2007-09-27T01:09:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Ari\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a698b9/32.png) [@Ari\_B](https://www.ruby-forum.com/u/Ari_B)\
**Post date:** [September 27, 2007, 1:24am UTC](https://www.ruby-forum.com/t/aliasing-return-false/115630/2 "2007-09-27T01:24:35Z")

</div>

On 9/26/07, Ari B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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

---

<div class="post-metadata">

**Author:** ![Ari\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a698b9/32.png) [@Ari\_B](https://www.ruby-forum.com/u/Ari_B)\
**Post date:** [September 27, 2007, 1:41am UTC](https://www.ruby-forum.com/t/aliasing-return-false/115630/3 "2007-09-27T01:41:32Z")

</div>

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

> this is evil

Because it uses eval?

> end  
> 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

---

<div class="post-metadata">

**Author:** ![Ari\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a698b9/32.png) [@Ari\_B](https://www.ruby-forum.com/u/Ari_B)\
**Post date:** [September 27, 2007, 1:53am UTC](https://www.ruby-forum.com/t/aliasing-return-false/115630/4 "2007-09-27T01:53:48Z")

</div>

On 9/26/07, Ari B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Sep 26, 2007, at 7:24 PM, Logan C. 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.

---

<div class="post-metadata">

**Author:** ![Ari\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a698b9/32.png) [@Ari\_B](https://www.ruby-forum.com/u/Ari_B)\
**Post date:** [September 27, 2007, 3:39am UTC](https://www.ruby-forum.com/t/aliasing-return-false/115630/5 "2007-09-27T03:39:34Z")

</div>

Ari B. wrote:

> 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

---

<div class="post-metadata">

**Author:** ![Ari\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a698b9/32.png) [@Ari\_B](https://www.ruby-forum.com/u/Ari_B)\
**Post date:** [September 27, 2007, 4:00am UTC](https://www.ruby-forum.com/t/aliasing-return-false/115630/6 "2007-09-27T04:00:01Z")

</div>

Joel VanderWerf wrote:

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