ruby 中枚举的迭代

发布于 2024-10-08 22:01:22 字数 207 浏览 1 评论 0原文

我有以下带有枚举(UserStatus)的 ruby​​ 类(user.rd):

class User< ActiveRecord::Base
end

class UserStatus
  NEW = "new"
  OLD = "old"
  DELETED = "deleted"
end

有没有办法可以迭代所有枚举值?

i have the following ruby class (user.rd) that has an enum (UserStatus):

class User< ActiveRecord::Base
end

class UserStatus
  NEW = "new"
  OLD = "old"
  DELETED = "deleted"
end

is there a way i can iterate over all the enum values?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

独自唱情﹋歌 2024-10-15 22:01:22

您在那里创建的内容称为“常量”,而不是枚举。正如 Zabba 所说,“Ruby 没有‘enum’。”如果你必须保留这个数据结构,如果你已经在代码中使用了常量,那么你可以像这样迭代它们

UserStatus.constants(false).each do |const_name|
  p [ const_name, UserStatus.const_get( const_name ) ]
end
#=> :NEW, "new"]
#=> [:OLD, "old"]
#=> [:DELETED, "deleted"]

:需要上面的 false 来防止您获取超类中定义的常量:

class Foo; A = 1; end
class Bar < Foo; B = 1; end

Bar.constants
#=> [:B, :A]
Bar.constants(false)
#=> [:B]

如果您不习惯使用单个常量,您可能有兴趣创建不可变值的冻结哈希:

class User < ActiveRecord::Base
  STATUS = {
    :new => 'new',
    :old => 'old',
    :deleted => 'deleted'
  }
  STATUS.freeze
  STATUS.values.each{ |v| v.freeze }
end

bob.status = User::STATUS[:new]
User::STATUS.each do |name,string|
  p [ name, string ]
end

What you have created there are called 'constants', not enumerations. As Zabba said, "Ruby does not have 'enum'." If you must keep this data structure, if you are already using the constants in your code, then you can iterate them like so:

UserStatus.constants(false).each do |const_name|
  p [ const_name, UserStatus.const_get( const_name ) ]
end
#=> :NEW, "new"]
#=> [:OLD, "old"]
#=> [:DELETED, "deleted"]

The use of false above is needed to prevent you from getting constants defined in superclasses:

class Foo; A = 1; end
class Bar < Foo; B = 1; end

Bar.constants
#=> [:B, :A]
Bar.constants(false)
#=> [:B]

If you are not married to the use of individual constants, you might be interested in creating a frozen Hash of immutable values instead:

class User < ActiveRecord::Base
  STATUS = {
    :new => 'new',
    :old => 'old',
    :deleted => 'deleted'
  }
  STATUS.freeze
  STATUS.values.each{ |v| v.freeze }
end

bob.status = User::STATUS[:new]
User::STATUS.each do |name,string|
  p [ name, string ]
end
万人眼中万个我 2024-10-15 22:01:22

Ruby 没有“枚举”。这些是您定义的常量。
鉴于此,您可以像这样迭代类中的常量:

#UserStatus.constants returns an array, which we then iterate over
UserStatus.constants.each do |el|
  p el
end

Ruby does not have "enum". Those are constants that you have defined.
Given that, you can iterate over constants in a class like this:

#UserStatus.constants returns an array, which we then iterate over
UserStatus.constants.each do |el|
  p el
end
旧人九事 2024-10-15 22:01:22

看看这个: http://code.dblock.org/ShowPost.aspx?id=184 (比 http://www.rubyfleebie.com/enumerations-and- 略有改进红宝石/)。让您编写以下内容。

class Gender
  include Enum

  Gender.define :MALE, "male"
  Gender.define :FEMALE, "female"
end

当然还有

Gender.all
Gender::MALE

Look at this: http://code.dblock.org/ShowPost.aspx?id=184 (slight improvement over http://www.rubyfleebie.com/enumerations-and-ruby/). Lets you write the following.

class Gender
  include Enum

  Gender.define :MALE, "male"
  Gender.define :FEMALE, "female"
end

And of course

Gender.all
Gender::MALE
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文