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

    TypeError: can't convert into Array

    Scheduled Pinned Locked Moved Developers' Forum
    15 Posts 4 Posters 364 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.
    • thomthomT Offline
      thomthom
      last edited by

      @jim said:

      I think Enumerable requires that your class implement a .each method. Some Enumerable methods also need a <=> method (comparison) defined.

      I do have an .each method implemented. But in any case would that matter for converting to an Array?

      I manually added an #to_a method to my that returns an array. So I don't understand why it raises this error...

      Thomas Thomassen — SketchUp Monkey & Coding addict
      List of my plugins and link to the CookieWare fund

      1 Reply Last reply Reply Quote 0
      • J Offline
        Jim
        last edited by

        You need to yield in your .each iterator.

        Something like this (not tested.)

        @entities.each{|e| yield(e)}

        See also: block_given?

        Here's a related article about the performance of passing blocks: http://mudge.github.com/2011/01/26/passing-blocks-in-ruby-without-block.html

        Hi

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

          @thomthom said:

          I manually added an #to_a method to my [class] that returns an array. So I don't understand why it raises this error...

          Is the superclass one of those API C++ collection classes ? Most of these give us problems when we try to subclass them.

          I'm not here much anymore.

          1 Reply Last reply Reply Quote 0
          • thomthomT Offline
            thomthom
            last edited by

            @jim said:

            You need to yield in your .each iterator.

            It does that.

            But I fail to see why .each got to do with converting to an array when I have a .to_a method ... ? I've been searching for info on it, but I cannot find anything.

            Thomas Thomassen — SketchUp Monkey & Coding addict
            List of my plugins and link to the CookieWare fund

            1 Reply Last reply Reply Quote 0
            • J Offline
              Jim
              last edited by

              @thomthom said:

              It does that.

              Sorry, I was looking at your latest bitbucket repo, which must not be your latest version.

              Hi

              1 Reply Last reply Reply Quote 0
              • thomthomT Offline
                thomthom
                last edited by

                No - it's still on my computer. A larger rewrite. The one at the repo was just an early framework WIP.

                Thomas Thomassen — SketchUp Monkey & Coding addict
                List of my plugins and link to the CookieWare fund

                1 Reply Last reply Reply Quote 0
                • thomthomT Offline
                  thomthom
                  last edited by

                  A simplified example:

                  <span class="syntaxdefault"></span><span class="syntaxkeyword">class&nbsp;</span><span class="syntaxdefault">Foo</span><span class="syntaxkeyword">;&nbsp;</span><span class="syntaxdefault">def&nbsp;to_a</span><span class="syntaxkeyword">;&nbsp;[</span><span class="syntaxdefault">1</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">2</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">3</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">4</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">5</span><span class="syntaxkeyword">];&nbsp;</span><span class="syntaxdefault">end</span><span class="syntaxkeyword">;&nbsp;</span><span class="syntaxdefault">end</span><span class="syntaxkeyword">;<br /></span><span class="syntaxdefault">nil<br /><br />f</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">Foo</span><span class="syntaxkeyword">.new<br /></span><span class="syntaxcomment">#<Foo;0x15259760><br /><br /></span><span class="syntaxdefault">x</span><span class="syntaxkeyword">=[</span><span class="syntaxdefault">3</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">4</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">5</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">6</span><span class="syntaxkeyword">]<br />[</span><span class="syntaxdefault">3</span><span class="syntaxkeyword">,&nbsp;</span><span class="syntaxdefault">4</span><span class="syntaxkeyword">,&nbsp;</span><span class="syntaxdefault">5</span><span class="syntaxkeyword">,&nbsp;</span><span class="syntaxdefault">6</span><span class="syntaxkeyword">]<br /><br /></span><span class="syntaxdefault">x&nbsp;</span><span class="syntaxkeyword">|&nbsp;</span><span class="syntaxdefault">f<br />Error</span><span class="syntaxkeyword">;&nbsp;</span><span class="syntaxcomment">#<TypeError;&nbsp;(eval);155;in&nbsp;`|';&nbsp;can't&nbsp;convert&nbsp;Foo&nbsp;into&nbsp;Array><br /></span><span class="syntaxkeyword">(eval);</span><span class="syntaxdefault">155<br /></span><span class="syntaxkeyword">(eval);</span><span class="syntaxdefault">155</span>
                  

                  Off course, x | f.to_a works, but since the error message indicate some conversion was tried, I wonder why the .to_a method wasn't used.

                  Thomas Thomassen — SketchUp Monkey & Coding addict
                  List of my plugins and link to the CookieWare fund

                  1 Reply Last reply Reply Quote 0
                  • J Offline
                    Jim
                    last edited by

                    use .to_ary although I don't have a good explanation yet.

                    class Foo
                      def to_ary
                        [1,2,3,4,5]
                      end
                    end
                    

                    This is as close to an explanation as I could find: http://stackoverflow.com/questions/766712/make-an-object-behave-like-an-array-for-parallel-assignment-in-ruby/768201#768201

                    Hi

                    1 Reply Last reply Reply Quote 0
                    • thomthomT Offline
                      thomthom
                      last edited by

                      Thanks!

                      301 Moved Permanently

                      favicon

                      (www.ruby-doc.org)

                      @unknownuser said:

                      Returns a new array. In the first form, the new array is empty. In the second it is created with size copies of obj (that is, size references to the same obj). The third form creates a copy of the array passed as a parameter (the array is generated by calling to_ary on the parameter). In the last form, an array of the given size is created. Each element in this array is calculated by passing the element’s index to the given block and storing the return value

                      So it appears what happens is not that f.to_a is called, but instead Array.new( f ) - and Array.new uses .to_ary .

                      Thomas Thomassen — SketchUp Monkey & Coding addict
                      List of my plugins and link to the CookieWare fund

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

                        @thomthom said:

                        So it appears what happens is not that f.to_a is called, but instead Array.new( f ) - and Array.new uses .to_ary .

                        Actually (if you use the CHM Ruby refdoc,) the source shows that the #| method explicitly calls:
                        %(#0000BF)[arg2 = to_ary(arg2);]
                        ie, it makes a copy of the second arg to use locally (within its body.)

                        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