Is there something like Python __getattr__
for Ruby? In Python, obj.__getattr__
is called when an undefined attribute (method or property) is referenced. I found method_missing
in Ruby but it doesn't work for properties.
My use case is: I'm testing Mirah as a JRuby -> Java compiler. In standard JRuby, the Swing methods and attributes are "Rubyfied" (eg: label.setText "x" => label.text = "x"), but not in Mirah, because Mirah doesn't have a propietary runtime library like JRuby. Then, I want to get the attribute references in runtime and map each call to the corresponding Swing method (label.text => label.getText() / label.text = "x" => label.setText("x") ).
JRuby does this transformation in Java code, by walking the reflected class's methods and adding aliases for the Ruby names. You can do this from Ruby as well. Mirah has access to the actual java.lang.Class instances for all classes you're compiling against, and as a result could do this same name aliasing as part of the compiler phases.
The details on how exactly to do this are left as an exercise for the reader :)