(Improved ruby script implementation.) Tag: Visual edit |
(Improved script implementation.) Tag: Visual edit |
||
Line 1: | Line 1: | ||
− | = |
+ | = Comments: = |
⚫ | |||
Anything past the hash tag sign on the same line is a comment. |
Anything past the hash tag sign on the same line is a comment. |
||
⚫ | |||
− | # This is a comment |
||
⚫ | |||
Anything en-claused between the =begin and =end is also considered a comment. |
Anything en-claused between the =begin and =end is also considered a comment. |
||
+ | |||
+ | |||
=begin |
=begin |
||
This is comment |
This is comment |
||
Line 11: | Line 11: | ||
=end |
=end |
||
+ | |||
⚫ | |||
− | + | = Variables: = |
|
⚫ | |||
Local variables exist only within the current programming block. |
Local variables exist only within the current programming block. |
||
Line 19: | Line 20: | ||
name = "Captain Cuddles" # Strings are used for storing text. |
name = "Captain Cuddles" # Strings are used for storing text. |
||
− | + | == Instance Variables: == |
|
Instance variables (@my_var) are only accessible within the current body scope! |
Instance variables (@my_var) are only accessible within the current body scope! |
||
@my_var = 10 |
@my_var = 10 |
||
@my_other_var = "Hello" |
@my_other_var = "Hello" |
||
+ | |||
− | + | == Class Variables: == |
|
Class variables (@@my_var) are accessible within the all body scopes and controllers! |
Class variables (@@my_var) are accessible within the all body scopes and controllers! |
||
@@my_var = 10 |
@@my_var = 10 |
||
Line 40: | Line 42: | ||
getSetVar('count', 20) # <= returns 10 (the original value) and assigns 20. |
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. |
Global variables ($my_var) are accessible everywhere in the current SketchUp application. |
||
$my_var = 10 |
$my_var = 10 |
||
Line 52: | Line 55: | ||
number1 = "19837".to_i # Converts string (text) into an integer |
number1 = "19837".to_i # Converts string (text) into an integer |
||
+ | |||
number2 = "198.37".to_f # Converts text into a float/decimal/numeric. |
number2 = "198.37".to_f # Converts text into a float/decimal/numeric. |
||
− | # To combine the number with text we convert it to a string: |
+ | some_text = "Captain Slow has" + 19837.to_s + "peanuts" # To combine the number with text we convert it to a string: |
− | some_text = "Captain Slow has" + 19837.to_s + "peanuts" |
||
− | == Random |
+ | == Random Values: == |
''rand'' |
''rand'' |
||
+ | |||
Gives a random decimal value between 0 and 1. |
Gives a random decimal value between 0 and 1. |
||
+ | |||
''rand(n)'' |
''rand(n)'' |
||
+ | |||
Gives a whole (integer) random number between 0 and the specified number minus one. |
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. |
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/ |
|
[[Category:Tutorials]] |
[[Category:Tutorials]] |
Revision as of 19:31, 15 July 2014
Comments:
Anything past the hash tag sign on the same line is a comment. number = 10 # Assign digit 10 to a number.
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.
some_text = "Captain Slow has" + 19837.to_s + "peanuts" # To combine the number with text we convert 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/