Class: TokenString::Impl
- Inherits:
-
Object
- Object
- TokenString::Impl
- Defined in:
- lib/token_string.rb
Overview
The value type that implements the whole shebang.
Instance Attribute Summary collapse
-
#format ⇒ Object
readonly
Returns the value of attribute format.
-
#tokens ⇒ Object
readonly
Returns the value of attribute tokens.
Instance Method Summary collapse
-
#==(other) ⇒ Object
Two such strings should be equal independent of the representation.
-
#clone ⇒ Object
Clones.
-
#derive!(*args) ⇒ Object
(also: #derive)
Equal to calling clone.surround().
-
#force(new_format) ⇒ Object
Make this Impl a different type.
-
#initialize(format, tokens) ⇒ Impl
constructor
:nodoc:.
-
#lines ⇒ Object
Some string methods that may get called on us.
-
#postfix(*pf) ⇒ Object
Appends a token to the end of the tokens.
-
#postfix!(*pf) ⇒ Object
Equal to calling .clone.postfix(…).
-
#prefix(*pf) ⇒ Object
Appends a token to the beginning to the tokens.
-
#prefix!(*pf) ⇒ Object
Equal to calling .clone.prefix(…).
-
#surround(prefix_, *postfix_) ⇒ Object
Equal to calling prefix(<prefix>).postfix(<postfix>).
-
#to(new_format) ⇒ Object
“cast” this Imple to a different type.
-
#to_s ⇒ Object
Convert to string.
Constructor Details
#initialize(format, tokens) ⇒ Impl
:nodoc:
84 85 86 87 |
# File 'lib/token_string.rb', line 84 def initialize( format, tokens) # this should be called only from TokenString.from & TokenString.make @format, @tokens = format, tokens end |
Instance Attribute Details
#format ⇒ Object (readonly)
Returns the value of attribute format.
81 82 83 |
# File 'lib/token_string.rb', line 81 def format @format end |
#tokens ⇒ Object (readonly)
Returns the value of attribute tokens.
81 82 83 |
# File 'lib/token_string.rb', line 81 def tokens @tokens end |
Instance Method Details
#==(other) ⇒ Object
Two such strings should be equal independent of the representation
111 112 113 114 |
# File 'lib/token_string.rb', line 111 def ==(other) return false unless other other.tokens == tokens end |
#clone ⇒ Object
Clones
117 118 119 |
# File 'lib/token_string.rb', line 117 def clone Impl.new( format, tokens.map{|t|t}) end |
#derive!(*args) ⇒ Object Also known as: derive
Equal to calling clone.surround()
154 155 156 |
# File 'lib/token_string.rb', line 154 def derive!(*args) clone.surround( *args) end |
#force(new_format) ⇒ Object
Make this Impl a different type.
This method simply sets format to the given new format.
100 101 102 103 |
# File 'lib/token_string.rb', line 100 def force(new_format) @format = new_format self end |
#lines ⇒ Object
Some string methods that may get called on us
162 |
# File 'lib/token_string.rb', line 162 def lines; [to_s]; end |
#postfix(*pf) ⇒ Object
Appends a token to the end of the tokens
136 137 138 139 |
# File 'lib/token_string.rb', line 136 def postfix( *pf ) tokens.push(*TokenString.linearize_tokens(pf)) self end |
#postfix!(*pf) ⇒ Object
Equal to calling .clone.postfix(…)
142 143 144 |
# File 'lib/token_string.rb', line 142 def postfix!( *pf ) clone.postfix(*pf) end |
#prefix(*pf) ⇒ Object
Appends a token to the beginning to the tokens
125 126 127 128 |
# File 'lib/token_string.rb', line 125 def prefix( *pf ) tokens.unshift(*TokenString.linearize_tokens(pf)) self end |
#prefix!(*pf) ⇒ Object
Equal to calling .clone.prefix(…)
131 132 133 |
# File 'lib/token_string.rb', line 131 def prefix!( *pf ) clone.prefix(*pf) end |
#surround(prefix_, *postfix_) ⇒ Object
Equal to calling prefix(<prefix>).postfix(<postfix>)
147 148 149 150 151 |
# File 'lib/token_string.rb', line 147 def surround( prefix_, *postfix_ ) prefix( prefix_ ) postfix( *postfix_ ) unless postfix_.empty? self end |
#to(new_format) ⇒ Object
“cast” this Imple to a different type. These casts use the same underlying buffer to be cheap.
91 92 93 |
# File 'lib/token_string.rb', line 91 def to(new_format) Impl.new( new_format, tokens ) end |
#to_s ⇒ Object
Convert to string
106 107 108 |
# File 'lib/token_string.rb', line 106 def to_s TokenString.convert_tokens_to( @tokens, @format ) end |