# Mapping a String to Properties - A Better Way?

**URL:** <https://www.ruby-forum.com/t/mapping-a-string-to-properties-a-better-way/60507>\
**Category:** Ruby\
**Created:** [May 15, 2006, 2:40am UTC](https://www.ruby-forum.com/t/mapping-a-string-to-properties-a-better-way/60507 "2006-05-15T02:40:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![afewtips](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/76d3ee/32.png) [@afewtips](https://www.ruby-forum.com/u/afewtips)\
**Post date:** [May 15, 2006, 2:40am UTC](https://www.ruby-forum.com/t/mapping-a-string-to-properties-a-better-way/60507/1 "2006-05-15T02:40:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![afewtips](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/76d3ee/32.png) [@afewtips](https://www.ruby-forum.com/u/afewtips)\
**Post date:** [May 15, 2006, 6:39am UTC](https://www.ruby-forum.com/t/mapping-a-string-to-properties-a-better-way/60507/2 "2006-05-15T06:39:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![afewtips](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/76d3ee/32.png) [@afewtips](https://www.ruby-forum.com/u/afewtips)\
**Post date:** [May 15, 2006, 11:01am UTC](https://www.ruby-forum.com/t/mapping-a-string-to-properties-a-better-way/60507/3 "2006-05-15T11:01:58Z")

</div>

On 15/05/06, Michael F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> maybe like that?
> 
> params.each do |key, value|  
> key = key.split(‘\_’)

Doesn’t this update the hash key back up in the caller?

---

<div class="post-metadata">

**Author:** ![afewtips](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/76d3ee/32.png) [@afewtips](https://www.ruby-forum.com/u/afewtips)\
**Post date:** [May 15, 2006, 11:26am UTC](https://www.ruby-forum.com/t/mapping-a-string-to-properties-a-better-way/60507/4 "2006-05-15T11:26:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![afewtips](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/76d3ee/32.png) [@afewtips](https://www.ruby-forum.com/u/afewtips)\
**Post date:** [May 15, 2006, 12:43pm UTC](https://www.ruby-forum.com/t/mapping-a-string-to-properties-a-better-way/60507/5 "2006-05-15T12:43:41Z")

</div>

Well that’s good to know. Thanks.

Michael

---

<div class="post-metadata">

**Author:** ![afewtips](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/76d3ee/32.png) [@afewtips](https://www.ruby-forum.com/u/afewtips)\
**Post date:** [May 15, 2006, 11:39am UTC](https://www.ruby-forum.com/t/mapping-a-string-to-properties-a-better-way/60507/6 "2006-05-15T11:39:08Z")

</div>

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}

---

<div class="post-metadata">

**Author:** ![afewtips](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/76d3ee/32.png) [@afewtips](https://www.ruby-forum.com/u/afewtips)\
**Post date:** [May 15, 2006, 4:46pm UTC](https://www.ruby-forum.com/t/mapping-a-string-to-properties-a-better-way/60507/7 "2006-05-15T16:46:21Z")

</div>

Michael F. 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
