Siep Korteling wrote:
> Jet Koten wrote:
>> Hi all,
>> 
>> I'm new to Ruby and even newer to regex. I'm trying to write my first
>> [useful] Ruby program and need a way to cut out an unneeded prefix
>> substring and retain the substring that comes after it.
>> 
>> Here are the actual details from my code:
>> 
>> result.each do |item|
>>   price = item.search(".price").text.match(/\d+[.]\d+/)
>>   condition = item.search(".condition").text.match(/Used - ([^,]+)/)
>>   rating = item.search(".rating a").text.to_i
>>   seller = item.search(".seller b").text
>>   puts "#{price} - #{condition} - #{rating} - #{seller}"
>> end
>> 
>> The one from condition [in the code above] is the one that is giving me
>> a challenge. The string that is sent to condition will always be exactly
>> one of the following and nothing else at all:
>> 
>> "Used - Like New"
>> "Used - Very Good"
>> "Used - Good"
>> "Used - Acceptable"
>> (...)
> 
> This is another option, avoiding regular expressions. It's kind of old 
> school, but it's fast, flexible, and handles garbage.
> 
> sanitize_condition = Hash.new("Unknown")
> sanitize_condition["Used - Like New"] = "Like New"
> sanitize_condition["Used - Very Good"] = "Very Good"
> sanitize_condition["Used - Good"] = "Good"
> sanitize_condition["Used - Acceptable"] = "Acceptable"
> sanitize_condition["Used - Broken"] = "Kaput"
> 
> demo_conditions = ["Used - Like New","",nil,"Used - Broken","Used - 
> Acceptable","garble"]
> demo_conditions.each{|cond| puts sanitize_condition[cond] }
> 
> hth,
> 
> Siep

Hi Siep,

Thanks! My offline friend actually suggested that I refactor everything 
into a hash actually, because the condition info is just one criteria of 
many that I am pulling into my app...

but it is making my head spin because I am so new to Ruby, so I'm going 
to take a break and then look at it again and also look at the 
documentation for hash and see what I can come up with.

My friend also suggested that I write sudocode for all my desired 
functionality and that that could help a lot. I have a prioritized list 
for now, but it is making my head hurt to try and do so much that I 
don't know how to do! :)

I can't say enough how helpful the list/forum is, and that I'm very 
grateful for everyone using their free time to help me along.
-- 
Posted via http://www.ruby-forum.com/.