# Function inside a function

**URL:** <https://www.ruby-forum.com/t/function-inside-a-function/158840>\
**Category:** Ruby\
**Created:** [January 28, 2009, 4:51pm UTC](https://www.ruby-forum.com/t/function-inside-a-function/158840 "2009-01-28T16:51:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![leveille](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a88e57/32.png) [@leveille](https://www.ruby-forum.com/u/leveille)\
**Post date:** [January 28, 2009, 4:51pm UTC](https://www.ruby-forum.com/t/function-inside-a-function/158840/1 "2009-01-28T16:51:45Z")

</div>

I have a question about the timing of function evaluations in this case:

def function\_a(x = 1)  
x\*\*2 + x  
end

def function\_b(any\_function)  
y = 4 + 5 #this would actually be more complex  
function\_a(y)  
end

puts function\_b(function\_a)

I found that function\_a(x = 1) is evaluated first, then  
function\_b(function\_a). I found that 'def function\_a(x = nil) returns an  
error. Why does function\_a need to be evaluated before it is called as  
an argument in function\_b?

PS - the reason I am writing things this way is to make function\_b  
accept any function (within reason).

---

<div class="post-metadata">

**Author:** ![leveille](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a88e57/32.png) [@leveille](https://www.ruby-forum.com/u/leveille)\
**Post date:** [January 28, 2009, 5:35pm UTC](https://www.ruby-forum.com/t/function-inside-a-function/158840/2 "2009-01-28T17:35:04Z")

</div>

On Wed, Jan 28, 2009 at 4:49 PM, Jason L.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> puts function\_b(function\_a)
> 
> I found that function\_a(x = 1) is evaluated first, then  
> function\_b(function\_a). I found that 'def function\_a(x = nil) returns an  
> error. Why does function\_a need to be evaluated before it is called as  
> an argument in function\_b?

The problem here is that when you write function\_a, this is actually  
_calling_ function\_a,which as you are not supplying any parameter uses  
the default value of 1 you defined.

> PS - the reason I am writing things this way is to make function\_b  
> accept any function (within reason).

For sure there are many ways to achieve this, but a common way is to  
use blocks and lambdas:

function\_a = lambda {|x| x\*\*2 + x}

def function\_b  
y = 4 + 5 #this would actually be more complex  
yield y  
end

or more explicitly:

def function\_b (&blk)  
y = 4 + 5  
blk.call(y) # or blk[y]  
end

puts function\_b(&function\_a)

Hope this helps,

Jesus.

---

<div class="post-metadata">

**Author:** ![leveille](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a88e57/32.png) [@leveille](https://www.ruby-forum.com/u/leveille)\
**Post date:** [January 28, 2009, 5:45pm UTC](https://www.ruby-forum.com/t/function-inside-a-function/158840/3 "2009-01-28T17:45:40Z")

</div>

In ruby, function\_a is a method call (with no arguments), and as a  
call, it gets evaluated to whatever that function returns. If you  
wanted to grab the function as a “pure function”, you’d use lambdas or  
Procs like Jesús pointed.

In other words:

function\_a = Proc.new {|x| …do something with x… }

returns a functional object. You can then call the method with  
function\_a.call (aliased [])

see [class Proc - RDoc Documentation](http://www.ruby-doc.org/core/classes/Proc.html) for details on Proc  
objects

Greetings

On Jan 28, 9:49 am, Jason L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![leveille](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a88e57/32.png) [@leveille](https://www.ruby-forum.com/u/leveille)\
**Post date:** [January 28, 2009, 8:04pm UTC](https://www.ruby-forum.com/t/function-inside-a-function/158840/4 "2009-01-28T20:04:11Z")

</div>

Ruben M. wrote:

> In ruby, function\_a is a method call (with no arguments), and as a  
> call, it gets evaluated to whatever that function returns. If you  
> wanted to grab the function as a “pure function”, you’d use lambdas or  
> Procs like Jesï¿½s pointed.
> 
> In other words:
> 
> function\_a = Proc.new {|x| …do something with x… }
> 
> returns a functional object. You can then call the method with  
> function\_a.call (aliased [])

Or, if you really want to do this with def’d methods, you can use

obj.method(:function\_a)

which will turn an existing method (bound to the instance ‘obj’) into a  
Proc-like thing that you can pass around and call later.

def function\_a(x = 1)  
x\*\*2 + x  
end

def function\_b(any\_function)  
y = 4 + 5  
any\_function.call(y) # or: any\_function[y]  
end

puts function\_b(method(:function\_a))

---

<div class="post-metadata">

**Author:** ![leveille](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a88e57/32.png) [@leveille](https://www.ruby-forum.com/u/leveille)\
**Post date:** [January 28, 2009, 6:45pm UTC](https://www.ruby-forum.com/t/function-inside-a-function/158840/5 "2009-01-28T18:45:44Z")

</div>

Perfect. Thank you!
