Forum: Ruby Mapping a String to Properties - A Better Way?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
455ac2a64d06dc8461f4d258d7f7e980?d=identicon&s=25 Michael Trier (Guest)
on 2006-05-15 02:40
(Received via mailing list)
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
86e33dee4a89a8879a26487051c216a8?d=identicon&s=25 Michael Fellinger (Guest)
on 2006-05-15 06:39
(Received via mailing list)
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
A52b0e1c5d982f2512a03c5dbfd033d6?d=identicon&s=25 Dick Davies (Guest)
on 2006-05-15 11:01
(Received via mailing list)
On 15/05/06, Michael Fellinger <m.fellinger@gmail.com> wrote:
> maybe like that?
>
> params.each do |key, value|
>   key = key.split('_')

Doesn't this update the hash key back up in the caller?
455ac2a64d06dc8461f4d258d7f7e980?d=identicon&s=25 Michael Trier (Guest)
on 2006-05-15 11:26
(Received via mailing list)
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
86e33dee4a89a8879a26487051c216a8?d=identicon&s=25 Michael Fellinger (Guest)
on 2006-05-15 11:39
(Received via mailing list)
No, it keeps the hash intact - just try it in irb :)
>> (h = {1,2,3,4}).each{|key, value| key += 2}
=> {1=>2, 3=>4}
455ac2a64d06dc8461f4d258d7f7e980?d=identicon&s=25 Michael Trier (Guest)
on 2006-05-15 12:43
(Received via mailing list)
Well that's good to know.  Thanks.

Michael
E0ed615bd6632dd23165e045e3c1df09?d=identicon&s=25 Florian GroÃ? (Guest)
on 2006-05-15 16:46
(Received via mailing list)
Michael Fellinger wrote:

> params.each do |key, value|
>   key = key.split('_')
>   if key[0] == 'item_'

Minor improvement (IMHO):

key = key[/^item_(\d+)$/, 1]
if key then
   ...
end
This topic is locked and can not be replied to.