Error checking routine issue?
-
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!
-
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>
-
Thanks thomthom, I also just noticed I should have posted this in Developers' Forum ....my bad!
-
Moved it. Didn't notice it was in the Plugins section.
-
@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.)
-
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'sinclude?
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
-
And you can also create referenced
Range
objects:@valid_width = 2..12
@valid_width.class
Range
so..
return nil unless @valid_width === @width
-
Thanks Dan! for raking me over the coals.... I needed that ...ouch!
-
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 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!
Advertisement