Consider the following code:
x = 10 puts x
Here you assign the numeric value of 10 to a variable called x.
You can name variables however you like, with only a few limitations.
Variable names
The following table demonstrates variable names that are valid and invalid.
Variable Name | Valid Or Invalid? |
---|---|
x | Valid |
y2 | Valid |
_x | Valid |
7x | Invalid (starts with a digit) |
this_is_a_test | Valid |
this is a test | Invalid (not a single word) |
this'is@a'test! | Invalid (contains invalid characters: ', @, and !) |
this-is-a-test | Invalid (looks like subtraction) |
x = 100 y = 10 # from w w w . j av a 2 s.c om puts x - y x = 50 y = x * 100 x += y puts x