From: Robert Klemme Date: 2004-08-07T18:31:23+09:00 Subject: Re: Defining a new function by composition "Edgardo Hames" schrieb im Newsbeitrag news:478c16ae0408061345100e8692@mail.gmail.com... > On Sat, 7 Aug 2004 04:46:25 +0900, Robert Klemme wrote: > > > > "Edgardo Hames" schrieb im Newsbeitrag > > news:478c16ae0408061209424c2032@mail.gmail.com... > > > > > > I would like to add a method to the Array class, say Array#pop! which > > > is actually split!(-1). But then, I can think of a more general > > > problem: define a new function by composition of two or more > > > functions. > > > > How about > > > > class Array > > def pop!() split!(-1) end > > end > > I imagined this. But, I thought you were going to surprise me with a > weird (or unimagined) way to use alias ;-) :-) Not possible because of the argument. Here's another solution - even more functional, although I still think this looks a bit more elegant in functional languages: def concat3(*fun) lambda {|*a| fun.inject(a) {|val,f| f.call(*val)} } end >> foo = lambda {|x| "<#{x}>"} => # >> bar = lambda {|x| "[#{x}]"} => # >> xx3 = concat3 bar, foo => # >> xx3.call 100 => "<[100]>" >> xx3[ 100 ] => "<[100]>" As you can see, there are plenty ways to do this. And you can even use Ruby as a functional language, although I'd say best use is made of it if you use it OO. Regards robert