Professional Web Applications Themes

An addition to Array (or Enumerable)? - Ruby

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+/) 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 ...

  1. #1

    Default An addition to Array (or Enumerable)?

    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+/)

    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]]

    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.



    Harry Guest

  2. #2

    Default Re: An addition to Array (or Enumerable)?


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

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

    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).
     

    Sincerely,
    Gennady Bystritsky




    Gennady Guest

  3. #3

    Default Re: An addition to Array (or Enumerable)?

    > What I then wanted to do was pull out items 4 and 8 (the size and 
    irb(main):001:0> x = ['A','B','C','D'] => ["A", "B", "C", "D"]
    irb(main):002:0> y = x[0],x[1]
    => ["A", "B"]
    irb(main):003:0> p y
    ["A", "B"]
    => nil
    Tim Guest

  4. #4

    Default Re: An addition to Array (or Enumerable)?


    On Dec 28, 2003, at 16:00, Gennady wrote:
     
    >
    > listing = `ls -l #{path}`.split
    > is enough.

    >
    > data = listing.select(4,8)[/ref]

    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"

     
    >
    > Sincerely,
    > Gennady Bystritsky
    >
    >
    >
    >[/ref]

    Sincerely,
    Gennady Bystritsky




    Gennady Guest

  5. #5

    Default Re: An addition to Array (or Enumerable)?

    [Gennady:] 
    >
    > data = listing.select(4,8)
    > or for older Ruby
    > data = listing.indices(4,8)[/ref]

    Enumerable#select is an alias for #find_all.

    #values_at is the 1.8 version of #indices (I miss that method name :).

    Gavin





    Gavin Guest

  6. #6

    Default Re: An addition to Array (or Enumerable)?

    Gennady wrote:
     

    Thanks!

    Obviously, I should have tried

    puts [].methods.sort.join("\n")

    in irb, to see what methods are currently available for arrays. I'm pretty sure I would have guessed that values_at was what I was looking for!

    Sadly, I just use ri (and rj) and neither of them listed values_at. Somehow, I didn't notice Array#select in either of them; either that or I misread the doentation.

    Note: I still think that indexing an array with an array of indices is such an obvious thing to try that it should be there, but for the time being, I'll just spend some time reading through the updated doentation once the new ri comes out.

    Cheers,

    Harry O.



    Harry Guest

  7. #7

    Default Re: An addition to Array (or Enumerable)?

    Tim Hunter wrote:
     

    Sure, that would work.

    Of course, it doesn't look so neat when the name of the array is more than one character, and the list of indices is longer.

    It feels just a little repetitious (again, not for a couple of indices). It could also be messy if the set of indices you wanted was in a variable, rather than hard-coded as in the specific case I gave as an example.

    At this point, it's an academic exercise. As I said in my original post, in my real-world case, I really should have just used File#size to get what I wanted, but it's so much harder to code after a couple of bottles of Christmas cheer :-).

    H.


    Harry Guest

  8. #8

    Default Re: An addition to Array (or Enumerable)?

    > Gennady wrote: 
    >
    > Thanks!
    >
    > Obviously, I should have tried
    >
    > puts [].methods.sort.join("\n")[/ref]

    The "join" method is redundant unless you have modified the output
    separator :)
     

    I'm pretty sure that ri 1.8b contains values_at. I'm surprised about rj,
    since I thought it was based on 1.8b. Anyway, rj is becoming blissfully
    redundant as Dave improves ri. The pace of that improvement is
    astonishing (see recent Ruby ChangeLog entries).

    Anyway, you may want to check rdoc.sf.net to see if your 'ri' is up to
    date with the latest stable release (not the new experimental stuff).
     

    Not in my opinion, actually. It makes sense once I see it, but the code
    you presented is unappealing to me: too many [[ and ]] :). But if that's
    what I'd seen first in Ruby, I probably wouldn't have thought twice.

    Cheers,
    Gavin




    Gavin Guest

  9. #9

    Default Re: An addition to Array (or Enumerable)?


    On Dec 28, 2003, at 18:15, Gennady wrote:
     

    It changed... :)

    The new ri has it right:


    dave[ruby/ruby 17:06:31] ri -f bs Array.select
    ----------------------------------------------------------- Array#select
    array.select {|i| block } -> an_array
    ------------------------------------------------------------------------
    Invokes the block passing in successive elements from arr,
    returning an array containing those elements for which the block
    returns a true value (equivalent to Enumerable#select).

    a = %w{ a b c d e f }
    a.select(1, 3, 5) #=> ["b", "d", "f"]
    a.select(1, 3, 5, 7) #=> ["b", "d", "f", nil]
    a.select(-1, -3, -5, -7) #=> ["f", "d", "b", nil]
    a.select {|v| v =~ /[aeiou]/} #=> ["a", "e"]


    Cheers

    Dave



    Dave Guest

  10. #10

    Default Re: An addition to Array (or Enumerable)?

    Gavin Sinclair wrote:
     
    >
    >
    > The "join" method is redundant unless you have modified the output
    > separator :)[/ref]

    Thanks.

    That's the only problem with POLS ... when I try something and it just works (as it so often does), I keep that idiom in my head forever, when there's almost always a simpler way :-).

    I think the only solution is to try to read more of other people's Ruby code, in which case I'd notice things like that and think "I wonder why they don't have join("\n") there?".
     

    I downloaded the latest rj from CVS just recently, but that's at home. I'll make sure to check when I get a chance. Maybe it's there and I was just more sheets to the wind than I remember!
     

    Sadly, the web site is down for maintenance. I'll check back sometime soon, though.
     
    >
    >
    > Not in my opinion, actually. It makes sense once I see it, but the code
    > you presented is unappealing to me: too many [[ and ]] :). But if that's
    > what I'd seen first in Ruby, I probably wouldn't have thought twice.[/ref]

    I guess that's similar to my previous comment about remembering the first thing you see that works ... perhaps this is just a further ogue of duck typing ... imprinting :-).

    Cheers,

    H.



    Harry Guest

  11. #11

    Default Re: An addition to Array (or Enumerable)?

    On Mon, 2003-12-29 at 02:51, Dave Thomas wrote: 
    >
    > It changed... :)
    >
    > The new ri has it right:
    >
    >
    > dave[ruby/ruby 17:06:31] ri -f bs Array.select
    > ----------------------------------------------------------- Array#select
    > array.select {|i| block } -> an_array
    > ------------------------------------------------------------------------
    > Invokes the block passing in successive elements from arr,
    > returning an array containing those elements for which the block
    > returns a true value (equivalent to Enumerable#select).
    >[/ref]

    The text is ok.
     

    But these examples don't work in 1.9.0
     

    This one is ok again.

    -- Thomas Uehlinger



    Thomas Guest

  12. #12

    Default Re: An addition to Array (or Enumerable)?


    On Dec 29, 2003, at 4:59, Thomas Uehlinger wrote:
     

    Thanks! My fault for being lazy when cut'n'pasting from values_at.
    It'll be fixed next time I commit.

    Cheers

    Dave


    Dave Guest

Similar Threads

  1. Support for arbitrary iterators in Enumerable?
    By Pierre-Charles David in forum Ruby
    Replies: 2
    Last Post: November 20th, 02:34 PM
  2. Enumerable#inject is surprising me...
    By Nathaniel Talbott in forum Ruby
    Replies: 15
    Last Post: October 20th, 11:53 AM
  3. Why isn't Enumerable in StringIO?
    By Jason Creighton in forum Ruby
    Replies: 1
    Last Post: July 26th, 05:14 AM
  4. A different Version of Enumerable#inject
    By Mauricio Fernández in forum Ruby
    Replies: 6
    Last Post: July 25th, 02:06 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139