Is there an opposite function of slice function in Ruby?

AdamNYC picture AdamNYC · Jan 9, 2012 · Viewed 11.3k times · Source

In this post, slice function is used to get only necessary elements of params. What would be the function I should use to exclude an element of params (such as user_id)?

Article.new(params[:article].slice(:title, :body))

Thank you.

Answer

Guillaume picture Guillaume · Jan 9, 2012

Use except:

a = {"foo" => 0, "bar" => 42, "baz" => 1024 }
a.except("foo")
# returns => {"bar" => 42, "baz" => 1024}