SketchyPhysicsWiki
Advertisement

Comments:[]

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

number = 10 # Assign digit 10 to a number.

Anything included between the =begin and =end is also considered a comment. The =begin or =end must start in the first column.

=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 0. 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.

some_text = "Captain Slow has" + 19837.to_s + "peanuts" # To combine the number with text we converted it to a string.

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:[]

http://www.sketchup.com/intl/en/developer/

http://ruby-doc.org/core-1.8.6/ For SU2013 and below

http://ruby-doc.org/core-2.0.0/ For SU2014

Advertisement