Is there any function for get an array value from string in ruby except eval?

I have a string. It will be "1" or "['1','2','3','4']". By using eval method i can get the result 1 or [1,2,3,4] but i need the result should be array [1] or [1,2,3,4].

params[:city_id] = eval(params[:city_id]) #params will be "1" or "['1','2','3']"

scope :city, -> (params) { params[:city_id].present? ? where(city_id: (params[:city_id].is_a?(String) ? eval(params[:city_id]) : params[:city_id])) : all }

Here i don’t want eval.

scope :city, -> (params) { params[:city_id].present? ? where(city_id: params[:city_id]) : all }

params[:city_id] #should be array values e.g [1] or [1,2,3,4] instead of string


#ruby-on-rails #ruby

1.80 GEEK