sketchucation logo sketchucation
    • Login
    🤑 SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

    Error checking routine issue?

    Scheduled Pinned Locked Moved Developers' Forum
    10 Posts 3 Posters 116 Views 3 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.
    • T Offline
      tomot
      last edited by

      The following statement works!

      if (@width < 2) 
      UI.messagebox( "A Width of less than 2 is not allowed ")
      return nil
      end
      

      Question: How do I combine less than and more than into one statement using Ruby?
      The Ruby API is not very clear on this. Neither of the 2 statements below work!

      if (@width < 2 > 12)
      UI.messagebox( "A Width of less than 2 or greater than 12 is not allowed ")
      return nil
      end
      
      if ( 2 < @width > 12)
      UI.messagebox( "A Width of less than 2 or greater than 12 is not allowed ")
      return nil
      end
      

      Tia!

      [my plugins](http://thingsvirtual.blogspot.ca/)
      tomot

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

        Use the OR operator: ||

        <span class="syntaxdefault">if </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">width </span><span class="syntaxkeyword"><</span><span class="syntaxdefault"> 2 </span><span class="syntaxkeyword">||</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">width </span><span class="syntaxkeyword">></span><span class="syntaxdefault"> 12<br />  UI</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">messagebox</span><span class="syntaxkeyword">(</span><span class="syntaxdefault"> </span><span class="syntaxstring">"A Width of less than 2 or greater than 12 is not allowed "</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">  return nil<br />end</span>
        

        Link Preview Image
        Ruby Operators - Techotopia

        favicon

        (www.techotopia.com)

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

        1 Reply Last reply Reply Quote 0
        • T Offline
          tomot
          last edited by

          Thanks thomthom, I also just noticed I should have posted this in Developers' Forum ....my bad!

          [my plugins](http://thingsvirtual.blogspot.ca/)
          tomot

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

            Moved it. Didn't notice it was in the Plugins section.

            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

              @tomot said:

              Question: How do I combine less than and more than into one statement using Ruby?
              The Ruby API is not very clear on this.

              Nor would it be !

              The Google SketchUp Ruby API, consists of only 3 modules (and a lot of classes inside them.) Also a few additions to a couple of standard Ruby classes.

              This API, only extends standard Ruby, so we can work with Sketchup and it's models.

              You need to use the standard Ruby reference to understand what methods are available to the standard Ruby modules and classes.

              You need to read a good book on standard Ruby programming, to understand how to program in Ruby, before you can effectively know how to employ the SketchUp Ruby API's extensions. (This book is also distributed as a CHM, in the full Ruby install. See the "C:\Ruby186\doc" directory.)

              I'm not here much anymore.

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

                You can also use the between? method:

                unless @width.between?(2,12)
                  UI.messagebox( "A Width of less than 2 or greater than 12 is not allowed ")
                  return nil
                end
                

                .. or you could use a literal Range object, and it's include? method:

                unless (2..12).include?(@width)
                  UI.messagebox( "A Width of less than 2 or greater than 12 is not allowed ")
                  return nil
                end
                

                You can also use a Range object's === method:

                unless (2..12) === @width
                  UI.messagebox( "A Width of less than 2 or greater than 12 is not allowed ")
                  return nil
                end
                

                I'm not here much anymore.

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

                  And you can also create referenced Range objects:

                  @valid_width = 2..12

                  @valid_width.class

                  Range

                  so..

                  return nil unless @valid_width === @width

                  I'm not here much anymore.

                  1 Reply Last reply Reply Quote 0
                  • T Offline
                    tomot
                    last edited by

                    Thanks Dan! for raking me over the coals.... I needed that ...ouch! 😄

                    [my plugins](http://thingsvirtual.blogspot.ca/)
                    tomot

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

                      I just want you to understand where the information you need is at. (And that it is readily available.) Will save you a lot of grief.

                      Not a day goes by, without me opening both of these CHM references (they are both open right now, in fact, but minimized to the taskbar.)

                      I'm not here much anymore.

                      1 Reply Last reply Reply Quote 0
                      • T Offline
                        tomot
                        last edited by

                        I understand fully! and that's why your 1 of 3 of my favorite "SU Ruby API goto guys"
                        I also have to include TIG! & thomthom!

                        [my plugins](http://thingsvirtual.blogspot.ca/)
                        tomot

                        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