我有这个代码:
#!/usr/bin/env ruby
#encoding: utf-8
require "csv"
class FileTypeEnum
channel=0
national=1
regional=2
end
class CsvParser
attr_accessor :row_hash, :file_path, :mode
def initialize(filePath, file_type_enum) #Client should only pass the legal values of file_type_enum
@file_path = filePath
@mode = file_type_enum #mode should be one of the 3 legal integer values corresponding to the enum
puts "CSV Parser received = #{filePath}"
csv = CSV.read("#{filePath}")
case @mode
when 0
parse_channel
when 1
parse_national
when 2
parse_regional
else
puts "Error in method invocation"
end
end#initialize
这就是我通过谷歌搜索使枚举在 Ruby 中工作的方法,因为没有原生枚举类。
这是我想要完成的
1) any code that instantiates CsvParser must only be able to pass the legal values for the parameter "file_type_enum"
2) Can someone give an example of code of How I can retrieve the integer value inside initialize from the enum parameter and set mode.
谢谢,