Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > principle of most suprise

Reply
Thread Tools

principle of most suprise

 
 
tony summerfelt
Guest
Posts: n/a
 
      01-30-2004
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
 
Reply With Quote
 
 
 
 
Gennady
Guest
Posts: n/a
 
      01-30-2004
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.
>
>




 
Reply With Quote
 
 
 
 
Joel VanderWerf
Guest
Posts: n/a
 
      01-30-2004
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?



 
Reply With Quote
 
Maik Schmidt
Guest
Posts: n/a
 
      01-30-2004
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/>

 
Reply With Quote
 
tony summerfelt
Guest
Posts: n/a
 
      02-01-2004
On Fri, 30 Jan 2004 at 21:24 GMT, Maik Schmidt <(E-Mail Removed)> 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
 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      02-02-2004

"tony summerfelt" <(E-Mail Removed)> schrieb im Newsbeitrag
news:NbeTb.1048$(E-Mail Removed)...
> On Fri, 30 Jan 2004 at 21:24 GMT, Maik Schmidt <(E-Mail Removed)>

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

 
Reply With Quote
 
tony summerfelt
Guest
Posts: n/a
 
      02-02-2004
On Mon, 02 Feb 2004 at 09:13 GMT, Robert Klemme <(E-Mail Removed)> 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
 
Reply With Quote
 
Frank Schmitt
Guest
Posts: n/a
 
      02-05-2004
tony summerfelt <(E-Mail Removed)> 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
 
Reply With Quote
 
Ged
Guest
Posts: n/a
 
      02-05-2004
tony summerfelt <(E-Mail Removed)> wrote in message news:<E4zSb.719$(E-Mail Removed)>...
[...]
>
> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Principle Engineer needed Joe Feldman C++ 14 05-06-2010 12:47 AM
inheritence of aliases methods - suprise! ara.t.howard@noaa.gov Ruby 3 02-06-2006 09:57 PM
Webmessenger principle CW ASP .Net 5 09-23-2004 02:14 AM
Dependency Inversion Principle Dilemma Thomas Matthews C++ 12 12-23-2003 08:51 PM
principle of stport std::sort Pavel Pluhacek C++ 2 09-01-2003 05:37 PM



Advertisments