seattlerb / ruby_parser

ruby_parser is a ruby parser written in pure ruby. It outputs s-expressions which can be manipulated and converted back to ruby via the ruby2ruby gem.
http://www.zenspider.com/projects/ruby_parser.html
476 stars 100 forks source link

Cannot obtain super B of A #335

Closed n1ec closed 1 year ago

n1ec commented 1 year ago

Hi,Can you provide an example of getting ":super"?

require 'ruby_parser'
class SillyRubyParserExample
  def self.example
    class_eval(RubyParser.new.parse(<<-EOS
  class A < B
end
    EOS
    ).inspect)
  end
  def self.s(*args)
    p args
    if args[0] == :super
      puts "supr: " + args[1].to_s
    end
  end
end
SillyRubyParserExample.example
#output
[:const, :B]
[:class, :A, nil]
zenspider commented 1 year ago

I don't understand what you mean by 'getting ":super"'. The code you're parsing returns s(:class, :A, s(:const, :B)). There is no :super in there, so I'm confused.

What you're doing with the class_eval and inspect and your self.s method... I don't know.

zenspider commented 1 year ago
require 'ruby_parser'

class SillyRubyParserExample
  def self.example
    RubyParser.new.parse "class A < B; end"
  end
end

pp SillyRubyParserExample.example

output:

% ruby -Ilib bug335.rb
s(:class, :A, s(:const, :B))
zenspider commented 1 year ago

no response. closing.

n1ec commented 1 year ago
require 'ruby_parser'

class SillyRubyParserExample
  def self.example
    RubyParser.new.parse "class A < B; end"
  end
end

pp SillyRubyParserExample.example

Why isn't this B not super class?

My idea is to get B as super class I want to know that A inherits B

% ruby -Ilib bug335.rb
s(:class, :A, s(:super, :B))
zenspider commented 1 year ago

I already showed you:

s(:class, :A, s(:const, :B))
n1ec commented 1 year ago

Oh, sorry, I thought” :const”not :super