Class: RubyScriptWriter

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

Direct Known Subclasses

RubySpecWriter

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRubyScriptWriter

Returns a new instance of RubyScriptWriter.



6
7
8
9
# File 'lib/rubyscriptwriter.rb', line 6

def initialize
  @r = StringIO.new
  @tab_count = 0
end

Instance Attribute Details

#rObject (readonly)

Returns the value of attribute r.



4
5
6
# File 'lib/rubyscriptwriter.rb', line 4

def r
  @r
end

Instance Method Details

#comment(*args) ⇒ Object



49
50
51
# File 'lib/rubyscriptwriter.rb', line 49

def comment(*args)
  puts "# ", *args
end

#indentObject



11
12
13
# File 'lib/rubyscriptwriter.rb', line 11

def indent
  @tab_count += 1
end

#outdentObject



15
16
17
# File 'lib/rubyscriptwriter.rb', line 15

def outdent
  @tab_count -= 1
end

#put_class(class_name, superclass_name = nil) {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



19
20
21
22
23
24
25
26
# File 'lib/rubyscriptwriter.rb', line 19

def put_class(class_name,superclass_name = nil)
  puts "class #{class_name}#{ superclass_name && " < #{superclass_name}"}"
  indent
  yield self
  outdent
  puts "end"
  puts
end

#put_coding(coding = 'utf-8') ⇒ Object



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

def put_coding(coding = 'utf-8')
  comment 'coding: ', coding
end

#put_method(method_name, *arguments) {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rubyscriptwriter.rb', line 32

def put_method(method_name,*arguments)
  if arguments.empty?
    puts "def ", method_name
  else
    puts "def ", method_name, "(", arguments.join(','), ")"
  end
  indent
  yield self
  outdent
  puts "end"
  puts
end

#put_simple_method(method_name, *method_code) ⇒ Object



28
29
30
# File 'lib/rubyscriptwriter.rb', line 28

def put_simple_method(method_name,*method_code)
  puts "def " + method_name + "; " + method_code.join + "; end"
end

#puts(*args) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/rubyscriptwriter.rb', line 53

def puts(*args)
  if args.empty?
    r.puts
    return self
  end
  args.compact!
  r.puts tabs + args.join unless args.empty?
  self
end

#tabsObject



63
64
65
# File 'lib/rubyscriptwriter.rb', line 63

def tabs
  "  " * @tab_count
end

#to_sObject



67
68
69
# File 'lib/rubyscriptwriter.rb', line 67

def to_s
  r.string
end