Menu, require and .rb files
-
Hi guys,
I've read some posts about require and rb files.
So, I would like to know if is possible you have a menu that each sub item call a script or function inside differents .rb files? How to do this?
I have a main scritp and inside it there are a lot of function that can become smaller functions in separate files used by mora than one script.
Example: I have two rb files: file1.rb and file2.rb.
File1.rb do an action that use a function from file2.rb . But file2.rb can work alone too. So, I want to create a menu with two subitem to choose file1.rb or file2.rb.
This would be amazing! I could separate my functions and use them in several othes scripts, without copy the code lines.
Thanks -
If you set up each script as a module or a class you can call different aspects of the various methods it contains...
You can pass arguments likeMYtool.new(1)orMytool.new(2)and in the class'sdef initialize(opt=1)method of Mytool class you have a 'case' test 'opt 1' do this method and 'opt 2' do this other method etc...
With a module you'll end up with something like the Geom module and you would use it like that -Geom.Point2d.new(...)orGeom.Transformation.scaling(...)etc etc... -
Thankfully Ruby makes this simple, just wrap it all up in a module and you can split it over multiple files. For example...
File 1
module MyModule def foo(x) puts(x) end endFile 2
module MyModule def bar() foo("Hello World!") end endand by Ruby magik the call to bar works since it's in the same namespace.
-
We have topic for discussing Ruby Modules:
[talk] Using Ruby Modulesand an informational topic:
[info] Using Ruby Modules -
Thanks guys,
I'll try Ruby modules
SérgioBizello
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