Module: MethodAsCode

Included in:
Method, UnboundMethod
Defined in:
lib/as_code.rb

Instance Method Summary collapse

Instance Method Details

#as_code(indent = 0, name = nil) ⇒ Object



255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/as_code.rb', line 255

def as_code(indent=0, name=nil)
  sig = self.signature
  if self.body.respond_to?(:body) then
    # YARV
    body_expression = self.body.body.as_code(indent+1)
  else
    # pre-YARV
    body_expression = self.body ? self.body.as_code(indent+1) : ''
  end
  name ||= sig.name
  s = "#{'  '*indent}def #{name}(#{sig.param_list})\n"
  if body_expression then
    s += "#{body_expression}\n"
  end
  s += "#{'  '*indent}end"
  return s
end