Class: BinStruct::String
- Inherits:
-
Object
- Object
- BinStruct::String
- Extended by:
- Forwardable
- Includes:
- LengthFrom, Structable
- Defined in:
- lib/bin_struct/string.rb
Overview
This class mimics regular String, but it is Structable.
Constant Summary
Constants included from LengthFrom
Instance Attribute Summary collapse
-
#static_length ⇒ Integer
readonly
String static length, if set.
-
#string ⇒ ::String
readonly
Underlying Ruby String.
Instance Method Summary collapse
-
#<<(str) ⇒ self
Append the given string to String.
-
#format_inspect ⇒ ::String
Format String when inspecting from a Struct.
-
#initialize(options = {}) ⇒ String
constructor
A new instance of String.
-
#initialize_copy(_orig) ⇒ void
Initialize object on copying: * duplicate underlying Ruby String.
-
#read(str) ⇒ self
(also: #from_human)
Populate String from a binary String.
-
#static_length? ⇒ Boolean
Say if a static length is defined.
-
#sz_to_read ⇒ Integer
Size to read.
-
#to_s ⇒ ::String
(also: #to_human)
Generate binary string.
Methods included from LengthFrom
#initialize_length_from, #read_with_length_from
Methods included from Structable
Constructor Details
#initialize(options = {}) ⇒ String
Returns a new instance of String.
35 36 37 38 39 |
# File 'lib/bin_struct/string.rb', line 35 def initialize( = {}) register_internal_string(+'') initialize_length_from() @static_length = [:static_length] end |
Instance Attribute Details
#static_length ⇒ Integer (readonly)
String static length, if set
29 30 31 |
# File 'lib/bin_struct/string.rb', line 29 def static_length @static_length end |
#string ⇒ ::String (readonly)
Underlying Ruby String
26 27 28 |
# File 'lib/bin_struct/string.rb', line 26 def string @string end |
Instance Method Details
#<<(str) ⇒ self
Append the given string to String
84 85 86 87 |
# File 'lib/bin_struct/string.rb', line 84 def <<(str) @string << BinStruct.force_binary(str.to_s) self end |
#format_inspect ⇒ ::String
Format String when inspecting from a BinStruct::Struct
77 78 79 |
# File 'lib/bin_struct/string.rb', line 77 def format_inspect inspect end |
#initialize_copy(_orig) ⇒ void
This method returns an undefined value.
Initialize object on copying:
-
duplicate underlying Ruby String
44 45 46 |
# File 'lib/bin_struct/string.rb', line 44 def initialize_copy(_orig) @string = @string.dup end |
#read(str) ⇒ self Also known as: from_human
Populate String from a binary String. Limit length using LengthFrom or #static_length, if one is set.
51 52 53 54 55 |
# File 'lib/bin_struct/string.rb', line 51 def read(str) s = read_with_length_from(str) register_internal_string(s) self end |
#static_length? ⇒ Boolean
Say if a static length is defined
71 72 73 |
# File 'lib/bin_struct/string.rb', line 71 def static_length? !static_length.nil? end |
#sz_to_read ⇒ Integer
Size to read. Computed from #static_length or length_from
, if one defined.
63 64 65 66 67 |
# File 'lib/bin_struct/string.rb', line 63 def sz_to_read return static_length if static_length? old_sz_to_read end |
#to_s ⇒ ::String Also known as: to_human
Generate binary string
91 92 93 |
# File 'lib/bin_struct/string.rb', line 91 def to_s @string end |