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

    Using each_slice possible?

    Scheduled Pinned Locked Moved Developers' Forum
    10 Posts 2 Posters 986 Views 2 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.
    • F Offline
      Frankn
      last edited by

      I have a feeling this is a stupid question but I can't find the answer!

      I need to use each_slice in a plugin I'm working on but can't get it to work... I keep getting Error: #<NoMethodError: undefined method `each_slice' for #Array:0xc6c8c14>.

      Thanks in advance!

      Frank

      1 Reply Last reply Reply Quote 0
      • F Offline
        Frankn
        last edited by

        Thanks Dan!
        But I get this error now...
        Error: #<NoMethodError: undefined method `abs' for Math:Module>

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

          @frankn said:

          But I get this error now...
          Error: #<NoMethodError: undefined method `abs' for Math:Module>

          Fixed absolute call (in example above.)

          It is not a Math module function. It is a standard Ruby Numeric instance method.

          I just assumed the Math module would have an "abs" function. 😳


          The absolute call is to prevent an Index error:
          ary.slice!(0,-3) %(#004000)[Error: #<IndexError: (eval):0:inslice!': negative length (-3)>]`

          ❗

          I'm not here much anymore.

          1 Reply Last reply Reply Quote 0
          • F Offline
            Frankn
            last edited by

            Thanks Dan that works awesome!

            Now I'm having the same problem with delete_if! Isn't there an addon or file we can download to get the missing methods? If not how do you know figure out how to make new ones as you did? I don't want to have to bother anyone the next time I get a similar error if I can fix it myself.

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

              @frankn said:

              Now I'm having the same problem with "delete_if!"

              That is because the method's name is delete_if.

              There is no exclamation point name.

              You should refer to the documentation for Ruby v1.8.6 when programming for Ruby v1.8.6.
              RUBY PROGRAMMING REFERENCES

              I'm not here much anymore.

              1 Reply Last reply Reply Quote 0
              • F Offline
                Frankn
                last edited by

                @dan rathbun said:

                @frankn said:

                Now I'm having the same problem with "delete_if!"

                That is because the method's name is delete_if.

                There is no exclamation point name.

                I was actually using the ! as punctuation not in my code.

                BUT reading your post made me realize I had made typo somewhere else in my code causing the error.

                Thanks again for your help sir.

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

                  @frankn said:

                  Isn't there an addon or file we can download to get the missing methods?

                  There may be a "Backport" gem for Ruby v1.8.6 that adds later methods.

                  But you would need to convince all users to install it. It also may not have ALL the new methods in later versions (such as v1.9.x or v2.0.x.)

                  @frankn said:

                  If not how do you know figure out how to make new ones as you did?

                  I do not know how to answer that. Many many years experience.

                  Read as many books on Ruby as you can.

                  Keeping trying and failing, until you gain success ?

                  I'm not here much anymore.

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

                    Here is another edition that will collect the slices into an output array, IF a block is NOT given. (Sort of like the opposite to flatten().)

                    If a block is given, it returns an array of the block's return values.

                    Prints to $stdout only if $VERBOSE == true.

                    def slicer(ary,num)
                      a2 = ary.dup
                      len = num.to_i.abs
                      out = []
                      until (s = a2.slice!(0,len)).empty?
                        puts(s.inspect) if $VERBOSE
                        if block_given?
                          out << yield(s)
                          puts( out.last.inspect ) if $VERBOSE
                        else
                          out << s
                        end
                      end # until
                      return out
                    end # slicer()
                    

                    πŸ’­

                    EDIT: fixed puts'ing block results

                    I'm not here much anymore.

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

                      EDIT: fixed puts'ing block results in the 2nd example.

                      I'm not here much anymore.

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

                        You get a NoMethodError because Enumerable does not have that method defined in Ruby 1.8.x.

                        This should do the same, except you pass the array in as the 1st argument:

                        def slicer(ary,num)
                          a2 = ary.dup
                          len = num.to_i.abs
                          until (s = a2.slice!(0,len)).empty?
                            if block_given?
                              yield(s) 
                            else
                              puts(s.inspect)
                            end
                          end # until
                        end # slicer()
                        

                        See another example that will collect the slices into an output array, IF a block is NOT given. (Sort of like the opposite to flatten().) If a block is given, it returns an array of the block's return values. Prints to $stdout only if $VERBOSE == true.

                        πŸ’­

                        Fixed absolute call. It is not a Math module function. It is a standard Ruby Numeric instance method. 😳

                        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