MC logo


  Ruby Example Code

<<Ruby Hello, World! var1.rb Oh, I Meant Objects>>
# Variables and expressions.
a = 10
b = 3 * a + 2
printf("%d %d\n", a, b);

# Type is dynamic.
b = "A string"
c = 'Another String'
print b + " and " + c + "\n"
See:Ruby Manual

The variables shown in this example are "local variables". (We'll explore other possibilities later.)

Names are mostly conventional: letters, underscore, and digit, not starting with a digit. Local variables must start with a lower-case letter.

Variable types are dynamic: Assignment transfers both and type, not just value. Variables are not declared with a type, and assignment does not need to perform conversion.

Obviously, the + concatenates strings, as in Java and C++.

The printf function is lifted from plain C.
<<Ruby Hello, World! Oh, I Meant Objects>>