OLW (One Liner of the Week): Convert To Int Array

Another #rubyoneliner – we use this one all the time in our backend controllers when dealing with parameters that should be arrays.string_or_array.to_s.gsub(/[\[\]”‘\\]/,””).split(“,”).map {|val| val.strip.to_i}

It is particularly interesting because it can take any string or array of strings/ints/arrays/etc and convert it into an array of ints, as long as the elements are separated by commas.

“I’m angrier than a dragon trying to blow out candles!”
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s