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.
- s = 'Hello World!'
- x = 10
A bareword is any combination of letters, numbers, and underscores, and is not qualified by any symbols (Reference: http://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.
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.
- # p004stringusage.rb
- # Defining a constant
- PI = 3.1416
- puts PI
- # Defining a local variable
- my_string = 'I love my city, Pune'
- puts my_string
- =begin
- Conversions
- .to_i, .to_f, .to_s
- =end
- var1 = 5;
- var2 = '2'
- puts var1 + var2.to_i
- # << appending to a string
- a = 'hello '
- a<<'world.
- I love this world...'
- puts a
- =begin
- << marks the start of the string literal and
- is followed by a delimiter of your choice.
- The string literal then starts from the next
- new line and finishes when the delimiter is
- repeated again on a line on its own. This is known as
- Here document syntax.
- =end
- a = <<END_STR
- This is the string
- And a second line
- END_STR
- 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).
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).