Module-class
-
HI, guys,
I need HELP!
I have written my first method (±70 lines) that works very well.
Now I want to wrap it in a module and a class.
(And later a start-operation & commit. And finally a begin-rescue...I guess)
But adding a - class Name -before everything and -end- as last line breaks it all.
I'm trying to find good readings about all this.
Meanwhile here are a few simple lines of it. They give me an "undefined method" error.
After all that work and studying for weeks, I am so disappointed that these few lines give me a headache !
-
You need to call the whole 'path' to the method:
McF3D::McF3DTools.new()
AND also add into theinitialize()method [which is called by 'new']
[at the end]
perceOuv() -
@tig said:
You need to call the whole 'path' to the method:
McF3D::McF3DTools.new()
AND also add into theinitialize()method [which is called by 'new']
[at the end]
perceOuv()Many thanks for your answer,
Heu... but very sorry. Still too abstract for me.
Tried many ways to implementMcF3D::McF3DTools.new(), but cannot figure out where it goes.
Every scripts I open for reference has a different way of about class.
very tired. -
I found and still find it confusing, so it good to create a really simple example...
try each separately or run all at once...
puts 'hi' #################### def my_hi puts 'hi' end my_hi # call #################### class My_Hi def my_hi3 puts 'hi' end end My_Hi.new.my_hi3 # call #################### module JcB class My_Hi def my_hi4 puts 'hi' end # my_hi end # My_Hi end # JcB JcB;;My_Hi.new.my_hi4 # call #################### module JcB def self.my_hi5 puts 'hi' end # my_hi end # JcB JcB;;my_hi5 # call or JcB.my_hi5and there are other variations on the theme...
john
-
Mario,
Glad that helped.
@mariocha said:
is the
def Initializea must ?Technically, no. But, if there's no need for it, one might ask the question 'should this be a module?' The answer would vary amongst programmers, and isn't dependent on that single criteria.
[EDIT] I should add that when you create a class, it is a Ruby Class object, which has a :new method, which calls initialize if it exists. Just in case you were wondering...
Greg
-
Hey, great ! Now I get it. (slapping my forehead)

Thanks a lot.
On tostart_operation - commitnow. update: that was an easy one.
Oh btw, is thedef Initializea must ? -
Mario,
This might help (I reformatted your code a bit), see the last couple of lines --
# require 'sketchup.rb' # why? not needed for this code module McF3D class McF3DTools def initialize @mod = Sketchup.active_model # Open model @ent = @mod.entities # All entities in model @sel = @mod.selection # Current selection end def perceOuv unless @sel[0].is_a?(Sketchup;;ComponentInstance) && @sel[0].definition.behavior.cuts_opening? UI.messagebox "Sélectionnez un composant F3D !" end end end # class McF3DTools end # module McF3D obj = McF3D;;McF3DTools.new() obj.perceOuv()The topic of modules & classes in programming is very complex. Conversely, what most people writing a SketchUp extension need to do is a very small subset of that topic.
HTH,
Greg
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