• Login
sketchucation logo sketchucation
  • Login
⚠️ Libfredo 15.4b | Minor release with bugfixes and improvements Update

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 1 Mar 2012, 01:22

    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
    • T Offline
      thomthom
      last edited by 1 Mar 2012, 08:58

      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 1 Mar 2012, 18:06

        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
        • T Offline
          thomthom
          last edited by 1 Mar 2012, 18:35

          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
          • D Offline
            Dan Rathbun
            last edited by 1 Mar 2012, 19:37

            @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
            • D Offline
              Dan Rathbun
              last edited by 1 Mar 2012, 19:50

              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
              • D Offline
                Dan Rathbun
                last edited by 1 Mar 2012, 19:54

                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 1 Mar 2012, 19:56

                  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
                  • D Offline
                    Dan Rathbun
                    last edited by 1 Mar 2012, 20:01

                    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 1 Mar 2012, 20:15

                      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
                      1 / 1
                      • First post
                        1/10
                        Last post
                      Buy SketchPlus
                      Buy SUbD
                      Buy WrapR
                      Buy eBook
                      Buy Modelur
                      Buy Vertex Tools
                      Buy SketchCuisine
                      Buy FormFonts

                      Advertisement