简体   繁体   中英

Array to Hash with conditional logic in Ruby?

ruby 2.1.1

Is there a way to do the logic in this piece of code in one line or a more concise manner?

user = User.new
h = Hash.new
attrs = [:name, :foo, :bar]
attrs.each do |a|
    h[a] = user[a] if user.has_attribute? a
end
return h

If you're using Rails and User is an ActiveRecord model (which it looks like given your use of has_attribute? ) then this will do the same thing:

user = User.new
...
return user.attributes.slice("name", "foo", "bar")

Or, if you really want symbols:

return user.attributes.with_indifferent_access.slice(:name, :foo, :bar)

It seems you are on Rails. If so,then -

attrs = [:name, :foo, :bar]
# the result hash will be returned, if last line of the method.
user.attributes.extract!(*attrs) 

Look these methods extract! and attributes .

Example :

arup@linux-wzza:~/Rails/app> rails c
Loading development environment (Rails 4.1.1)
2.0.0-p451 :001 > h = { a: 1, b: 2, c: 3, d: 4 }
 => {:a=>1, :b=>2, :c=>3, :d=>4}
2.0.0-p451 :002 > h.extract!(:a ,:b ,:x)
 => {:a=>1, :b=>2}
2.0.0-p451 :003 >

Answers above are correct in Rails scope, I'l just add generic solution:

# assuming user[a] returns nil, if user have no a attribute
[:name, :foo, :bar].
  map{|a| [attr, user[a]]}.
  reject{|k, v| v.nil?}.
  to_h

# assuming user[a] can raise if not user.has_attribute?(a)
[:name, :foo, :bar].
   map{|a| [attr, user.has_attribute?(a) && user[a]]}.
   reject{|k, v| !v}.
   to_h

I've formatted them as NOT one-liners, but they are still one-statements :)

Basically, the trick is "invent the right method chain to convert one sequence to other", and requires to know all Enumerable sequence-transforming methods (map/select/reduce/reject/...), as well as a method to transform array of key-value pairs into hash ( #to_h is standard in Ruby 2.1.1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM