From: "Eric I." Date: 2008-06-04T11:49:15+09:00 Subject: Re: Remove Parts of a String On Jun 3, 7:15 pm, Dan __ wrote: > Eric, your hash idea seems very interesting.  I'm assuming I'd have to > update the hash every time I changed the entry in the database though, > correct? Or you can do it the other way around, if that's more convenient. You can add a new item to the Hash, and regenerate the String, and then save it to your db. See below where I assign to the variable back_to_string. ==== ratings = "12343:3,73820:1,183874:8" ratings_array = ratings.split(/[,:]/).map { |str| str.to_i } # we now have array w/ ids & ratings interleaved: # [12343, 3, 73820, 1, 183874, 8] ratings_by_id = Hash[*ratings_array] # we convert that into a hash where keys are ids and values are ratings: # {12343=>3, 183874=>8, 73820=>1} # and now we can look up an id and get either its rating or, if there # is no rating, nil r1 = ratings_by_id[183874] p r1 r2 = ratings_by_id[77777] p r2 # add a new rating to the hash ratings_by_id[62541] = 4 # regenerate the String back_to_string = ratings_by_id.map { |k, v| "#{k}:#{v}"}.join(',') puts back_to_string ==== Eric ==== LearnRuby.com offers Rails & Ruby HANDS-ON public & ON-SITE workshops. Ruby Fundamentals Wkshp June 16-18 Ann Arbor, Mich. Ready for Rails Ruby Wkshp June 23-24 Ann Arbor, Mich. Ruby on Rails Wkshp June 25-27 Ann Arbor, Mich. Ruby Plus Rails Combo Wkshp June 23-27 Ann Arbor, Mich Please visit http://LearnRuby.com for all the details.