Robert Klemme wrote:
> On 10/28/2009 09:22 PM, Matt Brooks wrote:
>> I guess you could say the strings are comma separated within each string
>> blah = "CHLE,231,1"
>> 
>> blah["CHLE,231,2"]
>> => nil            * GOOD
>> 
>> blah["CHLE,23"]
>> => "CHLE,23"      * BAD, I want this to be NIL
>> 
>> 
>> Thanks for the help!!!
> 
> You can split your original string into an Array and start testing from
> the first element on.
> 
> irb(main):001:0> check = 'CHLE,231,1'
> => "CHLE,231,1"
> irb(main):002:0> prepared = check.split /,+/
> => ["CHLE", "231", "1"]
> 
> Cheers
> 
>   robert

Here is a quick implementation. I did not spend that much time on it so 
there could be some holes. Looks like you could do it in one line if you 
wanted. Enjoy - Richard

def check(s2)
 s1 =  "CHLE,231,1"
 return nil if s2 == ""
 a1 = s1.split /,/
 a2 = s2.split /,/
 a1[0...a2.size] == a2 ? s2 : nil
end

puts check("CHLE,231,1")
puts check("CHLE,231")
puts check("CHLE,23")
puts check("CHLE")
puts check("")

Output:
CHLE,231,1
CHLE,231
nil
CHLE
nil


-- 
Posted via http://www.ruby-forum.com/.