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 end
File 2
module MyModule def bar() foo("Hello World!") end end
and 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
Advertisement