# Help ruby started acting strange

**URL:** <https://www.ruby-forum.com/t/help-ruby-started-acting-strange/75044>\
**Category:** Ruby\
**Created:** [November 7, 2006, 11:07am UTC](https://www.ruby-forum.com/t/help-ruby-started-acting-strange/75044 "2006-11-07T11:07:50Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![cparticle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/2bfe46/32.png) [@cparticle](https://www.ruby-forum.com/u/cparticle)\
**Post date:** [November 7, 2006, 11:07am UTC](https://www.ruby-forum.com/t/help-ruby-started-acting-strange/75044/1 "2006-11-07T11:07:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cparticle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/2bfe46/32.png) [@cparticle](https://www.ruby-forum.com/u/cparticle)\
**Post date:** [November 7, 2006, 11:07am UTC](https://www.ruby-forum.com/t/help-ruby-started-acting-strange/75044/2 "2006-11-07T11:07:59Z")

</div>

On 11/6/06, Ion F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> section in one object. The first line with the Regular expression
> 
> 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(', ')

1. 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 😉

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

---

<div class="post-metadata">

**Author:** ![cparticle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/2bfe46/32.png) [@cparticle](https://www.ruby-forum.com/u/cparticle)\
**Post date:** [November 7, 2006, 11:08am UTC](https://www.ruby-forum.com/t/help-ruby-started-acting-strange/75044/3 "2006-11-07T11:08:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cparticle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/2bfe46/32.png) [@cparticle](https://www.ruby-forum.com/u/cparticle)\
**Post date:** [November 7, 2006, 11:08am UTC](https://www.ruby-forum.com/t/help-ruby-started-acting-strange/75044/4 "2006-11-07T11:08:13Z")

</div>

On 11/6/06, CParticle [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

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

---

<div class="post-metadata">

**Author:** ![cparticle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/2bfe46/32.png) [@cparticle](https://www.ruby-forum.com/u/cparticle)\
**Post date:** [November 7, 2006, 11:08am UTC](https://www.ruby-forum.com/t/help-ruby-started-acting-strange/75044/5 "2006-11-07T11:08:17Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![cparticle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/2bfe46/32.png) [@cparticle](https://www.ruby-forum.com/u/cparticle)\
**Post date:** [November 7, 2006, 11:08am UTC](https://www.ruby-forum.com/t/help-ruby-started-acting-strange/75044/6 "2006-11-07T11:08:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cparticle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/2bfe46/32.png) [@cparticle](https://www.ruby-forum.com/u/cparticle)\
**Post date:** [November 7, 2006, 11:09am UTC](https://www.ruby-forum.com/t/help-ruby-started-acting-strange/75044/7 "2006-11-07T11:09:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cparticle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/2bfe46/32.png) [@cparticle](https://www.ruby-forum.com/u/cparticle)\
**Post date:** [November 7, 2006, 11:25am UTC](https://www.ruby-forum.com/t/help-ruby-started-acting-strange/75044/8 "2006-11-07T11:25:15Z")

</div>

thanks a mil!

I’ll pass on the information.

Have fun.

Ivor

---

<div class="post-metadata">

**Author:** ![cparticle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/2bfe46/32.png) [@cparticle](https://www.ruby-forum.com/u/cparticle)\
**Post date:** [November 7, 2006, 11:15am UTC](https://www.ruby-forum.com/t/help-ruby-started-acting-strange/75044/9 "2006-11-07T11:15:13Z")

</div>

ivor

On 11/7/06, Ivor [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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](http://download.ywesee.com/rpdf2txt/rpdf2txt-1.0.6.tar.bz2)
