SketchyPhysicsWiki
Advertisement

General code

Comments:

Anything past the hash tag sign on the same line is a comment.

  1. This is a comment

number = 10 # Assign

Anything en-claused between the =begin and =end is also considered a comment. =begin This is comment ... =end

Variables:

Local Variables:

Local variables exist only within the current programming block.

number1 = 10 # A simple integer number2 = 112.3412 # Decimal or float as they are called name = "Captain Cuddles" # Strings are used for storing text.

Instance Variables:

Instance variables (@my_var) are only accessible within the current body scope! @my_var = 10 @my_other_var = "Hello"

Class Variables:

Class variables (@@my_var) are accessible within the all body scopes and controllers! @@my_var = 10 @@my_other_var = "Hello"

You may also use getVar('var_name'), setVar('var_name', value). setVar('count', 20) getVar('count') # <= returns 20

The only difference between class variables and getVar is that an undefined class variable will raise an exception, but an undefined getVar('var_name') will simply return nil. It is recommended to use getVar('var_name') when implementing data between the controllers, as they won't raise any errors.

Another method is getSetVar('var_name', value). What it does is returns original values and assigns the new value. setVar('count', 10) getSetVar('count', 20) # <= returns 10 (the original value) and assigns 20.

Global Variables:

Global variables ($my_var) are accessible everywhere in the current SketchUp application. $my_var = 10 $my_other_var = "Hello"

It is recommended to minimize the use of global variables to follow the Ruby scripting rules.


Combinations:

Now if we want to combine text and numbers we need some type conversions, but those are no problem in Ruby.

number1 = "19837".to_i # Converts string (text) into an integer number2 = "198.37".to_f # Converts text into a float/decimal/numeric.

  1. To combine the number with text we convert it to a string:

some_text = "Captain Slow has" + 19837.to_s + "peanuts"


Random values

rand Gives a random decimal value between 0 and 1.

rand(n) Gives a whole (integer) random number between 0 and the specified number minus one. For instance, rand(10) will return a whole number between 0 and 9.

See Also [1] [2]

Advertisement