Class: Enum

Inherits:
Object
  • Object
show all
Defined in:
enum/enum.rb

Overview

The Enum class allows for better handling of the states within a given scenario. This is often inherited when introducing different enums.

Author:

  • Joseph Beck

Direct Known Subclasses

GameStates, PacketType

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = 0) ⇒ NilClass

Initialize the given attribute. 0 is assigned if nil args.

Parameters:

  • (Integer)


43
44
45
# File 'enum/enum.rb', line 43

def initialize(attrs = 0)
  @attrs = attrs
end

Class Method Details

.enum_attr(name, num) ⇒ NilClass

Create an enum attribute and create methods for each attribute. Each attribute can be checked whether it is true for that instance of the attribute. Additionally they can be changed to another type of attribute.

Parameters:

  • (String, Integer)

Returns:

  • (NilClass)


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'enum/enum.rb', line 19

def self.enum_attr(name, num)
  name = name.to_s

  define_method(name + '?') do
    @attrs & num != 0
  end

  define_method(name + '=') do |set|
    if set
      @attrs |= num
    else
      @attrs &= ~num
    end
  end
end

Instance Method Details

#to_iInteger

Convert the value of the attribute on integer.

Parameters:

  • (NilClass)

Returns:

  • (Integer)


52
53
54
# File 'enum/enum.rb', line 52

def to_i
  @attrs
end