Class: Code
- Inherits:
-
Object
- Object
- Code
- Defined in:
- lib/code.rb,
lib/template_code.rb
Instance Attribute Summary collapse
-
#code ⇒ Object
Returns the value of attribute code.
-
#indent ⇒ Object
Returns the value of attribute indent.
Instance Method Summary collapse
- #<<(str) ⇒ Object
- #finalize(dot = false) ⇒ Object
-
#initialize(indent = 0) ⇒ Code
constructor
A new instance of Code.
- #newline ⇒ Object
- #save_as(filename, verbose = true, sep = "\n") ⇒ Object
- #size ⇒ Object
Constructor Details
#initialize(indent = 0) ⇒ Code
Returns a new instance of Code.
5 6 7 8 |
# File 'lib/code.rb', line 5 def initialize indent=0 @code=[] @indent=indent end |
Instance Attribute Details
#code ⇒ Object
Returns the value of attribute code.
3 4 5 |
# File 'lib/code.rb', line 3 def code @code end |
#indent ⇒ Object
Returns the value of attribute indent.
3 4 5 |
# File 'lib/code.rb', line 3 def indent @indent end |
Instance Method Details
#<<(str) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/code.rb', line 10 def <<(str) if str.is_a? Code str.code.each do |line| @code << " "*@indent+line end elsif str.is_a? Array str.each do |kode| @code << kode end elsif str.nil? else @code << " "*@indent+str end end |
#finalize(dot = false) ⇒ Object
25 26 27 28 29 30 |
# File 'lib/code.rb', line 25 def finalize dot=false if dot return @code.join('\n') end @code.join("\n") if @code.any? end |
#newline ⇒ Object
32 33 34 |
# File 'lib/code.rb', line 32 def newline @code << " " end |
#save_as(filename, verbose = true, sep = "\n") ⇒ Object
36 37 38 39 40 41 |
# File 'lib/code.rb', line 36 def save_as filename,verbose=true,sep="\n" str=self.finalize File.open(filename,'w'){|f| f.puts(str)} puts "saved code in file #{filename}" if verbose return filename end |
#size ⇒ Object
43 44 45 |
# File 'lib/code.rb', line 43 def size @code.size end |