principle of most suprise

T

tony summerfelt

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

Gennady

tony said:
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.
 
J

Joel VanderWerf

tony said:
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?
 
M

Maik Schmidt

tony said:
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/>
 
T

tony summerfelt

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 :)
 
R

Robert Klemme

tony summerfelt said:
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
 
T

tony summerfelt

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

Frank Schmitt

tony summerfelt said:
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
 
G

Ged

tony summerfelt said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top