On Dec 28, 2003, at 16:00, Gennady wrote: > > 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) Oops, just checked that in final Ruby 1.8.1 it should be data = listing.values_at(4,8) Strange, ri 1.8b claims that Array#select([anIndex]*) may be used, however I got this error: irb(main):001:0> [1,2].select(1) ArgumentError: wrong number arguments (1 for 0) from (irb):1:in `select' from (irb):1 irb(main):002:0> VERSION => "1.8.1" > 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 > > > > Sincerely, Gennady Bystritsky