From: Michael Trier Date: 2006-05-15T18:25:04+09:00 Subject: Re: Mapping a String to Properties - A Better Way? I forgot about parallel assignments. I like that. I'd probably keep the regex or at least not alter the key as Dick stated. I'd be interested to know if it does make a difference. Thanks. Michael On 5/15/06, Michael Fellinger wrote: > maybe like that? > > params.each do |key, value| > key = key.split('_') > if key[0] == 'item_' > x, y, width, height = value.split > Item.update key, :x => x, :y => y, :width => width, :height => height > end > end > > On Monday 15 May 2006 09:39, Michael Trier wrote: > > I have a piece of code that works fine, but I'm wondering if there is > > a better way to go about it. The method is receiving a hash (params) > > and in that hash are a group of items named item_1, item_2, item_3, > > and so on. Each of these items contains a whitespace separated list > > of values. For instance: > > > > item_1 => "120 355 240 145" > > item_2 => "130 400 100 100" > > > > These values map to x, y, width, and height properties on an object. > > > > Here's my code, but is there a better, more Ruby way to do this: > > > > params.each do |key, value| > > if key =~ /^item_/ > > a = value.split > > Item.update(key.split("_")[1], > > > > :x => a[0], > > :y => a[1], > > :width => a[2], > > :height => a[3]) > > > > end > > end > > > > Thanks in advance for any pointers. > > > > Michael > > -- > Reporter: "What would you do if you found a million dollars?" > Yogi Berra: "If the guy was poor, I would give it back." > >