Q:

Ruby & Rails - Convert String to Proc

I'm in the following situation.  The current url looks like the following:  
/categories/Art
And I'm using name = location.pathname.split('/')[2] in order to grab the Art portion of the URL.  Then, I send an AJAX the following path back to the controller: http://localhost:3000/sort?sortMethod=name&category=name or date, whichever link is clicked on.  
Now in my controller I can use sort = params[:category] to get the string name, yet what I'd like to do with this string is sort an array by it.  @categories is an array of objects and I'd like to call .sort_by(&:sort) yet it doesn't recognize the string value of sort = name.  So now I'd like to convert this string into a proc in order to sort the array.  Anyone know how I accomplish this? 
Any help is greatly appreciated! 

A:

Convert it to a symbol first and then use Symbol#to_proc:
@categories.sort_by(&sort.to_sym)

However be sure that the users can't call anything malicious on your objects like:
http://localhost:3000/sort?sortMethod=destroy

One way of protecting yourself is to use attribute_accessible definitions in your model and then do
@categories.sort_by(&sort.to_sym) if Category.accessible_attributes.include?  sort.to_sym

