在Ruby中实现枚举习语的最佳方法是什么?我正在寻找一些东西,我可以使用(几乎)像Java/ c#枚举。
当前回答
我使用以下方法:
class MyClass
MY_ENUM = [MY_VALUE_1 = 'value1', MY_VALUE_2 = 'value2']
end
我喜欢它的优点如下:
它在视觉上将值组合为一个整体 它执行一些编译时检查(与仅使用符号相比) 我可以很容易地访问所有可能值的列表:只有MY_ENUM 我可以轻松地访问不同的值:MY_VALUE_1 它可以有任何类型的值,而不仅仅是Symbol
符号可能会更好,因为如果你在另一个类中使用它,你不必写外部类的名称(MyClass::MY_VALUE_1)
其他回答
查看ruby-enum gem, https://github.com/dblock/ruby-enum。
class Gender
include Enum
Gender.define :MALE, "male"
Gender.define :FEMALE, "female"
end
Gender.all
Gender::MALE
有时我所需要的是能够获取enum的值,并识别其名称类似于java世界。
module Enum
def get_value(str)
const_get(str)
end
def get_name(sym)
sym.to_s.upcase
end
end
class Fruits
include Enum
APPLE = "Delicious"
MANGO = "Sweet"
end
Fruits.get_value('APPLE') #'Delicious'
Fruits.get_value('MANGO') # 'Sweet'
Fruits.get_name(:apple) # 'APPLE'
Fruits.get_name(:mango) # 'MANGO'
这对我来说是枚举的目的,并保持它非常可扩展。您可以向Enum类添加更多的方法,并且viola可以在所有已定义的Enum中免费获得它们。为例。Get_all_names之类的。
另一种使用一致的平等处理来模拟枚举的方法(无耻地采用了Dave Thomas)。允许打开枚举(很像符号)和关闭(预定义的)枚举。
class Enum
def self.new(values = nil)
enum = Class.new do
unless values
def self.const_missing(name)
const_set(name, new(name))
end
end
def initialize(name)
@enum_name = name
end
def to_s
"#{self.class}::#@enum_name"
end
end
if values
enum.instance_eval do
values.each { |e| const_set(e, enum.new(e)) }
end
end
enum
end
end
Genre = Enum.new %w(Gothic Metal) # creates closed enum
Architecture = Enum.new # creates open enum
Genre::Gothic == Genre::Gothic # => true
Genre::Gothic != Architecture::Gothic # => true
也许最好的轻量级方法是
module MyConstants
ABC = Class.new
DEF = Class.new
GHI = Class.new
end
这样,值就有了关联的名称,就像在Java/ c#中一样:
MyConstants::ABC
=> MyConstants::ABC
要获得所有的值,可以这样做
MyConstants.constants
=> [:ABC, :DEF, :GHI]
如果你想要枚举的序号值,你可以这样做
MyConstants.constants.index :GHI
=> 2
两种方式。符号(:foo符号)或常量(foo符号)。
当您希望增强可读性而不使用文字字符串乱写代码时,符号是合适的。
postal_code[:minnesota] = "MN"
postal_code[:new_york] = "NY"
当您有一个重要的基础值时,常量是合适的。只需声明一个模块来保存常量,然后在其中声明常量。
module Foo
BAR = 1
BAZ = 2
BIZ = 4
end
flags = Foo::BAR | Foo::BAZ # flags = 3
增加了2021-01-17
如果您正在传递枚举值(例如,将其存储在数据库中),并且需要能够将值转换回符号,那么两种方法都可以混搭
COMMODITY_TYPE = {
currency: 1,
investment: 2,
}
def commodity_type_string(value)
COMMODITY_TYPE.key(value)
end
COMMODITY_TYPE[:currency]
这个方法受到andrew-grimm的答案https://stackoverflow.com/a/5332950/13468的启发
我也建议阅读这里剩下的答案,因为有很多方法来解决这个问题,它实际上归结为你关心的其他语言的枚举