Created
November 15, 2012 14:02
-
-
Save gabrielengel/4078769 to your computer and use it in GitHub Desktop.
Get Symbol Array from a String
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def symbol_array_from_string(value) | |
regex = /\[((:([^ ,\]])+) *,{0,1} *)+\]/ | |
return value unless value =~ regex | |
value.scan(/:[^ ,\]]+/).map{|v| v.gsub(/^:/,'').to_sym} | |
end | |
result = symbol_array_from_string("[:a, :apple,:dog]") | |
result == [:a, :apple, :dog] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment