开发者

Improving on retrieving values for a class constant

开发者 https://www.devze.com 2023-03-15 06:27 出处:网络
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

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)
0

精彩评论

暂无评论...
验证码 换一张
取 消