skip to main
|
skip to sidebar
ReuvenabLog
Showing posts with label
Hash
.
Show all posts
Showing posts with label
Hash
.
Show all posts
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
}
)
}
Home
Subscribe to:
Posts (Atom)
Blog Archive
▼
2009
(2)
▼
July
(1)
Apache: Enable access to the file in the directory...
►
March
(1)
►
2008
(1)
►
May
(1)
About Me
Reuven Abliyev
View my complete profile