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

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].

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

What's new in Bootstrap 5 and when Bootstrap 5 release date?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Random Password Generator Online

HTML Color Picker online | HEX Color Picker | RGB Color Picker

Explain Ruby on rails MVC

Our Ruby on Rails Training will provide you to learn about Rails and web applications development with realty. Our Ruby on Rails Course also includes live sessions.

Pros & Cons you must know before using Ruby on Rails for your startup

Click here, free ruby on rails course videos for you. It shows you best way to complete Ruby certification. OnlineITGuru guides you towards easy Web script

Ruby on Rails Performance Tuning

Synchronises Assets between Rails and S3. Contribute to AssetSync/asset_sync development by creating an account on GitHub.