Johandk - 1 year ago 76

Ruby Question

I have a string like this:

`price<=>656000<br>bathrooms<=>1<br>bedrooms<=>3<br>pets<=>1<br>surface<=>60<br>brokerfree<=>1`

model<=>opel/corsa<br>mileage<=>67000<br>vinnumber<=>unknown<br>price<=>145000<br>year<=>2010<br>condition<=>2<br>transmission<=>unknown<br>cartype<=>1

and want I want to get it into a hash

`:model=>'opel/corsa'`

etc etc... the string is variable so this is also valid:

`year<=>2015<br>condition<=>1<br>price<=>2100mileage<=>22000<br>price<=>120000<br>year<=>2012<br>condition<=>2`

or this

`price<=>656000<br>bathrooms<=>1<br>bedrooms<=>3<br>pets<=>1<br>surface<=>60<br>brokerfree<=>1`

model<=>opel/corsa<br>mileage<=>67000<br>vinnumber<=>unknown<br>price<=>145000<br>year<=>2010<br>condition<=>2<br>transmission<=>unknown<br>cartype<=>1

Answer Source

You don't need a regex. You can use plain ruby methods.

```
array = string.split('<br>')
hash = Hash[array.map {|el| el.split('<=>') }]
```