From: dohzya Date: 2007-08-06T19:32:37+09:00 Subject: Re: Dynamic method call Le lundi 06 ao�t 2007 � 08:47 +0900, Gregory Brown a �crit : > On 8/4/07, Bas van Gils wrote: > > On Thu, Aug 02, 2007 at 04:48:30AM +0900, dohzya wrote: > > > > > > Do you want something like this : > > > --- > > > def sendif( args, &bloc ) > > > if args[:if] > > > send args[:then].shift, *args[:then], &bloc > > > else > > > send args[:else].shift, *args[:else], &bloc if args[:else] > > > end > > > end > > > > > > sendif :if => 1 < 2, :then => [:p, "gagne"], :else => [:puts, "perdu"] > > > --- > > > ? > > > > Damn, that's elegant! > > I dig it too, though I like better: > > sendif 1 < 2, :then => [:p, "gagne"], :else => [:puts, "perdue"] > > just because it kills the redundant if. > in ruby 1.9 : --- sendif 1 < 2, then: [:p, "gagne"], else: [:puts, "perdue"] --- as elegant as Objective-C ! if I am very evil : --- def then &b ; Proc.new &b ; end def else &b ; Proc.new &b ; end def sendif test, then, else test ? then.call : else.call end sendif 1 < 2, then {p "gagne"}, else {puts "perdue"} --- but it's too expensive for a simple function... -- Etienne Vallette d'Osia