Class: Code

Inherits:
Object
  • Object
show all
Defined in:
lib/crokus/code.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(str = nil) ⇒ Code

Returns a new instance of Code.



5
6
7
8
9
# File 'lib/crokus/code.rb', line 5

def initialize str=nil
  @lines=[]
  (@lines << str) if str
  @indent=0
end

Instance Attribute Details

#indentObject

Returns the value of attribute indent.



3
4
5
# File 'lib/crokus/code.rb', line 3

def indent
  @indent
end

#linesObject

Returns the value of attribute lines.



3
4
5
# File 'lib/crokus/code.rb', line 3

def lines
  @lines
end

Instance Method Details

#<<(thing) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/crokus/code.rb', line 11

def <<(thing)
  if (code=thing).is_a? Code
    code.lines.each do |line|
      @lines << " "*@indent+line.to_s
    end
  elsif thing.is_a? Array
    thing.each do |kode|
      @lines << kode
    end
  elsif thing.nil?
  else
    @lines << " "*@indent+thing.to_s
  end
end

#finalizeObject



26
27
28
29
# File 'lib/crokus/code.rb', line 26

def finalize
  return @lines.join("\n") if @lines.any?
  ""
end

#newlineObject



35
36
37
# File 'lib/crokus/code.rb', line 35

def newline
  @lines << " "
end

#save_as(filename, verbose = true, sep = "\n") ⇒ Object



39
40
41
42
43
# File 'lib/crokus/code.rb', line 39

def save_as filename,verbose=true,sep="\n"
  str=self.finalize
  File.open(filename,'w'){|f| f.puts(str)}
  return filename
end

#sizeObject



45
46
47
# File 'lib/crokus/code.rb', line 45

def size
  @lines.size
end

#to_sObject



31
32
33
# File 'lib/crokus/code.rb', line 31

def to_s
  finalize
end