Ruby Scripting Question - Defining Functions
-
I am trying to streamline my code by putting my function calls into a loop. So far, I have been unsuccessful. Anyone care to give me a clue as to why?
#What I have UI.menu("PlugIns").add_item("Method 1") {method1} UI.menu("PlugIns").add_item("Method 2") {method2} UI.menu("PlugIns").add_item("Method 3") {method3} def method1 derp = "derp1" end def method2 derp = "derp2" end def method3 derp = "derp3" endThe above code creates three menu items called "Method 1", "Method 2" and "Method 3". Those entries call the corresponding methods created below them. What I would like to do is put all of that into a loop so I can create as many entries and methods as I want. Here is my rough attempt at that:
#What I want max = 3 for n in 1..max switchStr = "" UI.menu("PlugIns").add_item("Method " + n.to_s) { switchStr = "method" + n.to_s eval switchStr } def switchStr derp = "derp" + n.to_s end endNow, unfortunately the above code doesn't work. I am currently looking through the Ruby documentation to determine what exactly it is that I need (a proc perhaps?) however I thought I would take a shortcut and ask you. So, what am I doing wrong here? I feel like this should be simple and I'm just missing something. Thoughts?
-
Why not make one method that takes arguments
module James ###menu plugins=UI.menu("PlugIns") plugins.add_item("Some_Method 1"){self.some_method(1)} plugins.add_item("Some_Method 2"){self.some_method(2)} plugins.add_item("Some_Method 3"){self.some_method(3)} ### method[s] def self.some_method(arg=0) @derp="derp"+arg.to_s ### etc self.another_method() if arg > 0 end def self.another_method() UI.messagebox("You chose '"+@derp"' !") end end#modulePut your methods inside their own [your?] Module!
Called asJames.some_method(arg)
The value of 'arg' can be any type of class - here it defaults to0, and the.to_sassumes we want to convert it, whatever it is, into a string...
Note, makingderpinto@derpwill allow you to use it in other methods within the same module too ! -
I tried using your function and sadly, regards of which menu item I select ("Some_Method 1", "Some_Method 2" or "Some_Method 3") it always says "You chose 3". Any ideas why I am only getting the final index of the loop?
-
Without seeing your exact code I don't know...

-
The first line in TIG's example should read:
module JStroupand the last:
end #moduleHe typo'd ("method" instead of "module". And I'd use a more unique module name than just "James", but TIG's intent is that you always have your code run inside your own unique "Author" namespace.)
P.S.: This thread should be moved to the Developers forum.
-
@jrstoup said:
I tried using your function and sadly, ... it always says "You chose 3". Any ideas why I am only getting the final index of the loop?
It may work better if you pass the argument on to the
another_method(), like:
(Probably the cause is really the "module" typo as above.)require('sketchup.rb') module JStroup ### method[s] def self.some_method(arg=0) @derp="derp"+arg.to_s ### etc self.another_method(arg) if arg > 0 end def self.another_method(arg) UI.messagebox("You chose '#{arg.to_s}' !") end ### load once block unless file_loaded?(File.basename(__FILE__)) ### menu plugins=UI.menu("PlugIns") plugins.add_item("Some_Method 1"){self.some_method(1)} plugins.add_item("Some_Method 2"){self.some_method(2)} plugins.add_item("Some_Method 3"){self.some_method(3)} ### reqister file as loaded file_loaded(File.basename(__FILE__)) end end#module -
Sorry about the typo !
I've corrected the original post
[== a brain-fart - I must must type 'module' several times a day !]
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better π
Register LoginAdvertisement