Codesnippet works in Console but not in "Ruby Code Editor" (Length#== bug)

bug
length
comparison

#1

Hi

I try to run this Code Snippet.

model=Sketchup.active_model
eye=model.active_view.camera.eye
ss=model.selection[0]
distance=eye.distance(ss.bounds.center)

When I copy&paste it into the Console then I get the distance between the Camera Eye to the Center of the Boundingbox of the selection.
But when I paste this code into the “Ruby Code Editor” an click on “run” then I get this Error in the Console:

Error: #<ArgumentError: comparison of Length with nil failed>
C:/Users/blablabla/AppData/Roaming/SketchUp/SketchUp 2016/SketchUp/Plugins/as_rubyeditor/as_rubyeditor.rb:270:in ==' C:/Users/blablabla/AppData/Roaming/SketchUp/SketchUp 2016/SketchUp/Plugins/as_rubyeditor/as_rubyeditor.rb:270:in!=‘
C:/Users/blablabla/AppData/Roaming/SketchUp/SketchUp 2016/SketchUp/Plugins/as_rubyeditor/as_rubyeditor.rb:270:in block in initialize' SketchUp:1:incall’

There must be some basic understanding I missed.
Does anybody know what I do wrong ?

Kind regards
Alain


#2

What version of the as_rubyeditor are you using ?
The EWH version is 3-2.0
That has no == in line #270 - where the error is being noted.
It actually says:
r!=nil ? r = r.to_s : r='Nil result (no result returned or run failed)'
Try with the latest version ?


#3

It’s Version 3.2 (at least in “as_rubyeditor.rb” there is that information).
The “About” Screen says something different (I guess this is a mistake by the Author):
http://pasteall.org/pic/index.php?id=112058


#4

@I_Alain_I When you post code, or error messages,
it must be wrapped in triple backticks or the forum engine
will swallow the error message (on the first line.)

```text
paste error message here
```


ping @alexschreyer


#5

Here’s the error:

#<ArgumentError: comparison of Length with nil failed>

Simplified example:

1.inch != nil
#<ArgumentError: comparison of Length with nil failed>

Yet

nil != 1.inch # => true

and

1.inch.nil? # => false

#6

Ah, okay yes that is an API bug in the Length class.

@tt_su @ChrisFullmer

The Length#!= method just calls the Length#== method (negating it,) which is overridden in the Length class, so it never gets passed up to Object#==.

The overridden method has missed some needed type checking.


(Moved to Ruby API category.)


#7

@I_Alain_I One quick way around the bug us to print out the last value. So just add

puts distance

As the last line in the editor.


#8

A workaround in the editor’s code would be to simply check if ‘r’ exists, then otherwise raise an error…
So rather than checking if ‘r’ is not equal to ‘nil’ etc, in the original code:
r!=nil ? r=r.to_s : r='Nil result (no result returned or run failed)'
it becomes the less convoluted:
r ? r=r.to_s : r='Nil result (no result returned or run failed)'
i.e. when ‘r’ exists at all we turn it into a string, otherwise when ‘r’ doesn’t exist [i.e. it’s ‘nil’ or ‘false’] we make ‘r’ into an error-message string…


#9

@jim_foltz: Thanks, it works.
@TIG: I replaced the line you suggested in the Editor’s Code but it returns an Error:

"undefined method `bounds' for nil:NilClass"
Error: #<TypeError: no implicit conversion of NoMethodError into String>
C:/Users/-/AppData/Roaming/SketchUp/SketchUp 2014/SketchUp/Plugins/as_rubyeditor/as_rubyeditor.rb:264:in `+'
C:/Users/-/AppData/Roaming/SketchUp/SketchUp 2014/SketchUp/Plugins/as_rubyeditor/as_rubyeditor.rb:264:in `rescue in block in initialize'
C:/Users/-/AppData/Roaming/SketchUp/SketchUp 2014/SketchUp/Plugins/as_rubyeditor/as_rubyeditor.rb:279:in `block in initialize'
-e:1:in `call'