api-issue-tracker icon indicating copy to clipboard operation
api-issue-tracker copied to clipboard

Sketchup::InstancePath#leaf does not mention when it returns nil

Open DanRathbun opened this issue 7 years ago • 4 comments

Sketchup::InstancePath#leaf does not mention when it returns nil.

#leaf is the compliment to #root which does mention the case when nil is returned.

Basically these are meant as double-duty test methods so that a compound typing test is not needed:

unless ipath.to_a.first.is_a?(Sketchup::Group) ||
ipath.to_a.first.is_a?(Sketchup::ComponentInstance)
  # root is not a complex object
else
  # all is fine
end

... and of course the opposite for #leaf.


Note: It seems some new coders are expecting #leaf to act like Array#last, and #root to act like Array#first. (Perhaps add #first and #last methods to the Sketchup::InstancePath class ?)

DanRathbun avatar Apr 11 '18 13:04 DanRathbun

When should #leaf return nil? Should a path ending at an instance (Group/Component) regard that to be the leaf? Is leaf only nil for empty paths?

Eneroth3 avatar Oct 06 '20 12:10 Eneroth3

Internally a leaf is a non-group/component. I think leaf will be nil when the path refer to a group/component only.

thomthom avatar Oct 07 '20 10:10 thomthom

Agree.

DanRathbun avatar Oct 07 '20 12:10 DanRathbun

Exactly thomthom Sorry i report a similar subject here : https://github.com/SketchUp/api-issue-tracker/issues/995

I did some tests, and actually leaf always returns nil when the last item of a instancePath is a group or a component

and this would also explain why we have always the error with drawing_element_visible?(instancePath) Leaf must be a type of Sketchup::Drawingelement

tomboul26 avatar Aug 23 '24 08:08 tomboul26