Class: NRSER::Rash::Writer
Instance Attribute Summary collapse
-
#out ⇒ Object
Returns the value of attribute out.
Instance Method Summary collapse
- #block(start = nil, finish = nil, &body) ⇒ Object
- #dedent ⇒ Object
- #indent(&b) ⇒ Object
-
#initialize(options = {}) ⇒ Writer
constructor
A new instance of Writer.
-
#write(obj) ⇒ Object
add the indent, unless it's called with a newline (
puts
calls write twice for every line, with the second call being "\n").
Constructor Details
#initialize(options = {}) ⇒ Writer
Returns a new instance of Writer.
211 212 213 214 215 216 217 218 |
# File 'lib/nrser/rash/util.rb', line 211 def initialize( = {}) = {:out => $stdout, :spacer => ' '}.merge @spacer = [:spacer] @level = 0 @new_line = true @out = [:out] super [:out].fileno end |
Instance Attribute Details
#out ⇒ Object
Returns the value of attribute out.
220 221 222 |
# File 'lib/nrser/rash/util.rb', line 220 def out @out end |
Instance Method Details
#block(start = nil, finish = nil, &body) ⇒ Object
234 235 236 237 238 239 240 |
# File 'lib/nrser/rash/util.rb', line 234 def block(start = nil, finish = nil, &body) [start].flatten.each {|line| puts line } if start indent do yield end [finish].flatten.each {|line| puts line} if finish end |
#dedent ⇒ Object
230 231 232 |
# File 'lib/nrser/rash/util.rb', line 230 def dedent @level -= 1 end |
#indent(&b) ⇒ Object
222 223 224 225 226 227 228 |
# File 'lib/nrser/rash/util.rb', line 222 def indent(&b) @level += 1 if b yield dedent end end |
#write(obj) ⇒ Object
add the indent, unless it's called with a newline (puts
calls
write twice for every line, with the second call being "\n")
244 245 246 247 248 249 250 251 |
# File 'lib/nrser/rash/util.rb', line 244 def write(obj) if @new_line && @level > 0 super indent_string end str = obj.to_s @new_line = (str[-1] == "\n") super str.lines.to_a.join(indent_string) end |