From: Daniel Finnie Date: 2008-05-19T08:39:55+09:00 Subject: Re: How to wrap either forkoff or map depending on a config variable? ------=_Part_17984_13815805.1211153997102 Content-Type: text/plain; charset=WINDOWS-1252 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Hi, To pass a block as a block to another function, put an ampersand before the variable name: map &block forkoff &block If you are interested in performance, know that using the &block notation for blocks is slower than using the block_given? and yield semantics. Dan On Sun, May 18, 2008 at 4:03 PM, Shot (Piotr Szotkowski) wrote: > Hello, good people of ruby-talk. > > I'm trying to write an Enumerable#parallel method that, depending on > a config option (retrieved inside it) would end up being an alias for > either Enumerable#map or Enumerable#forkoff. > > (I know calling enum.forkoff(1) should equal enum.map, but I need > the wrapper to call #map, as forkoff doesn't work under JRuby.) > > I'm quite new to the idea of blocks/closures, and while I love using > them, I'm still not sure how to write such a method. What I'm looking > for is something like this: > > module Enumerable > def parallel &block > if Config.processes =3D=3D 1 > # call map with the passed block > else > # call forkoff with the passed block > end > end > end > > Ideally, the call to forkoff would pass Config.processes as its > first param =96 i.e., calling enum.parallel {=85} would mean calling > enum.forkoff(Config.processes) {=85} > > -- Shot > -- > An Emacs reference mug is what I want. It would hold > ten gallons of coffee. -- Steve VanDevender, asr > ------=_Part_17984_13815805.1211153997102--