From: mark sparshatt Date: 2004-08-07T06:27:11+09:00 Subject: Re: Defining a new function by composition Edgardo Hames wrote: >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 ;-) > > > Though this won't do what you want since there isn't a method split! for array. I think what you actually want is something like class Array def pop! slice!(-1, 1) end end This discussion did give me an idea for a curry method, that let's you predefine the parameters for a method. class Class def curry(newmethod, oldmethod, stored_params) send(:define_method, newmethod) do |*args| x = stored_params x += args if args send(oldmethod, *x) end end end then you can define pop! with class Array curry(:pop!, :slice!, -1, 1) end then x = [1,2,3] p x.slice! #=> 3 p x #=> [1,2] -- Mark Sparshatt