sketchucation logo sketchucation
    • Login
    ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

    Ruby errors in Sketchup 2021

    Scheduled Pinned Locked Moved Developers' Forum
    10 Posts 4 Posters 13.1k Views 4 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • S Offline
      spring.freediver
      last edited by

      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?

      1 Reply Last reply Reply Quote 0
      • TIGT Offline
        TIG Moderator
        last edited by

        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 !

        TIG

        1 Reply Last reply Reply Quote 0
        • Gus RG Offline
          Gus R
          last edited by

          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>

          www.instagram.com/gusrobatto/

          www.facebook.com/gusrobatto

          www.flickr.com/photos/gusrobatto

          1 Reply Last reply Reply Quote 0
          • S Offline
            spring.freediver
            last edited by

            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.

            1 Reply Last reply Reply Quote 0
            • TIGT Offline
              TIG Moderator
              last edited by

              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]

              TIG

              1 Reply Last reply Reply Quote 0
              • S Offline
                spring.freediver
                last edited by

                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.

                1 Reply Last reply Reply Quote 0
                • S Offline
                  spring.freediver
                  last edited by

                  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?

                  1 Reply Last reply Reply Quote 0
                  • TIGT Offline
                    TIG Moderator
                    last edited by

                    array.compact removes any ' nil' elements in the returned array
                    or use array.compact! to actually remove them from the original array !

                    TIG

                    1 Reply Last reply Reply Quote 0
                    • Dan RathbunD Offline
                      Dan Rathbun
                      last edited by

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

                      I'm not here much anymore.

                      1 Reply Last reply Reply Quote 0
                      • Dan RathbunD Offline
                        Dan Rathbun
                        last edited by

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

                        I'm not here much anymore.

                        1 Reply Last reply Reply Quote 0
                        • 1 / 1
                        • First post
                          Last post
                        Buy SketchPlus
                        Buy SUbD
                        Buy WrapR
                        Buy eBook
                        Buy Modelur
                        Buy Vertex Tools
                        Buy SketchCuisine
                        Buy FormFonts

                        Advertisement