Ruby errors in Sketchup 2021
-
I have an old Sketchup script (written in 2012) that I am trying to get working in SU 2021, which I know has a new version of Ruby:
Error: #<NoMethodError: undefined method 'nitems' for []:Array>
This error does not make sense to me, since the Array class does have a method called 'nitems'. I checked to make sure the array is not nil (which would have produced a different error).
Does anyone know what might be causing this error?
-
Unless we see the whole code who knows ?
Post it here [within 'code' tags], unless it's confidential...
Then post the relevant part...How are you using this 'nitems' - I've written hundreds of scripts and I've never felt the need to use it !
-
What's the plugin?
@spring.freediver said:
It can be reproduced with one line, similar to the example in the Ruby Documentation:
[1, 2, 3].nitems
This should return "3". Instead it returns:
Error: #<NoMethodError: undefined method `nitems' for [1, 2, 3]:Array>
-
It can be reproduced with one line, similar to the example in the Ruby Documentation:
[1, 2, 3].nitems
This should return "3". Instead it returns:
Error: #<NoMethodError: undefined method `nitems' for [1, 2, 3]:Array>
In the script, I am using it in an if statement to do different things, depending on how many items are in the array. Pretty straight forward.
I did a Google search to see if 'nitems' had been deprecated in this new version of Ruby, but didn't find anything.
-
The method
nitems
is deprecated...
There are alternatives...[1, 2, 3].each{|n| ### }
Should do it ?
But if you really want to limit the resultant array to just contain integers, a safe way would be...
[1, nil, 2, 3, 'banana'].compact.each{|n| next unless n.class==Integer; ### }
which should give
[1, 2, 3]
-
The plugin is GeoSketch (https://www.youtube.com/user/TacForgeGeoSketch/videos)
I wrote GeoSketch in the 2008-2012 timeframe. I hadn't used Sketchup since then, but had a need for GeoSketch recently. I purchased a license of SketchUp 2021 last week, so I could try to get GeoSketch running again. Most of it works, but I get this error in one method.
I can probably replace 'nitems' with 'size', but it won't address nils in the array. But I shouldn't have any.
I do find it curious that 'nitems' seems to be missing. As I replied to TIG, I did a Google search to see if it was deprecated, but didn't find anything.
-
Thank you TIG. I was not able to find that it was deprecated.
I can just use 'size' instead.
'nitems' was not about limiting the array to integers. It was simply to provide a count of non-nil items in the array.
By the way, I was using http://ruby-doc.com/docs/ProgrammingRuby/ for Ruby documentation, and it still lists nitems as a method of the Array class.
Can you recommend a better site for documentation of the version of Ruby Sketchup is now using?
-
array.compact
removes any 'nil
' elements in the returned array
or usearray.compact!
to actually remove them from the original array ! -
Just a quickie to note that an alternative which does not create a new array object is:
ary.count {|e| !e.nil? }
Also, as the
Enumerable
library module is mixed intoArray
, you can easily test fornil
members ...true if even 1 member is nil, false otherwise
ary.any?(&:nil?)
true if no nil members, false if even 1 is nil
ary.none?(&:nil?)
true if all members are nil, false if even 1 is not nil
ary.all?(&:nil?)
-
@spring.freediver said:
I was not able to find that it was deprecated.
It was more than deprecated. It was removed in Ruby 2.0 and higher (which is SketchUp 2014 and up.)
The CHANELOGs for the Ruby Core is at it's GitHub project repository.
(But the logs are not that easy to read. They often create "Breaking Changes" synopsis in the NEWS files.)
See: https://github.com/ruby/ruby/tree/master/doc@spring.freediver said:
By the way, I was using http://ruby-doc.com/docs/ProgrammingRuby/ for Ruby documentation, and it still lists
nitems
as a method of the Array class.Can you recommend a better site for documentation of the version of Ruby Sketchup is now using?
The ol' "Pick Axe" book you see online was written for Ruby 1.6 !
(Even the first version of SketchUp that came with an API used Ruby version 1.8.0.)Use the Ruby core documentation for the proper version:
https://ruby-doc.org/core-2.7.2/There is link at the top for the corresponding Standard Library docs that now also ships with SketchUp (since v2014.)
Also I have an extensive Ruby Resources list at the official SketchUp forum.
https://forums.sketchup.com/t/ruby-learning-resources-wikilists/22861/~ regards ~
Advertisement