principle of most suprise

Discussion in 'Ruby' started by tony summerfelt, Jan 30, 2004.

  1. 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 [xxxx] 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 summerfelt, Jan 30, 2004
    #1
    1. Advertising

  2. tony summerfelt

    Gennady Guest

    tony summerfelt wrote:
    > 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


    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


    >
    > is it too much to assume that the string is split using [xxxx] 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.
    >
    >
     
    Gennady, Jan 30, 2004
    #2
    1. Advertising

  3. tony summerfelt wrote:
    > 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=logline.split(/\[\d+\]/)
    > tda.first
    >
    > is it too much to assume that the string is split using [xxxx] as a demliter
    > and that tda.first should return "+ 30 Jan 12:20:09"?


    Now tda.first returns "+ 30 Jan 12:20:09 ". That help?
     
    Joel VanderWerf, Jan 30, 2004
    #3
  4. tony summerfelt

    Maik Schmidt Guest

    tony summerfelt wrote:
    > 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
    >

    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 Schmidt, Jan 30, 2004
    #4
  5. On Fri, 30 Jan 2004 at 21:24 GMT, Maik Schmidt <> wrote:

    > split will always return an Array (see "Ruby in a nutshell, p. 54).


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

    > You do not have to force this explicitly by using the Array constructor.


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

    > And if you want to do so, you have to use '()' not '[]' :))


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


    --
    http://home.cogeco.ca/~tsummerfelt1
     
    tony summerfelt, Feb 1, 2004
    #5
  6. "tony summerfelt" <> schrieb im Newsbeitrag
    news:NbeTb.1048$...
    > On Fri, 30 Jan 2004 at 21:24 GMT, Maik Schmidt <>

    wrote:
    >
    > > split will always return an Array (see "Ruby in a nutshell, p. 54).

    >
    > i read that a number of times before i posted...that's why i was stumped
    >
    > > You do not have to force this explicitly by using the Array

    constructor.
    >
    > i don't know what i was thinking...you'd never know it, but i'm a better
    > programmer than that :/
    >
    > > And if you want to do so, you have to use '()' not '[]' :))

    >
    > i want to thank everyone for the polite replies :) it must have been

    hard
    > restraining yourselves :)


    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 Klemme, Feb 2, 2004
    #6
  7. On Mon, 02 Feb 2004 at 09:13 GMT, Robert Klemme <> wrote:

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


    yup, thought of that one already :)

    > To make results look a bit nicer you could also do


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

    --
    http://home.cogeco.ca/~tsummerfelt1
     
    tony summerfelt, Feb 2, 2004
    #7
  8. tony summerfelt <> writes:

    > 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


    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 Schmitt, Feb 5, 2004
    #8
  9. tony summerfelt

    Ged Guest

    tony summerfelt <> wrote in message news:<E4zSb.719$>...
    [...]
    >
    > i seem to be getting hung up on the simplest things. i'm using
    > 'ruby in a nutshell' and 'programming ruby' as my references.


    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, Feb 5, 2004
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. CW

    Webmessenger principle

    CW, Sep 22, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    706
    Steven Cheng[MSFT]
    Sep 23, 2004
  2. H.MuthuKumaraRajan
    Replies:
    3
    Views:
    488
    H.MuthuKumaraRajan
    Feb 4, 2004
  3. Pavel Pluhacek

    principle of stport std::sort

    Pavel Pluhacek, Sep 1, 2003, in forum: C++
    Replies:
    2
    Views:
    438
    llewelly
    Sep 1, 2003
  4. xkenneth
    Replies:
    8
    Views:
    367
    Bruno Desthuilliers
    Feb 6, 2008
  5. Replies:
    3
    Views:
    120
Loading...

Share This Page