# Discovering the number of return arguments requested

**URL:** <https://www.ruby-forum.com/t/discovering-the-number-of-return-arguments-requested/79968>\
**Category:** Ruby\
**Created:** [December 20, 2006, 7:18pm UTC](https://www.ruby-forum.com/t/discovering-the-number-of-return-arguments-requested/79968 "2006-12-20T19:18:38Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![bpauly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ecd19e/32.png) [@bpauly](https://www.ruby-forum.com/u/bpauly)\
**Post date:** [December 20, 2006, 7:18pm UTC](https://www.ruby-forum.com/t/discovering-the-number-of-return-arguments-requested/79968/1 "2006-12-20T19:18:38Z")

</div>

Is it possible ( and I think not ) that you can discover if any return  
arguments are requested when a method is called.

Eg:

def foo  
if nargout == 1  
“bar”  
end  
end

a = foo # nargout == 1  
foo # nargout == 0

This would be useful in certain patterns where you only want to  
create a return value and consequently a new object if it is  
actually required.

---

<div class="post-metadata">

**Author:** ![bpauly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ecd19e/32.png) [@bpauly](https://www.ruby-forum.com/u/bpauly)\
**Post date:** [December 21, 2006, 1:55am UTC](https://www.ruby-forum.com/t/discovering-the-number-of-return-arguments-requested/79968/2 "2006-12-21T01:55:04Z")

</div>

Brad P. wrote:

> a = foo # nargout == 1  
> foo # nargout == 0
> 
> This would be useful in certain patterns where you only want to  
> create a return value and consequently a new object if it is  
> actually required.

One option is yield:

def foo  
yield “bar” if block\_given?  
end

a = nil

foo {|x| a=x}  
#or:  
#foo {|a|} # this doesn’t work in 1.9 IIRC
