In Python, this is not much better:
# How to get the name of the current method? # Add this snippet of code to your logic somewhere module Kernel private # Defined in ruby 1.9 unless defined?(__method__) def __method__ caller[0] =~ /`([^']*)'/ and $1 end end end
import tracebackdef asdf(): (filename,line_number,function_name,text)=traceback.extract_stack()[-1] print function_name asdf()
Update: __method__ is part of Ruby as of 1.8.7.
Here is something else rather awkward in Ruby:
In Python, we could simply do this:
# Print all modules (excluding classes) puts Module.constants.sort.select {|x| eval(x.to_s).instance_of? Module}
import sys; print sys.modules.keys()Ruby's introspection (also) reveals a lot about the structure of classes.
No comments:
Post a Comment