0
class A end class B end class Y < A end class Y < B # TypeError: superclass mismatch for Y end 

Is there a way to get the class the raises the superclass mismatch? I would like to know that it is class Y that causes it and hold that class in my bare variables.

2
  • Do you use a script to generate those classes? Could you post the relevant parts of it? Commented May 22, 2013 at 12:13
  • These classes can be found in files that are dynamically loaded and may chnge over time. So a superclass mismatch can occur if the source changed. Commented May 22, 2013 at 16:08

1 Answer 1

0
class A;end class B;end class Y < A;end x = (class Y < B;end) rescue $! p x.to_s #=> "superclass mismatch for class Y" 

Now from this string "superclass mismatch for class Y", you can get the class with:

Object.module_eval(x.to_s[/class (?<name>\S*)$/, 'name'], __FILE__, __LINE__) #=> Y 

(Taken from this SO answer)

Sign up to request clarification or add additional context in comments.

4 Comments

Continuing from @Priti's answer, you could get the class with Object.module_eval(x.to_s[/class (?<name>\S*)$/, 'name'], __FILE__, __LINE__)
@tessi please edit it on my answer. I am not strong on Regex, thus kept it blank. :)
@User no issue.. if you place it properly,you would get the result :)
I am working with maglev and named groups of regex do not work. Here is my code: github.com/knub/maglevrecord/blob/niccokunzmann/migration/lib/… Note that it does not work for nested classes.. but this is no issue for me :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.