On Dec 28, 2003, at 15:29, Harry Ohlsen wrote:

> Yesterday, I wanted to get the output from "ls -l some_file" and pull 
> out just the file size and the file name. As I start writing this, I 
> realise, of course, that I'd have been better off just using the 
> File#size method, but I still think the issue I hit is interesting.
>
> I did something like:
>
>   listing = `ls -l #{path}`.split(/\s+/)

listing = `ls -l #{path}`.split
is enough.

>
> so I had the fields from "ls" as an array of strings.
>
> What I then wanted to do was pull out items 4 and 8 (the size and 
> filename) into a new two-element array. I half expected to be able to 
> do that via:
>
>   data = listing[[4, 8]]

data = listing.select(4,8)
or for older Ruby
data = listing.indices(4,8)

If you want to extract a file name, I would use index -1 instead of 8 
to get the last element. "ls -l" produces more fields for device nodes 
(in this case index 4 refers to the major device number followed by a 
comma).

>
> but that doesn't work.
>
> Someone will probably tell me there's an easy way to achieve this.  
> Please feel free to do so.
>
> However, I think it would be nice if the indexing operator for Array, 
> and maybe Hash, or perhaps anything Enumerable, would accept an array 
> as parameter and return another array containing the corresponding 
> items, to allow for non-contiguous selections.
>
> I haven't really thought it through in the case of hashes. Perhaps in 
> that case, one would want to get back a new hash, although I can't 
> imagine that.
>
> Anyway, just a thought.
>
> Cheers,
>
> Harry O.
>
>
>

Sincerely,
Gennady Bystritsky