There are several ways to create string objects, most commonly using string literals (literals), a sequence of characters between a set of single or double quotes. The difference between the two forms is that when you construct a literal, Ruby differs in how much the string handles. Ruby has little to do with single quote strings. Except for the few exceptions. The value of this string is formed by typing into the literal amount of the string.
Ruby has more processing for double quote strings. First, it looks for a sequence that starts with a backslash and replaces them with a binary value. The most common one is \ n, which is replaced by a carriage return line feed. When a string output that contains a carriage return line break is enforced, \ n will force a newline.
Puts "and good night, \NGRANDMA"
Output results:
The second thing Ruby does with a double quote string is an expression interpolation within the string (expression interpolation), and the #{expression} sequence is replaced by the value of the expression. You can rewrite the previous method in this way.
def say_goodnight (name) result
= "good night,#{name}" return result end
puts Say_goodnight (' Pa ')
Output results:
When Ruby constructs this string object, it finds the current value of name and replaces it with a string. Any complex expression is allowed to be placed in the #{...} Structure. Here, the capitalize method defined in all strings is invoked, and the first letter of the parameter is capitalized after the output.
def say_goodnight (name) result
= ' good night,#{name.capitalize} ' return to result
puts Say_ Goodnight (' Uncle ')
Output results:
For convenience, you do not need to provide curly braces if the expression is just a global instance or a class variable.
$greeting = "Hello" # $greeting is a global variable
@name = "Prudence" # @name is an instance variable
puts "# $greeting, # @name
Output results:
This method can be further simplified. The value returned by the Ruby method is the value of the last evaluated expression, so you can remove both the temporary variable and the return statement.
def say_goodnight (name)
"Good Night,#{name}"
end
puts Say_goodnight (' Ma ')
Output results:
Ruby uses a naming convention to differentiate the use of names: the first character of the name shows how the name is used. Local variables, method parameters, and method names must start with lowercase letters or underscores. Global variables are prefixed by the dollar sign ($), and the instance variable begins with the "at" (@) symbol. The class variable starts with a two "at" (@@) symbol. Finally, class names, module names, and constants must start with an uppercase letter.
Starting with the initial character specified above, the name can be any combination of letters, numbers, and underscores (but the symbol following the @ symbol cannot be a number). However, by convention, instance variable names that contain multiple words use an underscore connection between words, and class variable names that contain multiple words use mixed capitalization (each single initial capitalization). Method name can? 、! and = Character end.