"Where" you should define your classes and methods is, again, up to you to decide.
If you define it as simply
class ReferenceendThat means if anyone also defined their own class as "Reference", you could potentially run into problems with conflicting definitions and behaviors.If you define it ias
module XRef class Reference endendThere is usually no confusion because you can explicitly refer to it as XRef::Reference, and the chances of there being name conflicts is much lower. Of course, if someone wanted to call their module XRef and they also placed their "Reference" class in there...There are technical reasons why one would use a module, but when it comes to
module SHAZ module XRef class Reference end endendI think it's purely for organization and to avoid name collisions (cause I'm sure no one's going to go and call their module SHAZ).
def ref(type, id = nil)
setup(type, id)
# ^ is it good or bad to call this here?
# If an element doesn't exist, it'll create it, which will let the return value always be an array, even if it's empty
# Classes calling this then just need to check for .empty? and not .nil? as well
@data[type] if id.nil?
@data[type][id]
end
I don't think there is anything wrong with making sure the data is initialized, but it means each time you call the ref method, you're going to call the initialization, which is sort of redundant after you call it once.
Which sort of contradicts my usual practice since I always say things like
Code:
load_notetag_something unless @somethingreturn @something
But that's mainly because of loading-from-save issues...