Ruby - File for Enumerations

In C #, I use to create a file called "Enums.cs", and there all the Enums that my application will need are defined ... It seems to me that this is easier to do. Now, when I take Ruby, I read and choose the approach modulefor defining enums, because I can associate an int with a "word", for example:

module ContractType
  Undefined = 0
  Internship = 1
  CLT = 2
  Contractor = 4
end

Now, how newGuy.CurrentContractType = ContractType.Internshipdoes my User model expose a property of type ?!?

Do I provide a importmodule? extend? Or should I link to a file GlobalEnums.rbwhere all the listings are?

+4
source share
1 answer
. require GlobalEnums.rb , :
newGuy.currentContractType = ContractType::Internship

, Ruby :: ( ), .. , :: s:

module Foo
  module Bar
    class Baz
      Qux = "quux"
    end
  end
end

p Foo::Bar::Baz::Qux
# => "quux"

P.S. , , . Ruby snake_case. CamelCase, SCREAMING_CAMEL_CASE.

, , :

module MyApp
  module ContractType
    UNDEFINED = 0
    INTERNSHIP = 1
    CLT = 2
    CONTRACTOR = 4
  end
end

# Assuming this is somewhere inside the MyApp namespace...
new_guy.current_contract_type = ContractType::INTERNSHIP

, , Integer(n) , URI(str) Nokogiri::XML(str).

+2

Source: https://habr.com/ru/post/1627177/


All Articles