It looks like you're new here. If you want to get involved, click one of these buttons!
Two questions:
1) How do I call one .lym script from another one? I tried the following: Make one .lym script with this inside:
module Test
include RBA
p "Hello World"
end
and another .lym script with this inside:
module Test2
include RBA
Test
end
When I run "Test" I get "Hello World". When I run "Test2" I get nothing..
2) Same question, but how do I pass values to the "child" script and receive a result back from the "child" script?
Thanks!
Comments
Hi David,
the "module" is just a namespace, not a function that you can call. You can do the following:
I'd recommend to take a look into a good Ruby book to learn more about modules, functions etc.
Please note, that "load" can only load plain Ruby (text) files, not .lym files - they are a KLayout speciality.
Regards,
Matthias
Thanks. I thought "module" was KLayout specific but didn't realize it is from Ruby.
After reading your reply I realized how little Ruby I know and I was just flying by the seat of my pants.
So I read a Ruby book this weekend, almost in its entirety
and while I can't say I understood everything, at least now have a much better handle on things.
Quick note that may be helpful for future readers: To pass variables you do it the standard Ruby way (which I didn't know, but below are a few examples, hopefully it helps someone)
This is the calling .lym file:
And this is the .rb file that is being referred to. Saved as “NumberAdder.rb”
When you run the first script, the output should be:
Hi David,
thank you for the note.
Actually, defining classes and using them in the top-level context is even more the "Ruby-way" :-)
Matthias
This is working, to call .rb scripts.
However now I want to call an .lym script.
For example, I have one code that makes a button on the toolbar as shown next. Then I have a second .lym code (not shown) that I want to just run when the toolbar button is pressed.
This works fine if RunMe.rb is an .rb file. But if I'm trying to actuate an .lym file when I press the button and I change this:
to this:
(so, no parameters to pass this time -- I just want to run it), then it doesn't work. Is there a way to do this?
Lastly, how do I get rid of the non-portable filepaths such as
and replace it with some relative path? e.g. to the "macros" directory? I tried putting TheIcon.png in $: (where the macros directory is indeed listed for me) and $KLAYOUT_PATH, and changing the string to refer there, but am perhaps not using it right. Can you provide an example?
Thanks!
Hi David,
".lym" files are not native ruby, hence you cannot load them with Ruby's "load". The idea behind ".lym" is to provide meta-information for integration into the application. They are intended for the entry-point scripts and provide information about menu binding or the interpreter to use (once I support other languages beside Ruby).
Libraries or utility modules which are called from those script should be provided as plain Ruby files (*.rb) and can be loaded by the interpreter (i.e. through "load"). Such files are supported in the IDE as well, but can't be bound to a menu for example. That is an exclusive right of ".lym" which provides the meta-information for doing so.
To load a ruby file relative to "$:" you can just use "require 'file'" which will locate and load "file.rb". In order to use some file path relative to the current script's location, you can use "File.dirname($0)" as the directory for the file. $0 is the path of the script currently executed. Using the current script as for the base path is the most convenient method and allows you to store the auxiliary files along with the script.
Regards,
Matthias