Module: ByteObject
- Defined in:
- lib/ByteObject.rb
Overview
The ByteObject module adds several attribute methods to any class that includes it, to aid in writing programs that require manipulating bytes of specific size. It also has helper methods to make working with binary files easier.
Defined Under Namespace
Modules: ByteAttributes
Instance Method Summary collapse
-
#from_hash!(hash) ⇒ Object
Takes a hash (or hash-like object whose #each method yields a key-value pair) and assigns the values of its keys to instance variables with the same names.
Instance Method Details
#from_hash!(hash) ⇒ Object
Takes a hash (or hash-like object whose #each method yields a key-value pair) and assigns the values of its keys to instance variables with the same names. If there is an attribute writer method, it will attempt to use that; otherwise it will set the instance variable directly (much to the chagrin of whoever wrote the official Ruby documentation).
As the bang might suggest, this method is dangerous and modifies the calling object.
83 84 85 86 87 88 89 90 91 |
# File 'lib/ByteObject.rb', line 83 def from_hash!(hash) hash.each do |key, value| if methods.include?("#{key}=".to_sym) send("#{key}=", value) else instance_variable_set("@#{key}", value) end end end |