[#4766] Wiki — "Glen Stampoultzis" <trinexus@...>

21 messages 2000/09/04
[#4768] RE: Wiki — "NAKAMURA, Hiroshi" <nahi@...> 2000/09/04

Hi, Glen,

[#4783] Re: Wiki — Masatoshi SEKI <m_seki@...> 2000/09/04

[#4785] Re: Wiki — "NAKAMURA, Hiroshi" <nakahiro@...> 2000/09/05

Howdy,

[#4883] Re-binding a block — Dave Thomas <Dave@...>

16 messages 2000/09/12

[#4930] Perl 6 rumblings -- RFC 225 (v1) Data: Superpositions — Conrad Schneiker <schneik@...>

Hi,

11 messages 2000/09/15

[#4936] Ruby Book Eng. translation editor's questions — Jon Babcock <jon@...>

20 messages 2000/09/16

[#5045] Proposal: Add constants to Math — Robert Feldt <feldt@...>

15 messages 2000/09/21

[#5077] Crazy idea? infix method calls — hal9000@...

This is a generalization of the "in" operator idea which I

17 messages 2000/09/22

[#5157] Compile Problem with 1.6.1 — Scott Billings <aerogems@...>

When I try to compile Ruby 1.6.1, I get the following error:

15 messages 2000/09/27

[ruby-talk:4761] Re: Piping in Ruby?

From: Dave Thomas <Dave@...>
Date: 2000-09-03 12:53:05 UTC
List: ruby-talk #4761
Stephen White <steve@deaf.org> writes:

> > Could you give an example of how you'd use this in Ruby?
> 
> Another example (just tried to do this now) is filtering output:
> 
>   list.each {|i| puts i } | grep "fred"
>                           ^^^^^^^^^^^^^
...
>   a = []
>   list.scan(pattern) {|i| a << convert(i) }
>   a.each {|i| code }
> 

The trick in both of these cases is to keep your data in array format
until the last minute. In the first case, the 'puts' will have written 
the data out, so it is too late to access it directly. You _could_ do
it the way shell do, and create sub-processes and pipes, but why not
just do:

   list = %w(bert fred andy fredeerick)
   list.grep(/fred/).each {|i| puts i}

or use a side-effect:

   list.grep(/fred/) { |i| puts i }

In the second case, the trick is the collect method:

   list.grep(/fred/).collect {|i| convert(i)} .each {|i| code... }

Collect takes an enumerable collection and returns a new array where
each element is some mapping of the original collection. You can then
further process that resulting array. In the example above I've used
'each', just as you did in your original. In reality, you can keep
expanding this array pipeline indefinitely

   list.collect {|i| doThing1(i) } .
        collect {|i| doThing2(i) } .
        collect {|i| doThing3(i) } .
        collect {|i| doThing4(i) } .
        collect {|i| doThing5(i) } ... etc


Regards


Dave
  

In This Thread