Namespace question on noob level
-
@liquid98 said:
So every script should be in a double namespace ?? (Liquid and Name).. with or without loading other scripts ??
All your code needs to be within YOUR namespace (whatever you decide to call it.)
You will find that you wish to separate each of your plugins from one another. So each goes in it's own sub-module namespace, so they do not clash with each other.
Also in the "Plugins" directory.. your files need to go in YOUR "author" sub-directory.
You will want to maintain your plugins separately, so each should go in a sub-dir of your "author" directory.I suggest having the directory names and modules the same. It helps syncing up the
require
arguments with theinclude
orextend
arguments. -
Cooool! Thanx, I was hoping for this!
Global methods: just methods without any namespace? -- Refrain from using them, right?
So every script should be in a double namespace ?? (Liquid and Name).. with or without loading other scripts ??
What do you mean by:
'But never include() modules into the TOPLEVEL_BINDING, which is Object. ? '
EDIT:
require("liquid/useful.rb") include(Liquid::Useful)
Should be placed outside the modules!!Thnx
-
Thanx again Dan,
Really beautiful to see it running this way!!
This trick is not widely known according to my grep..Liquid
-
Hi Dan,
Thnx for the information. The next script I've written will have this namespace treatment!
By the way I found that calling :require("liquid/useful.rb") include(Liquid;;Useful)
only works outside the module definition..
edit: Other question: Is there some evil in using
load
instead ofrequire
? -
@liquid98 said:
By the way I found that calling :
require("liquid/useful.rb") > include(Liquid;;Useful)
only works outside the module definition..
NO.. This is not true, .. and Do not do this. If you do, you will add methods to EVERYONE's modules and classes, not just yours.
If you cannot get it to work, then you are doing something incorrect.
FYI: The mixin methods, in a mixin module, must be defined as instance methods, without a
self.
before the name. -
[off:2wuy0iss]
@liquid98 said:Other question: Is there some evil in using
load
instead ofrequire
?They do different things:
require()
checks the$LOADED_FEATURES
(aka$"
,) array and only loads the file if it has not already been loaded, and if it does (without error since v1.8.4, or so,) adds the filepath to that array. (Beware that v1.8.0 adds the filepath before the load attempt, so under Windows Sketchup < 8.x, that is still running v1.8.0, filepaths for failed files will exist in$"
.)load()
does not check$"
, always attempts to load the file, even it it was previously loaded, and does not push any filepaths into the$"
array.So.. we use
load()
during debugging, to re-load scripts, which will re-define methods with the new changes.After release...
require()
is used so libraries are only loaded once.[/off:2wuy0iss] -
@liquid98 said:
This trick is not widely known according to my grep..
I will need to disagree. It is not a "trick."
Mixin Library Modules, that are used with
include()
andextend()
, is basic Ruby (perhaps in the second semester course,) but still one of the fundamental principals of the dynamic nature built into Ruby.The problem is that most people are impatient, and refuse to read the Ruby textbook, to learn the fundamentals first. They read the first chapter and start hacking away.
My Question to YOU: Have YOU read the book yet ??
(I do not think you have given your questions.)It is in the "doc" folder of the the Full Ruby install, usually on Windows:
%(#004000)["C:/Ruby186/doc/ProgrammingRuby.chm"]
Also online at:
http://phrogz.net/ProgrammingRuby/frameset.html
Am I the only person in the world, who read this book cover-to-cover, before ever writing a single line of Ruby ??
Doing so saved me much frustration ! Although that book is old (... written in the v1.6.x days,) it is still valid, and I still refer to it often.
-
Dan,
No I haven't read the book yet. Though I scraped my information from several places, like this forum, google, other books, Rubydocs
'Automatic Sketchup', etc.In my point of view 90% of ruby scripting for sketchup is just using the Ruby API. The other 10 % is a set of rules cq best practices
to manage the code in a efficient way.Though I thank you for your suggestion, I have I complete folder of URL's with ruby books, to impressive to start with..
So a good starting point is welcome.Rene
-
@dan rathbun said:
@liquid98 said:
By the way I found that calling :
require("liquid/useful.rb") > > include(Liquid;;Useful)
only works outside the module definition..
NO.. This is not true, .. and Do not do this. If you do, you will add methods to EVERYONE's modules and classes, not just yours.
If you cannot get it to work, then you are doing something incorrect.
FYI: The mixin methods, in a mixin module, must be defined as instance methods, without a
self.
before the name.Hi Dan,
I just copy pasted your example into two files:
It works only ifrequire("useful.rb") include(Liquid;;Useful)
is outside the modules.
Can you please take a look??Thnx,
Rene
-
Ok got it.. you need to use a proxy class inside a module:
(1) The word useful has only 1 "L", by the way.
"Plugins/liquid/useful.rb"
module Liquid MENU = UI.menu('Plugins').add_submenu('Liquid') module Useful module_function def useful_1() msg = "I am method useful_1()." puts( msg ) return msg end def useful_2() msg = "I am method useful_1()." puts( msg ) return msg end def useful_3() msg = "I am method useful_1()." puts( msg ) return msg end end # Useful end # Liquid
"Plugins/liquid/fancy.rb"
require("liquid/useful.rb") module Liquid module Fancy class << self # proxy class include(Liquid;;Useful) end # proxy class useful_1() end # module Fancy end # module Liquid
But for a custom class, no proxy class is needed:
"Plugins/liquid/fancytool.rb"require("sketchup.rb") require("liquid/useful.rb") module Liquid module Fancy class FancyTool include(Liquid;;Useful) def activate() UI.messagebox( useful_1() ) Sketchup.active_model.select_tool(nil) end end # class unless file_loaded?('Liquid;;Fancy;;FancyTool') @@tool = FancyTool.new() Liquid;;MENU.add_item('FancyTool') { Sketchup.active_model.select_tool(@@tool) } file_loaded('Liquid;;Fancy;;FancyTool') end # unless end # module Fancy end # module Liquid
-
Off topic:
@unknownuser said:
(1) The word useful has only 1 "L", by the way.
thanx for your correction, the English language is not my mother language as you might have noticed..
/Off topicAll code is safe now and my code works like a charm, so thanx for your help. And yes I SHALL read the book before
I come here and ask questions like this..
Advertisement