It’s been almost two years since I stopped my apprenticeship in Ruby and today I want to resume.
I am a beginner so please be lenient with me.
I want to send a window with a message only once if one or more components are present in my selection.
mod = Sketchup.active_model
sel = mod.selection
sel.each do |s|
if s.is_a? Sketchup::ComponentInstance
UI.messagebox("A component and present in the selection!")
else
UI.messagebox("No component is present in your selection!")
end
end
This method works but the problem is that it sends the message several times if several components are in my selection.
So how can I send the message only once even if several components are in my selection?
mod = Sketchup.active_model
sel = mod.selection
# Array of instances or empty Array
instances = sel.grep(Sketchup::ComponentInstance)
if instances.size > 0
UI.messagebox("#{instances.size} component(s) present in the selection!")
else
UI.messagebox("No component is present in your selection!")
end
2.Can you lay the code in the form of a method like in my example and not in a single line?
Because I am not yet able to transform a one-line code into a method with def and end.
3.How do you call the one-line code and the code with def and end?
Thank you very much for your help and links.
The solution was not so easy for a beginner like me because your method works with arrays and attributes in the definitions.