Converting String-Arrays to Arrays in Ruby
A quick trick that came to mind recently for converting string arrays to arrays was something that seems to come up quite a bit. Just to define what I am talking about, when I say “string-arrays” vs arrays I mean something like this:
The most frequent time I have seen something like this is within something like a Postgresql HStore hash. Since the hash will not allow storing anything besides a string storing an array will automatically be converting into an string form of that array. Of course it could be argued that this is not the most “correct” way to accomplish your goal and yada yada but thats not what we are here for today. There are many instances where you may already have this going on or have your reasons so heres some tips!
Treat the string as an array for fast conversion. This works well if you know the format of the string and know it will be consistent.
This will gather the items within the brackets using the standard method of grabbing items from an array using a range
[1..-2]and then split the remaining string which is
1,2,3,4by a delimeter you speciy (in this case
,) leaving you with
Another method is using gsub to remove specfic characters than splitting the elements like so:
"[1,2,3,4]".gsub("[", "").gsub("]", "").split(",")
This will remove the brackets using
gsubthen split the remaining string using
split. This method has the benefit of flexibility over the other method, you can convert string arrays of non standard format like
" [1,2,3,3]"which contains a space but at the cost of speed. You could also throw things like
.compactinto the mix to remove that space as well but you can decide.
As far as speed is concerned the first method is far faster even on large data sets. I made a quick script to benchmark the two methods that is available here. But a glimpse of the results is as such:
As you can see the later increases in time alot as the amout of items inscreases.