Just out of curiosity:
▶ str = "id,name,user[id,email]"
▶ eval "[#{str.gsub(/(\w+)\[(.*?)\]/, '{\1=>[\2]}').gsub(/\w+/, ':\0')}]"
#⇒ [
# [0] :id,
# [1] :name,
# [2] {
# :user => [
# [0] :id,
# [1] :email
# ]
# }
#]
Disclamer: to use eval in production one must understand risks.
UPD Safe evaling (note that every ASCII \w symbol is converted to it’s wide pair from UTF-8 to prevent injection; not the best way around, but it works nicely unless you have ruby functions named with wide characters):
▶ safe = str.gsub(/\w/) do |e|
▷ e.each_codepoint.map do |cp|
▷ cp + 0xFF00 - 0x0020
▷ end.pack('U')
▷ end
#⇒ "id,name,user[id,email]"
▶ eval "[#{safe.gsub(/(\p{L}+)\[(.*?)\]/, '{\1=>[\2]}').gsub(/\p{L}+/, ':\0')}]"
#⇒ [
# [0] :id,
# [1] :name,
# [2] {
# :user => [
# [0] :id,
# [1] :email
# ]
# }
#]
Now you are free to turn keys back from wide characters to ASCII.
["id", "name", "user" => ["id", "email"]]to["id", "name", {"user"=>["id", "email"]}]. In the example, user became a hash inside the array. You can try it typing my original array in console.