From: Michael Fellinger Date: 2006-05-15T13:37:47+09:00 Subject: Re: Mapping a String to Properties - A Better Way? 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."