Class: BinStruct::OUI
- Includes:
- Structable
- Defined in:
- lib/bin_struct/oui.rb
Overview
OUI type, defined as a set of 3 bytes
oui = OUI.new
oui.from_human('00:01:02')
oui.to_human # => "00:01:02"
oui.to_s # => "\x00\x01\x03"
Constant Summary
Constants inherited from Struct
Instance Attribute Summary collapse
-
#b0 ⇒ Integer
Right-most byte.
-
#b1 ⇒ Integer
Center byte.
-
#b2 ⇒ Integer
Left-most byte.
Instance Method Summary collapse
-
#from_human(str) ⇒ self
Read a human-readable string to populate object.
-
#to_human ⇒ ::String
Get OUI in human readable form (colon-separated bytes).
Methods included from Structable
#format_inspect, #read, #sz, #to_s, #type_name
Methods inherited from Struct
#[], #[]=, attributes, #attributes, #bits_on, define_attr, define_attr_after, define_attr_before, define_bit_attrs_on, inherited, #initialize, #inspect, #offset_of, #optional?, #optional_attributes, #present?, #read, remove_attr, remove_bit_attrs_on, #sz, #to_h, #to_s, update_attr
Constructor Details
This class inherits a constructor from BinStruct::Struct
Instance Attribute Details
#b0 ⇒ Integer
Returns right-most byte.
28 |
# File 'lib/bin_struct/oui.rb', line 28 define_attr :b0, Int8 |
#b2 ⇒ Integer
Returns left-most byte.
22 |
# File 'lib/bin_struct/oui.rb', line 22 define_attr :b2, Int8 |
Instance Method Details
#from_human(str) ⇒ self
Read a human-readable string to populate object
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/bin_struct/oui.rb', line 34 def from_human(str) return self if str.nil? bytes = str.split(':') raise ArgumentError, 'not a OUI' unless bytes.size == 3 self[:b2].from_human(bytes[0].to_i(16)) self[:b1].from_human(bytes[1].to_i(16)) self[:b0].from_human(bytes[2].to_i(16)) self end |
#to_human ⇒ ::String
Get OUI in human readable form (colon-separated bytes)
48 49 50 |
# File 'lib/bin_struct/oui.rb', line 48 def to_human attributes.map { |m| '%02x' % self[m] }.join(':') end |