Skip to content

Instantly share code, notes, and snippets.

@gabrielengel
Created November 15, 2012 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gabrielengel/4078769 to your computer and use it in GitHub Desktop.
Save gabrielengel/4078769 to your computer and use it in GitHub Desktop.
Get Symbol Array from a String
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