skip to main
|
skip to sidebar
ReuvenabLog
Thursday, May 29, 2008
Rails: String to Hash
Given a string like " 234,456 , 678 , 4 " I want to produce Hash object {456=>true, 678=>true, 234=>true, 4=>true}
Solution:
a =
" 234,456 , 678 , 4 "
b = a.
split
(
%
r
{
\s
*
,\s
*
}
)
.
inject
(
Hash
.
new
)
{
|
h, e
|
h.
merge
(
{
e.
to_i
=>
true
}
)
}
Newer Posts
Home
Subscribe to:
Posts (Atom)
Blog Archive
►
2009
(2)
►
July
(1)
►
March
(1)
▼
2008
(1)
▼
May
(1)
Rails: String to Hash
About Me
Reuven Abliyev
View my complete profile