# Closures

**URL:** <https://www.ruby-forum.com/t/closures/179558>\
**Category:** Ruby\
**Created:** [November 28, 2009, 7:10am UTC](https://www.ruby-forum.com/t/closures/179558 "2009-11-28T07:10:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Burlsm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/6f9a4e/32.png) [@Burlsm](https://www.ruby-forum.com/u/Burlsm)\
**Post date:** [November 28, 2009, 7:10am UTC](https://www.ruby-forum.com/t/closures/179558/1 "2009-11-28T07:10:25Z")

</div>

hi

i’m having a little trouble with closures and i’d like to know what  
the equivalent code for the canonical make-adder procedure would in  
ruby,

in scheme it would be like

(define (make-adder n)  
(lambda (x) (+ x n))

thanks in advance

---

<div class="post-metadata">

**Author:** ![Burlsm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/6f9a4e/32.png) [@Burlsm](https://www.ruby-forum.com/u/Burlsm)\
**Post date:** [November 28, 2009, 7:18am UTC](https://www.ruby-forum.com/t/closures/179558/2 "2009-11-28T07:18:36Z")

</div>

Hi Burlsm,

I think the following is what you want:

def make\_adder(n)  
lambda { |x| x + n }  
end

or the following new lambda syntax if you are using Ruby 1.9 or later:

def make\_adder(n)  
-\>(x) { x + n }  
end

Hope that helps.

allenlooplee

---

<div class="post-metadata">

**Author:** ![Burlsm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/6f9a4e/32.png) [@Burlsm](https://www.ruby-forum.com/u/Burlsm)\
**Post date:** [November 28, 2009, 8:50am UTC](https://www.ruby-forum.com/t/closures/179558/3 "2009-11-28T08:50:30Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

On a sidenote, Ruby 1.9 also has this cool function:

f = -\>(a,b){ a + b }  
c = f.curry  
b = c[1]

where b is a partially applied function, with a bound to 1.

b[6] #=\> a + b = 7

then evaluates the whole function.

Regards,  
Florian

P.S.: I still think it should be called #schönfinkeln.

On Nov 28, 2009, at 2:47 AM, Allen L. wrote:

> def make\_adder(n)
> 
> > hi  
> > thanks in advance

* * *

Florian G.

smtp: removed\_email\_address@domain.invalid  
jabber: removed\_email\_address@domain.invalid  
gpg: 533148E2

-----BEGIN PGP SIGNATURE-----  
Version: GnuPG/MacGPG2 v2.0.12 (Darwin)

iEYEARECAAYFAksQ1iEACgkQyLKU2FMxSOIX+QCeN3vFLd0m1dIkxo+limomEs1s  
1KYAniQhUT/DlL73ON63DSX/FrVTVIgN  
=irHI  
-----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![Burlsm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/6f9a4e/32.png) [@Burlsm](https://www.ruby-forum.com/u/Burlsm)\
**Post date:** [November 29, 2009, 5:45am UTC](https://www.ruby-forum.com/t/closures/179558/4 "2009-11-29T05:45:24Z")

</div>

Burlsm wrote:

> hi
> 
> i’m having a little trouble with closures and i’d like to know what  
> the equivalent code for the canonical make-adder procedure would in  
> ruby,
> 
> in scheme it would be like
> 
> (define (make-adder n)  
> (lambda (x) (+ x n))
> 
> thanks in advance

I noticed this link describing some scheme style closures in ruby  
[http://innig.net/software/ruby/closures-in-ruby.rb](http://innig.net/software/ruby/closures-in-ruby.rb)  
in the ruby talk faq the other day.  
-r

---

<div class="post-metadata">

**Author:** ![Burlsm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/6f9a4e/32.png) [@Burlsm](https://www.ruby-forum.com/u/Burlsm)\
**Post date:** [December 7, 2009, 10:14am UTC](https://www.ruby-forum.com/t/closures/179558/5 "2009-12-07T10:14:26Z")

</div>

2009/11/28 Florian G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> b[6] #=\> a + b = 7
> 
> then evaluates the whole function.

The cool thing is that the result of f.curry can be used to curry the  
function as well as to evaluate it - depending on the number of  
arguments:

irb(main):001:0\> f = -\>(a,b){ a + b }  
=\> #\<Proc:0x101731c0@(irb):1 (lambda)\>  
irb(main):002:0\> f[1,2]  
=\> 3  
irb(main):003:0\> c = f.curry  
=\> #Proc:0x10169d28  
irb(main):004:0\> c[1,2]  
=\> 3  
irb(main):005:0\> b = c[1]  
=\> #Proc:0x10159cc0  
irb(main):006:0\> b[2]  
=\> 3  
irb(main):007:0\>

> P.S.: I still think it should be called #schönfinkeln.

LOL

Kind regards

robert
