From: Robert Dober Date: 2007-08-06T19:44:40+09:00 Subject: Re: Dynamic method call On 8/6/07, dohzya wrote: > 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... let us make it cheaper then def then &b; b.call end def else &b; b.call end sendif 1 < 2, then { p "gagnee"}, else { puts "perdu"} My wife made me change the output ;) Cheers Robert -- [...] as simple as possible, but no simpler. -- Attributed to Albert Einstein