sketchucation logo sketchucation
    • Login
    ๐Ÿ›ฃ๏ธ Road Profile Builder | Generate roads, curbs and pavements easily Download

    Detect a Dimmension object without .typename

    scheduled pinned locked moved Developers' Forum
    22 Posts 7 Posters 716 Views 7 Watching
    loading-more-posts
    • oldest-to-newest
    • newest-to-oldest
    • most-votes
    reply
    • reply-as-topic
    guest-login-reply
    deleted-message
    • thomthomT Offline
      thomthom
      last edited by

      @unknownuser said:

      on a 100 lines each with a dimension

      0.003

      
      > Sketchup.active_model.entities.reject {|x| x.class==Sketchup;;Drawingelement}
      > 
      

      0.007 - 0.014

      
      > Sketchup.active_model.entities.reject {|x| x.typename=="DimensionLinear"}
      > 
      

      Problem is, DimensionRadial is also a Sketchup::Drawingelement.

      What I'm doing now is do a entity.kind_of?(Sketchup::Drawingelement) && entity.typename == "DimensionLinear"
      That way the slow type checking is only used when there's a potential it's a dimension object.

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

      one-reply-to-this-post last-reply-time reply quote 0
      • AdamBA Offline
        AdamB
        last edited by

        ..and you know for certain Ruby evaluates left to right?
        ๐Ÿค“

        Developer of LightUp Click for website

        one-reply-to-this-post last-reply-time reply quote 0
        • thomthomT Offline
          thomthom
          last edited by

          @adamb said:

          ..and you know for certain Ruby evaluates left to right?
          ๐Ÿค“

          erhh..? no... I just assumed it did. ..it doesn't?

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

          one-reply-to-this-post last-reply-time reply quote 0
          • thomthomT Offline
            thomthom
            last edited by

            I actually thought all scripting/programming languages evaluated left to right. That short-circut logic as a fundamental design.

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

            one-reply-to-this-post last-reply-time reply quote 0
            • tbdT Offline
              tbd
              last edited by

              it evaluates left side, then right side and then the operation between.

              here is an example:

              
              1==1 && (p "me too";true)
              => true
              
              

              so in your example you will not see any speed improvement ๐Ÿ˜ž

              SketchUp Ruby Consultant | Podium 1.x developer
              http://plugins.ro

              one-reply-to-this-post last-reply-time reply quote 0
              • thomthomT Offline
                thomthom
                last edited by

                I didn't understand that example. But I did a test:

                
                def always_return_false
                	puts 'call always_return_false'
                	return false
                end
                
                def always_return_true
                	puts 'call always_return_true'
                	return true
                end
                
                
                def test1
                	if always_return_false && always_return_false
                		#...
                	end
                end
                
                def test2
                	if always_return_true && always_return_true
                		#...
                	end
                end
                
                

                When I run the code:

                
                >> test1
                call always_return_false
                nil
                >> test2
                call always_return_true
                call always_return_true
                nil
                
                

                In the first case when the first check returns false it doesn't trigger the second check. I can't understand anything else than .kind_of? would do the same.

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

                one-reply-to-this-post last-reply-time reply quote 0
                • tbdT Offline
                  tbd
                  last edited by

                  my mistake, i was wrong. if left side is false the right side doesn't get evaluated

                  SketchUp Ruby Consultant | Podium 1.x developer
                  http://plugins.ro

                  one-reply-to-this-post last-reply-time reply quote 0
                  • J Offline
                    Jim
                    last edited by

                    @unknownuser said:

                    if left side is false the right side doesn't get evaluated

                    This is my understanding also, but watch because the and operator is not the same as &&. I don't think and will shortcut.

                    Hi

                    one-reply-to-this-post last-reply-time reply quote 0
                    • thomthomT Offline
                      thomthom
                      last edited by

                      @jim said:

                      but watch because the and operator is not the same as &&. I don't think and will shortcut.

                      I did two more tests for this:

                      
                      def test3
                      	if always_return_false and always_return_false
                      		#...
                      	end
                      end
                      
                      def test4
                      	if always_return_true and always_return_true
                      		#...
                      	end
                      end
                      
                      

                      Results:

                      
                      >> test3
                      call always_return_false
                      nil
                      >> test4
                      call always_return_true
                      call always_return_true
                      nil
                      
                      

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

                      one-reply-to-this-post last-reply-time reply quote 0
                      • thomthomT Offline
                        thomthom
                        last edited by

                        Found what the difference between and and && is:

                        @unknownuser said:

                        The binary "and" operator will return the logical conjunction of its two operands. It is the same as "&&" but with a lower precedence

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

                        one-reply-to-this-post last-reply-time reply quote 0
                        • AdamBA Offline
                          AdamB
                          last edited by

                          @thomthom said:

                          Found what the difference between and and && is:

                          @unknownuser said:

                          The binary "and" operator will return the logical conjunction of its two operands. It is the same as "&&" but with a lower precedence

                          Well that explains something that I've been bitten by a few times..

                          mask = mask or object.getmask
                          

                          assigns mask to itself and ors with the results of object.getmask()!! ๐Ÿ˜ฎ

                          I end up having to do:

                          mask = (mask or object.getmask)
                          

                          What kind of madman would introduce such an operator?

                          Actually the one that beats all for sheer insanity is ruby.h #define-ing fopen() to be something completely different calling some Ruby thing. What the!, Argggh...

                          I've got some harsh language for Mr.Ruby when/if I meet him.

                          Adam

                          Developer of LightUp Click for website

                          one-reply-to-this-post last-reply-time reply quote 0
                          • thomthomT Offline
                            thomthom
                            last edited by

                            I think I've always used && and || so I've avoided such problems. But I have had unexpected behaviour when I used not instead of !. I used not some times simply because I thought it was the same thing - but not would read better.

                            Looking at the table of Operator Precedence I can see how it all fits together now. http://www.techotopia.com/index.php/Ruby_Operator_Precedence

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

                            one-reply-to-this-post last-reply-time reply quote 0
                            • AdamBA Offline
                              AdamB
                              last edited by

                              Sure, it lists them out.

                              But I see absolutely no compelling reason to have "Logical AND" differing from "Logical composition" wrt precedence.

                              What is the 'use case' for the 2 forms? Does anyone know?

                              Adam

                              Developer of LightUp Click for website

                              one-reply-to-this-post last-reply-time reply quote 0
                              • tbdT Offline
                                tbd
                                last edited by

                                @adamb said:

                                What is the 'use case' for the 2 forms? Does anyone know?

                                maybe to play jokes like this :

                                
                                myvar = true and false
                                => false
                                myvar
                                => true
                                
                                

                                SketchUp Ruby Consultant | Podium 1.x developer
                                http://plugins.ro

                                one-reply-to-this-post last-reply-time reply quote 0
                                • R Offline
                                  RickW
                                  last edited by

                                  @adamb said:

                                  I end up having to do:

                                  mask = (mask or object.getmask)
                                  

                                  But now you know you can use

                                  mask = mask || object.getmask
                                  

                                  It saves typing the parentheses... (FWIW)

                                  RickW
                                  [www.smustard.com](http://www.smustard.com)

                                  one-reply-to-this-post last-reply-time reply quote 0
                                  • thomthomT Offline
                                    thomthom
                                    last edited by

                                    Saves you typing even more if you type mask ||= object.getmask ๐Ÿ˜‰

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

                                    one-reply-to-this-post last-reply-time reply quote 0
                                    • 1
                                    • 2
                                    • 1 / 2
                                    • first-post
                                      last-post
                                    Buy SketchPlus
                                    Buy SUbD
                                    Buy WrapR
                                    Buy eBook
                                    Buy Modelur
                                    Buy Vertex Tools
                                    Buy SketchCuisine
                                    Buy FormFonts

                                    Advertisement