Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
Last active December 10, 2015 13:28

Revisions

  1. JoshCheek revised this gist Jan 3, 2013. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions ripper_again_yalls.rb
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,7 @@
    require 'ripper'

    Ripper::SexpBuilder.instance_methods.grep(/error/i) # => [:on_alias_error, :on_assign_error, :on_class_name_error, :on_param_error, :on_parse_error]

    class DemoBuilder < Ripper::SexpBuilder
    instance_methods.each do |meth_name|
    next unless meth_name =~ /^on_/
  2. JoshCheek created this gist Jan 3, 2013.
    18 changes: 18 additions & 0 deletions ripper_again_yalls.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    require 'ripper'

    class DemoBuilder < Ripper::SexpBuilder
    instance_methods.each do |meth_name|
    next unless meth_name =~ /^on_/
    super_meth = instance_method meth_name
    define_method meth_name do |*args|
    super_meth.bind(self).call(*args).tap do |result|
    puts "#{meth_name}(#{args.map(&:inspect).join(', ')})".ljust(50) << " #> #{result.inspect}"
    end
    end
    end
    end

    src = %(+)
    DemoBuilder.new(src).parse
    # >> on_op("+") #> [:@op, "+", [1, 0]]
    # >> on_parse_error("syntax error, unexpected $end") #> [:parse_error, "syntax error, unexpected $end"]