Class: EacRubyUtils::Bit
Constant Summary collapse
- VALID_VALUES =
[0, 1].freeze
Instance Attribute Summary collapse
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(value) ⇒ Bit
constructor
A new instance of Bit.
- #one? ⇒ Boolean
- #to_i ⇒ Integer
Constructor Details
#initialize(value) ⇒ Bit
Returns a new instance of Bit.
33 34 35 |
# File 'lib/eac_ruby_utils/bit.rb', line 33 def initialize(value) @value = self.class.validate_integer(value) end |
Instance Attribute Details
#value ⇒ Object (readonly)
Returns the value of attribute value.
27 28 29 |
# File 'lib/eac_ruby_utils/bit.rb', line 27 def value @value end |
Class Method Details
.assert(obj) ⇒ Object
10 11 12 13 14 |
# File 'lib/eac_ruby_utils/bit.rb', line 10 def assert(obj) return obj if obj.is_a?(self) new(obj.to_i) end |
.valid_integer?(value) ⇒ Boolean
16 17 18 |
# File 'lib/eac_ruby_utils/bit.rb', line 16 def valid_integer?(value) value.is_a?(::Integer) && VALID_VALUES.include?(value) end |
.validate_integer(value) ⇒ Object
20 21 22 23 24 |
# File 'lib/eac_ruby_utils/bit.rb', line 20 def validate_integer(value) return value if valid_integer?(value) raise(::ArgumentError, "Invalid bit value: #{value} (Valid: #{VALID_VALUES})") end |
Instance Method Details
#to_i ⇒ Integer
43 44 45 |
# File 'lib/eac_ruby_utils/bit.rb', line 43 def to_i value end |