Ruby errors in Sketchup 2021

Ruby errors in Sketchup 2021

Postby spring.freediver » Mon Feb 01, 2021 6:20 pm

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?
0

spring.freediver 
 

Re: Ruby errors in Sketchup 2021

Postby TIG » Mon Feb 01, 2021 7:41 pm

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 !
0
TIG
User avatar
TIG 
Global Moderator
 

Re: Ruby errors in Sketchup 2021

Postby spring.freediver » Mon Feb 01, 2021 8:46 pm

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.
0
Last edited by spring.freediver on Mon Feb 01, 2021 9:01 pm, edited 1 time in total.

spring.freediver 
 

Re: Ruby errors in Sketchup 2021

Postby Gus R » Mon Feb 01, 2021 9:00 pm

What's the plugin?

spring.freediver wrote: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>
0
User avatar
Gus R 
 

Re: Ruby errors in Sketchup 2021

Postby TIG » Mon Feb 01, 2021 9:02 pm

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]
0
TIG
User avatar
TIG 
Global Moderator
 

Re: Ruby errors in Sketchup 2021

Postby spring.freediver » Mon Feb 01, 2021 9:11 pm

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

spring.freediver 
 

Re: Ruby errors in Sketchup 2021

Postby spring.freediver » Mon Feb 01, 2021 9:17 pm

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?
0

spring.freediver 
 

Re: Ruby errors in Sketchup 2021

Postby TIG » Mon Feb 01, 2021 11:01 pm

array.compact removes any 'nil' elements in the returned array
or use array.compact! to actually remove them from the original array !
0
TIG
User avatar
TIG 
Global Moderator
 

Re: Ruby errors in Sketchup 2021

Postby Dan Rathbun » Fri Apr 23, 2021 6:38 pm

spring.freediver wrote: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 wrote: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-lear ... sts/22861/

~ regards ~
0
Last edited by Dan Rathbun on Fri Aug 06, 2021 4:25 am, edited 1 time in total.
    I'm not here much anymore. But a PM will fire email notifications.
    User avatar
    Dan Rathbun 
    PluginStore Author
    PluginStore Author
     

    Re: Ruby errors in Sketchup 2021

    Postby Dan Rathbun » Wed Jul 14, 2021 8:36 pm

    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 into Array, you can easily test for nil 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?)
    0
      I'm not here much anymore. But a PM will fire email notifications.
      User avatar
      Dan Rathbun 
      PluginStore Author
      PluginStore Author
       

      SketchUcation One-Liner Adverts

      by Ad Machine » 5 minutes ago



      Ad Machine 
      Robot
       



       

      Return to Developers' Forum

      Who is online

      Users browsing this forum: buckethead381911 and 11 guests