相关疑难解决方法(0)

Ruby on Rails:删除多个哈希键

我经常发现自己在写这个:

params.delete(:controller)  
params.delete(:action)  
params.delete(:other_key)  
redirect_to my_path(params)  
Run Code Online (Sandbox Code Playgroud)

删除的痕迹感觉不对,也没有:

[:controller, :action, :other_key].each do |k|
  params.delete(k)
end
Run Code Online (Sandbox Code Playgroud)

有什么更简单,更清洁的吗?

ruby ruby-on-rails

146
推荐指数
6
解决办法
6万
查看次数

How to collect many attributes from hash in ruby

I'm wondering if it's possible to collet many attributes from a hash.

Currently using ruby 2.6.3

Something like that

hash = { name: "Foo", email: "Bar", useless: nil }
other_hash = hash[:name, :email]
Run Code Online (Sandbox Code Playgroud)

The output should be another hash but without the useless key/value

ruby hash ruby-on-rails

2
推荐指数
1
解决办法
71
查看次数

标签 统计

ruby ×2

ruby-on-rails ×2

hash ×1