module ByteObject::ByteAttributes
Whenever ByteObject
is included into a class, it also extends that class with the ByteAttributes
module. This is the module that bestows the byte-specific attribute methods.
Public Instance Methods
attr_byte(key, size, signed)
click to toggle source
This method creates an ordinary attribute reader and a specialized attribute writer for the given key. The writer will clamp any values passed to the new attribute based on its size and whether or not it is signed. Under normal circumstances, you will not need to call this method yourself. It is used by this module to generate the attribute methods given to any extended classes. @param key [Symbol] The name of the attribute. @param size [Integer] The size, in bits, of the attribute. @param signed [Boolean] Whether or not the attribute can be negative. @return [void]
# File lib/ByteObject.rb, line 50 def attr_byte(key, size, signed) attr_reader(key) bytesize = (2**size) min = signed ? -bytesize/2 : 0 max = signed ? (bytesize/2) - 1 : bytesize - 1 define_method("#{key}=") do |val| instance_variable_set("@#{key}", val.clamp(min, max)) end end