# Return\_on

**URL:** <https://www.ruby-forum.com/t/return-on/81608>\
**Category:** Ruby\
**Created:** [January 10, 2007, 3:40pm UTC](https://www.ruby-forum.com/t/return-on/81608 "2007-01-10T15:40:02Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [January 10, 2007, 3:40pm UTC](https://www.ruby-forum.com/t/return-on/81608/1 "2007-01-10T15:40:02Z")

</div>

Sometimes i do this:

return x if x

Anyway to code it as:

return\_on x

t.

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [January 10, 2007, 5:00pm UTC](https://www.ruby-forum.com/t/return-on/81608/2 "2007-01-10T17:00:23Z")

</div>

Trans schrieb:

> Sometimes i do this:
> 
> return x if x
> 
> Anyway to code it as:
> 
> return\_on x

It’s not very nice, but you could wrap the whole method body in a block  
and use throw/catch behind the scenes.

Regards,  
Pit

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [January 10, 2007, 5:29pm UTC](https://www.ruby-forum.com/t/return-on/81608/3 "2007-01-10T17:29:47Z")

</div>

On 1/10/07, Trans [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Sometimes i do this:
> 
> return x if x
> 
> Anyway to code it as:
> 
> return\_on x

I cannot recall I ever have used return, in this way,  
but my coding style may be 5% different than yours.  
Maybe I can improve my minimal insight.

Any good (larger) examples of this thing?

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [January 11, 2007, 6:36am UTC](https://www.ruby-forum.com/t/return-on/81608/4 "2007-01-11T06:36:40Z")

</div>

Trans wrote:

> Sometimes i do this:  
> return x if x  
> Anyway to code it as:  
> return\_on x

I’m wondering if continuations (or continuations + set\_trace\_func to  
catch the method end) could be used to achieve that, but am not a whiz  
at callcc, and am up past my bedtime (hence am not going to experiment  
w/ it).

Is there a Weirich in the house?

Devin  
(Even if it can be implemented using such hackery, I might consider that  
a smell that it’s so different, and doesn’t map to Ruby’s ways, and be  
hesitant to use such a beast in GP code.)

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [January 11, 2007, 5:54am UTC](https://www.ruby-forum.com/t/return-on/81608/5 "2007-01-11T05:54:36Z")

</div>

On 10-Jan-2007, at 20:09, Trans wrote:

> Sometimes i do this:
> 
> return x if x
> 
> Anyway to code it as:
> 
> return\_on x
> 
> t.

I do this quite often, as well! We need a macro system 😉

JS

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [January 19, 2007, 4:29pm UTC](https://www.ruby-forum.com/t/return-on/81608/6 "2007-01-19T16:29:18Z")

</div>

Simon S. wrote:

> I cannot recall I ever have used return, in this way,  
> but my coding style may be 5% different than yours.  
> Maybe I can improve my minimal insight.
> 
> Any good (larger) examples of this thing?

I don’t really have any examples that are repleat with it, but as to  
insight it’s especially convenient when caching a return value:

def x  
return\_on @cache[:x]  
# do stuff  
@cache[:x] = result\_of\_stuff  
end

this gets rid of an extraneous variable too b/c otherwise, the more  
efficient impl. is:

def x  
r = @cache[:x]  
return r if r  
# do stuff  
@cache[:x] = result\_of\_stuff  
end

funny thing i just came across a similar case for break. how would you  
DRY this up and get rid of ‘result’?

```
result = nil
files.each do
  result = require file
  break if result
end
if result
  ...

```

t.

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [January 19, 2007, 4:29pm UTC](https://www.ruby-forum.com/t/return-on/81608/7 "2007-01-19T16:29:59Z")

</div>

Trans schrieb:

> efficient impl. is:
> 
> def x  
> r = @cache[:x]  
> return r if r  
> # do stuff  
> @cache[:x] = result\_of\_stuff  
> end

You could also implement this as

def x  
@cache[:x] ||= (  
# do stuff  
result\_of\_stuff  
)  
end

> funny thing i just came across a similar case for break. how would you  
> DRY this up and get rid of ‘result’?
> 
> ```
> result = nil
> files.each do
> result = require file
> break if result
> end
> if result
> ...
> 
> ```

If “result” is only a flag whose value you don’t need later, you could  
do

if files.find { |file| require file }  
…  
end

Regards,  
Pit

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [January 19, 2007, 4:30pm UTC](https://www.ruby-forum.com/t/return-on/81608/8 "2007-01-19T16:30:24Z")

</div>

Trans wrote:  
…

> funny thing i just came across a similar case for break. how would you  
> DRY this up and get rid of ‘result’?
> 
> ```
> result = nil
> files.each do
> result = require file
> break if result
> end
> if result
> ...
> 
> ```

This only helps a little…

result = files.each do |file|  
r = require file and break r  
end

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [January 19, 2007, 4:29pm UTC](https://www.ruby-forum.com/t/return-on/81608/9 "2007-01-19T16:29:22Z")

</div>

“Trans” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> def x  
> return\_on @cache[:x]  
> # do stuff  
> @cache[:x] = result\_of\_stuff  
> end

def x  
@cache.fetch(:x) {  
@cache[:x] = result\_of\_stuff  
}  
end

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [January 19, 2007, 4:31pm UTC](https://www.ruby-forum.com/t/return-on/81608/10 "2007-01-19T16:31:11Z")

</div>

On Wed, Jan 10, 2007 at 11:39:00PM +0900, Trans wrote:

> Sometimes i do this:
> 
> return x if x
> 
> Anyway to code it as:
> 
> return\_on x
> 
> t.

This is pure unadulterated evil, however it did come up in #ruby-lang a  
while ago, so I whipped up some good old fashioned homemade evil:

def try\_return(&block)  
unless (blah = block.call).nil?  
GC.disable  
eval(“return ObjectSpace.\_id2ref(#{blah.object\_id})”, block)  
end  
ensure  
GC.enable  
end

def f  
try\_return { nil }  
try\_return { 7 }  
0  
end

p f

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [January 19, 2007, 4:31pm UTC](https://www.ruby-forum.com/t/return-on/81608/11 "2007-01-19T16:31:13Z")

</div>

Trans wrote:

> funny thing i just came across a similar case for break. how would you  
> DRY this up and get rid of ‘result’?
> 
> ```
> result = nil
> files.each do
> result = require file
> break if result
> end
> if result
> ...
> 
> ```

Well, this is much easier:

if files.any? {|file| require file }  
…

C’mon, Trans. 🙂

Devin  
(Or am I missing something obvious?)

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [January 19, 2007, 4:31pm UTC](https://www.ruby-forum.com/t/return-on/81608/12 "2007-01-19T16:31:06Z")

</div>

I have made the most hackish thing to accomplish this. It is basically  
a regexp-supporting preprocessor. I don’t recommend using it.

#! /usr/bin/ruby -w

def preprocessor\_define(\*args, &blk)

# Determine line of caller

callerLine = caller.join.match(/[0-9]+/)[0].to\_i

# Get the source of the running file

source = IO.read( **FILE** )

# Remove the callerLine line

source = source.split("\n")  
source.delete\_at(callerLine - 1)  
source = source.join("\n")

# Do the replacement

source.gsub!(\*args, &blk)

# Avoid function redefinition warnings

newName = “”  
50.times {newName \<\< (rand(26) + 65).chr }  
source.gsub!(“preprocessor\_define”, newName)

# Run the replacement

#puts source  
eval source  
exit  
end

preprocessor\_define(/return\_on (.\*)/) {|s| “return #{s[1]} unless  
#{s[1]}.nil?”}

def test  
zulu = 5  
return\_on zulu  
puts “Broken.”  
end  
test

Output: None, as ‘puts Broken’ never gets executed.

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [January 19, 2007, 4:31pm UTC](https://www.ruby-forum.com/t/return-on/81608/13 "2007-01-19T16:31:24Z")

</div>

On 1/13/07, Trans [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > >
> 
> def x  
> return r if r  
> break if result  
> end  
> if result  
> …
> 
> t.

Much like returning in ActiveSupport:  
[http://weblog.jamisbuck.org/2006/10/27/mining-activesupport-object-returning](http://weblog.jamisbuck.org/2006/10/27/mining-activesupport-object-returning)

- rob

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [January 19, 2007, 4:31pm UTC](https://www.ruby-forum.com/t/return-on/81608/14 "2007-01-19T16:31:31Z")

</div>

Daniel F. wrote:

> source = IO.read( **FILE** )  
> newName = “”  
> #{s[1]}.nil?"}
> 
> def test  
> zulu = 5  
> return\_on zulu  
> puts “Broken.”  
> end  
> test
> 
> Output: None, as ‘puts Broken’ never gets executed.

Holy Smokes! The Man goes out of his way!!! return\_on brother,  
return\_on! 🙂

T.

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [January 19, 2007, 4:31pm UTC](https://www.ruby-forum.com/t/return-on/81608/15 "2007-01-19T16:31:38Z")

</div>

On Jan 14, 1:34 am, “Trans” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> def x  
> return\_on @cache[:x]  
> # do stuff  
> @cache[:x] = result\_of\_stuff  
> end

def x  
@cache[:x] ||= begin  
# do stuff  
end  
end

Gavin

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [January 19, 2007, 4:31pm UTC](https://www.ruby-forum.com/t/return-on/81608/16 "2007-01-19T16:31:13Z")

</div>

Pit C. wrote:

> > this gets rid of an extraneous variable too b/c otherwise, the more
> 
> def x  
> @cache[:x] ||= (  
> # do stuff  
> result\_of\_stuff  
> )  
> end

That pretty good. I rarely use ( ) as local encapsulator and probably  
should consider it more often. I’m generally partial to less indention  
when I can get it though.

Thanks,  
T.

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [January 19, 2007, 4:31pm UTC](https://www.ruby-forum.com/t/return-on/81608/17 "2007-01-19T16:31:51Z")

</div>

Rob S. wrote:

> Much like returning in ActiveSupport:  
> [Buckblog: Mining ActiveSupport: Object#returning](http://weblog.jamisbuck.org/2006/10/27/mining-activesupport-object-returning)

Yes, kind-a. However there’s been a lot of focus on the more common  
method cache usecase, but return\_on could also be used multple times  
too:

def x(a,b)  
return\_on foo(a)  
return\_on foo(b)  
,  
end

T.

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [September 25, 2007, 10:30pm UTC](https://www.ruby-forum.com/t/return-on/81608/18 "2007-09-25T22:30:18Z")

</div>

Joel VanderWerf wrote:

> This only helps a little…
> 
> result = files.each do |file|  
> r = require file and break r  
> end

…and the use of ‘and’. these are the crafty of ruby 🙂

def x  
r = @cache[:x] and return r  
…  
end

pretty good, still a little unDRY, but pretty good.

t.

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [September 25, 2007, 10:36pm UTC](https://www.ruby-forum.com/t/return-on/81608/19 "2007-09-25T22:36:03Z")

</div>

Joel VanderWerf wrote:

> > @cache[:x] ||= begin  
> > )
> 
> CACHING  
> “oof”  
> “oof”

It doesn’t work correctly.

def reverse x  
@cache[:x] ||= (  
puts “CACHING”  
x.reverse  
)  
end

@cache = {}

p reverse(“foo”)  
p reverse(“what?”)  
p reverse(“And this is cached, you think???”)

— output -----  
CACHING  
“oof”  
“oof”  
“oof”

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [September 25, 2007, 10:42pm UTC](https://www.ruby-forum.com/t/return-on/81608/20 "2007-09-25T22:42:10Z")

</div>

Devin M. wrote:

> > ```
> > ...
> > 
> > ```
> 
> Well, this is much easier:
> 
> if files.any? {|file| require file }  
> …

Cool, I forget that #any? will break after the first true encounter. I  
used #find as pit suggested (which can return any value actually). But,  
that wasn’t really my point. Such a solution breaks the analogy to the  
original return case --whihc can’t be DRYed-up that way.

T.

[Next page](https://www.ruby-forum.com/t/return-on/81608.md?page=2)
