Thursday, 14 February 2019

Is there any function to get an array value from string except `eval`?


How to get an array value from string in ruby on rails without eval?

  

Input params in controller will be 
params[:city_id] = "1" or "[1,2,3,4,5]" 

Expected results should be [1] or [1,2,3,4,5] 

We can use eval(params[:city_id]).

Using eval method in controller is dangerous for sql. 

Result: 
 params[:city_id].scan(/\d+/).map(&:to_i)

No comments:

Post a Comment

Interactor in Rails

What is interactor? Interactor provides a common interface for performing complex user interactions An interactor is a simple, sin...