Puts current module & method in ruby console
-
Hi all.
Sorry if you don't understand at all....
Since a long time I want to show complete method names when user starts one of my plugins.
I think I've found a solution, but not with native methods.Here is the method:
<span class="syntaxdefault">def get_current_method<br /> return </span><span class="syntaxkeyword">$</span><span class="syntaxdefault">1 if </span><span class="syntaxkeyword">/`</span><span class="syntaxdefault">(.*)'/.match(caller.first)<br />end</span>
And code I put in all my plugins:
<span class="syntaxdefault"></span><span class="syntaxkeyword">(</span><span class="syntaxdefault">self</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">to_s </span><span class="syntaxkeyword">!=</span><span class="syntaxdefault"> </span><span class="syntaxstring">'main'</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">?</span><span class="syntaxdefault"> puts</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"#{self}.#{get_current_method}"</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> puts</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">get_current_method</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span>
Do you have a solution to do the same with some native methods? Or perhaps you use another bit of code?
Thank you!
-
Is this for debugging only?
It is rather tedious to have to insert a debug line into each method.
-
Hi Dan.
No debugging, just the command of the running plugin. If you want to know command of a plugin, just open the ruby console and run the plugin by the menu or toolbar... An information. -
I'm not sure if I understand the question entirely, but it sounds like you just want to show the method that is called every time the user runs the plugin? Why don't you just puts it to the console? Lots of ways to do that. One could be to add it to the menu command. If you have something like this:
menu.add_item("My Plugin") { My_module::My_class.my_method }
Well, those braces can be expanded to as many lines of code as you would like. You could write it like this:
menu.add_item("My Plugin") do puts "My_module::My_class.my_method" My_module::My_class.my_method end
Then every time they run the plugin, it will echo the method to the console.
-
@matt666 said:
No debugging, just the command of the running plugin.
Then why are you asking for a way native Ruby methods ?
@matt666 said:
If you want to know command of a plugin, just open the ruby console and run the plugin by the menu or toolbar... An information.
Why not just open the rb in an editor, and find the command in the file ?
BTW.. sometimes there is good reason for a command to be kept private, a commercial plugin, or a Pro only plugin.
The writer of a plugin, has the right, to not allow their plugin (or it's tools,) to be called in ways that could defeat the license, or cause other problems.If the writer wants to publish the commands, they would do so in a README document that comes with the plugin.
-
Hi Dan and Chris. Thank you for your answers !
@chris said:
Well, those braces can be expanded to as many lines of code as you would like. You could write it like this:
Yes, but I wanted to have a automated method... no need to writeModule::Sub_module.method
, just copy a line that returns current method in the ruby console.@dan said:
Why not just open the rb in an editor, and find the command in the file ?
that's what I do when I search the command. But I am not sure a "non-ruby" user could find this command easily.@unknownuser said:
BTW.. sometimes there is good reason for a command to be kept private, a commercial plugin, or a Pro only plugin.
The writer of a plugin, has the right, to not allow their plugin (or it's tools,) to be called in ways that could defeat the license, or cause other problems.
I am not saying this bit of code MUST be inserting in each plugin, of course... Just for MY personal plugins, because I like transparency.. I just asked this forum if someone had a better (or a native) way to do like my little method...
Advertisement