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("_"), :x => a, :y => a, :width => a, :height => a) end end
Thanks in advance for any pointers.