Professional Web Applications Themes

principle of most suprise - Ruby

gah, ruby is doing it to me again: logline=String.new("+ 30 Jan 12:20:09 [3988] addr: x.x.x.x") tda=Array[(logline.split(/\[\d+\]/))] tda.first is it too much to assume that the string is split using [x] as a demliter and that tda.first should return "+ 30 Jan 12:20:09"? i'm getting: + 30 Jan 12:20:09 addr: x.x.x.x i seem to be getting hung up on the simplest things. i'm using 'ruby in a nutshell' and 'programming ruby' as my references. -- http://home.cogeco.ca/~tsummerfelt1...

  1. #1

    Default principle of most suprise

    gah, ruby is doing it to me again:

    logline=String.new("+ 30 Jan 12:20:09 [3988] addr: x.x.x.x")
    tda=Array[(logline.split(/\[\d+\]/))]
    tda.first

    is it too much to assume that the string is split using [x] as a demliter
    and that tda.first should return "+ 30 Jan 12:20:09"?

    i'm getting: + 30 Jan 12:20:09 addr: x.x.x.x

    i seem to be getting hung up on the simplest things. i'm using
    'ruby in a nutshell' and 'programming ruby' as my references.


    --
    http://home.cogeco.ca/~tsummerfelt1
    tony Guest

  2. #2

    Default Re: principle of most suprise

    tony summerfelt wrote: 

    tda.first.first should work here.

    split() returns an array, and you pack it into another array.
    To fix your code, do:

    tda = logline.split(/\[\d+\]/)
    tda.first

     



    Gennady Guest

  3. #3

    Default Re: principle of most suprise

    tony summerfelt wrote: 
    tda=logline.split(/\[\d+\]/) 

    Now tda.first returns "+ 30 Jan 12:20:09 ". That help?



    Joel Guest

  4. #4

    Default Re: principle of most suprise

    On Fri, Jan 30, 2004 at 08:25:08PM +0000, tony summerfelt wrote: 

    I believe the problem here is that split RETURNs an array. You're creating a
    new array out of what is already an array, and therefore getting a new
    array containing one element, which is an array.
    Just do

    tda = logline.split(/.../)


    -Mark
    Mark Guest

  5. #5

    Default Re: principle of most suprise

    tony summerfelt wrote: 
    Try this

    tda = logline.split(/\[\d+\]/)

    split will always return an Array (see "Ruby in a nutshell, p. 54).
    You do not have to force this explicitly by using the Array constructor.
    And if you want to do so, you have to use '()' not '[]' :-))

    Cheers,

    <maik/>

    Maik Guest

  6. #6

    Default Re: principle of most suprise

    Dave Lee wrote: 
    >
    >
    > another way of writing this code, which you may find more explicit if you
    > only use tda.first is to use MatchData#pre_match
    >
    > logline = "+ 30 Jan 12:20:09 [3988] addr: x.x.x.x"
    > marker = /\[\d+\]/
    > marker.match(logline).pre_match # equivalent to tda.first from above[/ref]

    Or using the lookahead contruct, ?=, you can do this:

    logline = "+ 30 Jan 12:20:09 [3988] addr: x.x.x.x"
    logline[/^.*(?=\[\d+\])/]



    Joel Guest

  7. #7

    Default Re: principle of most suprise

    On Fri, 30 Jan 2004 at 21:24 GMT, Maik Schmidt <de> wrote:
     

    i read that a number of times before i posted...that's why i was stumped
     

    i don't know what i was thinking...you'd never know it, but i'm a better
    programmer than that :/
     

    i want to thank everyone for the polite replies :) it must have been hard
    restraining yourselves :)


    --
    http://home.cogeco.ca/~tsummerfelt1
    tony Guest

  8. #8

    Default Re: principle of most suprise


    "tony summerfelt" <com> schrieb im Newsbeitrag
    news:NbeTb.1048$cgocable.net... 
    wrote: 
    >
    > i read that a number of times before i posted...that's why i was stumped
    > [/ref]
    constructor. 
    >
    > i want to thank everyone for the polite replies :) it must have been[/ref]
    hard 

    Just to keep improving: change

    logline=String.new("+ 30 Jan 12:20:09 [3988] addr: x.x.x.x")

    to

    logline="+ 30 Jan 12:20:09 [3988] addr: x.x.x.x"

    which saves you a superfluous object creation. :-)

    To make results look a bit nicer you could also do

    result = logline.split(/\s*\[\d+\]\s*/).first

    to make the regexp eat up all white space that surrounds your split mark.

    Regards

    robert

    Robert Guest

  9. #9

    Default Re: principle of most suprise

    On Mon, 02 Feb 2004 at 09:13 GMT, Robert Klemme <net> wrote:
     
     

    yup, thought of that one already :)
     

    i just needed to get the string in usable format, for testing the age.

    --
    http://home.cogeco.ca/~tsummerfelt1
    tony Guest

  10. #10

    Default Re: principle of most suprise

    tony summerfelt <com> writes:
     

    split already returns an Array - no need to create one:

    tda= logline.split(/\[\d+\]/)
    tda.first

    => "+ 30 Jan 12:20:09 "

    (what you did was 'obtain the array generated by split and stuff it as the
    first element into a new array', thus getting
    [["+ 30 Jan 12:20:09 ", "addr: x.x.x.x"]])

    HTH & kind regards
    frank

    --
    Frank Schmitt
    quattro research GmbH
    e-mail: schmitt NO at SPAM quattro-research !! dot com
    Frank Guest

  11. #11

    Default Re: principle of most suprise

    Joel VanderWerf <Berkeley.EDU> wrote in message news:<berkeley.edu>... 
    > >
    > >
    > > another way of writing this code, which you may find more explicit if you
    > > only use tda.first is to use MatchData#pre_match
    > >
    > > logline = "+ 30 Jan 12:20:09 [3988] addr: x.x.x.x"
    > > marker = /\[\d+\]/
    > > marker.match(logline).pre_match # equivalent to tda.first from above[/ref]
    >
    > Or using the lookahead contruct, ?=, you can do this:
    >
    > logline = "+ 30 Jan 12:20:09 [3988] addr: x.x.x.x"
    > logline[/^.*(?=\[\d+\])/][/ref]

    Another alternative is:

    logline=String.new("+ 30 Jan 12:20:09 [3988] addr: x.x.x.x")
    tda, number,address=(logline.split(/\[|\]|(?:addr\:)/, 3))
    puts "Putting it all back together: #{tda} [#{number}] addr: #{address}"

    The most surprising thing is that programming can be so easy.
    Ged Guest

  12. #12

    Default Re: principle of most suprise

    tony summerfelt <com> wrote in message news:<E4zSb.719$cgocable.net>...
    [...] 

    Remember to use 'p variable' to see what your object actually
    contains. This makes it much easy to solve this type of problem.

    One of the hardest thing with Ruby is realising all the stuff you
    don't have to do. In a language like Java or VB you are always having
    to keep the type at the front of your mind and proceed with caution.
    With Ruby you have to learn some laziness.
    Ged Guest

Similar Threads

  1. Setting Principle for HttpWorkerRequest
    By Michael in forum ASP.NET Security
    Replies: 8
    Last Post: August 12th, 01:29 PM
  2. The Uncertainty Principle Is Untenable
    By Josef 'Jupp' Schugt in forum Ruby
    Replies: 6
    Last Post: September 30th, 08:23 PM
  3. Replies: 0
    Last Post: September 30th, 01:50 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