Improving on retrieving values for a class constant
I a开发者_开发百科m using Ruby on Rails 3.0.7 and I would like to set a class CONSTANT value. Since the .each_key
method for a hash
class seems to not return values as it makes a map
method for array
classes, I implemented the following:
class User < ActiveRecord::Base
return_values = []
CONSTANT = (1..1).map { |e|
HASH.each_key { |key|
return_values << key.to_s
}
return_values
}.flatten
end
The above code works for me, but it is very far from being "good" code. How can I improve that?
I think you're looking for
CONSTANT = HASH.keys.map(&:to_s)
精彩评论