Class: Proc

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

Instance Method Summary collapse

Instance Method Details

#to_astParser::AST::Node

Returns Proc AST.

Returns:

  • (Parser::AST::Node)

    Proc AST



93
94
95
96
97
# File 'lib/proc_to_ast/parser_gem.rb', line 93

def to_ast
  filename, linenum = source_location
  parser = ProcToAst::Parser.new
  parser.parse(filename, linenum)
end

#to_raw_source(highlight: false) ⇒ Object



110
111
112
113
114
115
116
117
118
# File 'lib/proc_to_ast/parser_gem.rb', line 110

def to_raw_source(highlight: false)
  source = to_ast.loc.expression.source.force_encoding("UTF-8")

  if highlight
    ProcToAst::Parser.highlight(source)
  else
    source
  end
end

#to_source(highlight: false) ⇒ String

Returns proc source code.

Parameters:

  • highlight (Boolean) (defaults to: false)

    enable output highlight

Returns:

  • (String)

    proc source code



101
102
103
104
105
106
107
108
# File 'lib/proc_to_ast/parser_gem.rb', line 101

def to_source(highlight: false)
  source = Unparser.unparse(to_ast)
  if highlight
    ProcToAst::Parser.highlight(source)
  else
    source
  end
end