Help ruby started acting strange

I

Ion Frantzis

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
 
J

Jan Svitok

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 ;-)

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

CParticle

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
 
J

Jan Svitok

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]
+ "*"

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

CParticle

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

David Vallner

--------------enigF3DBC97C9D7F3A50EC73FA80
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
firstName =3D displayName.to_s.split(", ")[1]
lastName =3D displayName.to_s.split(", ")[0]
=20

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 =3D display_name.split ', '

There, it's out of my system now.

David Vallner


--------------enigF3DBC97C9D7F3A50EC73FA80
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: OpenPGP digital signature
Content-Disposition: attachment; filename="signature.asc"

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (MingW32)

iD8DBQFFT87Qy6MhrS8astoRAm6cAJ4j3c+sZqNrVQOPvo9TW3qq/YSF2gCdH9vG
lr/0K7uTmkLB9hydXxq7lxc=
=J/nq
-----END PGP SIGNATURE-----

--------------enigF3DBC97C9D7F3A50EC73FA80--
 
I

Ivor

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
 
H

Hannes Wyss

ivor

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
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top