Class: RBM::Fragment

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

Constant Summary collapse

@@unnamed_fragment =
"fragment_0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code, name = nil, prerun = nil, postrun = nil) ⇒ Fragment

Returns a new instance of Fragment.



7
8
9
# File 'lib/rbm/fragment.rb', line 7

def initialize(code, name = nil, prerun = nil, postrun = nil)
  @code, @name, @prerun, @postrun = code, name, prerun, postrun
end

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.



5
6
7
# File 'lib/rbm/fragment.rb', line 5

def code
  @code
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/rbm/fragment.rb', line 5

def name
  @name
end

#postrunObject (readonly)

Returns the value of attribute postrun.



5
6
7
# File 'lib/rbm/fragment.rb', line 5

def postrun
  @postrun
end

#prerunObject (readonly)

Returns the value of attribute prerun.



5
6
7
# File 'lib/rbm/fragment.rb', line 5

def prerun
  @prerun
end

Instance Method Details

#run(bm, times, init = nil, cleanup = nil) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/rbm/fragment.rb', line 11

def run(bm, times, init = nil, cleanup = nil)
  fragment_name = (name || @@unnamed_fragment.succ!).gsub(/\s+/, "_")
  binding = Object.new.send(:binding)

  bm.report(name) do
    eval(init, binding, "#{fragment_name}_init", 0) if init
    eval(prerun, binding, "#{fragment_name}_prerun", 0) if prerun
    eval("#{times}.times { #{code} }", binding, "#{fragment_name}", 0)
    eval(postrun, binding, "#{fragment_name}_postrun", 0) if postrun
    eval(cleanup, binding, "#{fragment_name}_cleanup", 0) if cleanup
  end
end