Help ruby started acting strange

Hey all,

I need some help. I'm having some problems with strings in Ruby.
More specifically I keep having to explicitly convert string to
strings. Let me say that I've had some problems with my system and
I've had to uninstall and reinstall ruby from the one-click installer
for Windows 'ruby 185-21.exe'. This might be unrelated though.

Below is the code in question the current_message object is a simple
mail object its just simple way for me to keep the various mail
section in one object. The first line with the Regular expression
work with out error. The problem is I don't understand why I need to
specify to_s for displayName when I'm parsing out the first and last
name. I had to specify to_s again when I reused firstName and
lastName later in my code.

displayName = current_message.Body.scan(/^Employee Name -+> (.*)\./)
firstName = displayName.to_s.split(", ")[1]
lastName = displayName.to_s.split(", ")[0]
searchUser = lastName.to_s.downcase[0,3] + firstName.to_s.downcase[0,1] + "*"

Any clues as to what might cause something like this to happen would
be much appreciated.

C.Particle

Hi,

1. scan returns array of arrays, therefore you have to call
displayName = current_message.Body.scan(/^Employee Name -+> (.*)\./).first.first
Then everything will work fine. Read documentation for more precise
explanation, and possible alternatives.

2. you can do all the parsing at once:
displayName, lastName, firstName =
current_message.Body.scan(/^Employee Name -+> ((.*), (.*))\./).first

(partial version of that would be:
lastName, firstName = displayName.split(', ')

3. FYI: ruby's naming convention tends to use display_name, last_name
etc. instead of displayName, etc. It's just a convention, so feel free
to write as you wish :wink:

4. when in doubt, use p as in p displayName to see what's happening.
In this case it would show [["Smith, John"]]. irb and ruby -rdebug are
handy as well.

···

On 11/6/06, Ion Frantzis <cparticle@gmail.com> wrote:

Hey all,

I need some help. I'm having some problems with strings in Ruby.
More specifically I keep having to explicitly convert string to
strings. Let me say that I've had some problems with my system and
I've had to uninstall and reinstall ruby from the one-click installer
for Windows 'ruby 185-21.exe'. This might be unrelated though.

Below is the code in question the current_message object is a simple
mail object its just simple way for me to keep the various mail
section in one object. The first line with the Regular expression
work with out error. The problem is I don't understand why I need to
specify to_s for displayName when I'm parsing out the first and last
name. I had to specify to_s again when I reused firstName and
lastName later in my code.

displayName = current_message.Body.scan(/^Employee Name -+> (.*)\./)
firstName = displayName.to_s.split(", ")[1]
lastName = displayName.to_s.split(", ")[0]
searchUser = lastName.to_s.downcase[0,3] + firstName.to_s.downcase[0,1] + "*"

Any clues as to what might cause something like this to happen would
be much appreciated.

C.Particle

Thanks I was not aware that scan worked that way I'll have to adjust to
take account of scan beharvior.

I did know that I could assign first and last in one line I was just
was being lazy about looking up the syntax(I'm still new to ruby).

I'll keep that in mind regarding the naming convention.

Regarding my issue the fourth line is still puzzling to me I would
think that even with my code the way it is firstName and lastName at
least should come out as stings and so the last line the searchUser
assignment shouldn't need to have lastName.to_s can anyone shed some
light as to why this would be?

displayName = current_message.Body.scan(/^Employee Name -+> (.*)\./)
firstName = displayName.to_s.split(", ")[1]
lastName = displayName.to_s.split(", ")[0]
searchUser = lastName.to_s.downcase[0,3] + firstName.to_s.downcase[0,1]
+ "*"

CParticle

Hi

A friend is building an application that will have files of a very specific format uploaded. As part of the upload process, he would like certain fields in the database record that is created for each upload to be populated from information in the pdf. Does a library that can do this exist, and if so, can you please point me in its general direction.

thanks
ivor

It should work without it, and it works for me without the to_s. Use p
to see what class is firstName and lastName.

Maybe posting your test message body would help.

···

On 11/6/06, CParticle <cparticle@gmail.com> wrote:

Thanks I was not aware that scan worked that way I'll have to adjust to
take account of scan beharvior.

I did know that I could assign first and last in one line I was just
was being lazy about looking up the syntax(I'm still new to ruby).

I'll keep that in mind regarding the naming convention.

Regarding my issue the fourth line is still puzzling to me I would
think that even with my code the way it is firstName and lastName at
least should come out as stings and so the last line the searchUser
assignment shouldn't need to have lastName.to_s can anyone shed some
light as to why this would be?

displayName = current_message.Body.scan(/^Employee Name -+> (.*)\./)
firstName = displayName.to_s.split(", ")[1]
lastName = displayName.to_s.split(", ")[0]
searchUser = lastName.to_s.downcase[0,3] + firstName.to_s.downcase[0,1]
+ "*"

CParticle wrote:

firstName = displayName.to_s.split(", ")[1]
lastName = displayName.to_s.split(", ")[0]

I don't think I could bear looking at this for five seconds without
rewriting it to avoid calling #strip twice...

last_name, first_name = display_name.split ', '

There, it's out of my system now.

David Vallner

ivor

···

On 11/7/06, Ivor <ivor@rails.co.za> wrote:

certain fields in the database record that is created for each upload to
be populated from information in the pdf. Does a library that can do
this exist, and if so, can you please point me in its general direction.

rpdf2txt (1) can extract text from PDF-files. It is not very well
documented (i.e.: not at all), but if you have any questions about
usage, I can help you get going.

hth
Hannes

1) http://download.ywesee.com/rpdf2txt/rpdf2txt-1.0.6.tar.bz2

Thanks Jan apparently I was looking at a junk message. This only goes
to show that one must always validate their data. Soon as I was
looking at the correct message it started to behave. Again thanks for
the help.

CParticle.

thanks a mil!

I'll pass on the information.

Have fun.

Ivor

Hannes Wyss wrote:

···

ivor

On 11/7/06, Ivor <ivor@rails.co.za> wrote:

certain fields in the database record that is created for each upload to
be populated from information in the pdf. Does a library that can do
this exist, and if so, can you please point me in its general direction.

rpdf2txt (1) can extract text from PDF-files. It is not very well
documented (i.e.: not at all), but if you have any questions about
usage, I can help you get going.

hth
Hannes

1) http://download.ywesee.com/rpdf2txt/rpdf2txt-1.0.6.tar.bz2