From: Jan Svitok Date: 2006-11-07T05:57:07+09:00 Subject: Re: Help ruby started acting strange On 11/6/06, Ion Frantzis 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 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.