Ruby 2 Variables and Assignment

To store a number or a string in your computer's memory for use later in your program, you need to give the number or string a name. Programmers often refer to this process as assignment and they call the names variables. A variable springs into existence as soon as the interpreter sees an assignment to that variable.
  1. s = 'Hello World!'  
  2. x = 10  
bareword is any combination of letters, numbers, and underscores, and is not qualified by any symbols (Referencehttp://alumnus.caltech.edu/~svhwan/prodScript/avoidBarewords.html). Local variables and barewords look similar; they must start with either the underscore character (_) or a lowercase letter, and they must consist entirely of letters, numbers, and underscores. Remember, local variable references look just like method invocation expressions and Keywords can't be used as variable names.

Method calls can also be barewords, such as my_method. gets is a method call; so is system. Whenever Ruby sees a bareword, it interprets it as one of three things: (a) If there's an equal sign (=) to the right of the bareword, it's a local variable undergoing an assignment. (b) Ruby has an internal list of keywords and a bareword could be a keyword.(c) If the bareword is not (a) or (b) above, the bareword is assumed to be a method call. If no method by that name exists, Ruby raises a NameError.
The p004stringusage.rb shows us some more usage with strings.
  1. # p004stringusage.rb  
  2. # Defining a constant  
  3. PI = 3.1416  
  4. puts PI  
  5. # Defining a local variable  
  6. my_string = 'I love my city, Pune'  
  7. puts my_string  
  8. =begin  
  9.   Conversions  
  10.   .to_i, .to_f, .to_s  
  11. =end  
  12. var1 = 5;  
  13. var2 = '2'  
  14. puts var1 + var2.to_i  
  15. # << appending to a string  
  16. a = 'hello '  
  17. a<<'world. 
  18. I love this world...'  
  19. puts a  
  20. =begin  
  21.   << marks the start of the string literal and  
  22.   is followed by a delimiter of your choice.  
  23.   The string literal then starts from the next  
  24.   new line and finishes when the delimiter is  
  25.   repeated again on a line on its own. This is known as  
  26.   Here document syntax.  
  27. =end  
  28. a = <<END_STR  
  29. This is the string  
  30. And a second line  
  31. END_STR  
  32. puts a  
In the example:
x = "200.0".to_f
the dot means that the message "to_f" is being sent to the string "200.0", or that the method to_f is being called on the string "200.0". The string "200.0" is called the receiver of the message. Thus, when you see a dot in this context, you should interpret it as a message (on the right) being sent to an object (on the left).