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


I try to run this Code Snippet.


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:in call’

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

Kind regards

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 ?

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):

@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.)

paste error message here

ping @alexschreyer

Here’s the error:

#<ArgumentError: comparison of Length with nil failed>

Simplified example:

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


nil != 1.inch # => true


1.inch.nil? # => false

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.)

@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.

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…

@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'