Issue with passing arguments

Well now that explains a lot of why you are having so much trouble.

If you look at my Tutorials list in the Resources wikis, you’ll see I strongly discourage it’s use in big red typeface.

Martin was a Java programmer, that just did not really understand Ruby’s paradigms.
He often proposed that SketchUp should abandon Ruby and use Java in a long debate topic where the Rubyists had to defend it’s use from others who proposed various other scripting languages.

Anyway, several times he posted and proposed modifying the global ObjectSpace for specific utility, not understanding his code was modifying everything. I called him on this several times. As far as I know he never corrected his online tutorial to “play nice” with SketchUp’s shared Ruby process. And this is why I added the warning.

I think that now is the time to remove it from the tutorial resource list.


Listen, I am moving on. I don’t want any more to do with this or any edition of that codebase.

I have arthritis in my hands and all this typing is about problems that would not occur if ya fully read a good book on Ruby like the ol’ Pick Axe book, and get a grasp of the basic fundamentals of Ruby.

Unfortunately, this is not one of the first books you should read. It is similar to Martin’s work in that Matthew does not teach coding that is correct for a shared Ruby environment. It really needs an update.

The module concept is the 2nd thing I teach after describing how Ruby is object oriented.

It is just the way the Ruby interpreter is coded to work.

Also I do not use them myself and many other do not.
I use blocks of # lines. Most good code editors can collapse contiguous comment lines.

You are not selecting it. That is something for the end user to do manually.

With the API all you need to do is reference an object. The visible selection that the user sees is not needed for manipulating model objects with the API.

So, again, if you wish to find (and reference) an object by a string name, use the searching statements I gave above.

def m(*args)
  model = Sketchup.active_model
  ents = model.active_entities
  if args[0].is_a?(String)
    instset = ents.grep(ComponentInstance)
    # search for the 1st instance whose name is args[0]
    inst = instset.find {|i| i.name == args[0] }
    if inst # an instance was found
      # use this inst
    else
      # search for the 1st instance whose definition name is args[0]
      inst = instset.find {|i| i.definition.name == args[0] }
      if inst # an instance was found
        # use this inst
      end
    end
  else # args[0] is some other type
    # ... etc ...
  end

  # ... etc ...

end # method m()

ADD: You can add in a conditional test if you still want to use the visible selection,
whether the inst reference found is actually selected in the viewport …

   if model.selection.include?(inst)

well I do still appreciate the help you have given me.
Enjoy
Brad

I don’t mean to beat ya’ up, but these are the same issue debates I had with Martin, Matthew and numerous others (newbs and gurus both) beginning about 2009. I get weary of retyping these over and over.

1 Like

Dan
No worries. I did not mean to re-open old arguments. I really am trying to learn this stuff. I came across Martin’s and Mathew’s books and both looked comprehensive and focused on ruby for sketchup. So I am reading them and trying their examples. They may well be outdated but they are easier to follow then trying to puzzle out example code such as that in the GitHub tutorials when my knowledge base is currently at preschool levels.

You provided much more help with your explanations and enabled me to get things working again when I was quite stumped.

I understand about not wanting to keep repeating the same lessons and debates but that is the territory of someone who teaches. And arthritis is not a joke, I know.

I will continue my learning efforts and would welcome your help. Especially if you know of a current tutorial on Ruby specific to Sketchup that starts at the elementary and progresses thru advanced.

If not, that’s fine too.

Thanks again for your help to date.

Brad Smith