There are many ways to create string objects, and the most commonly used one is probably using string literals, a sequence of characters between a set of single or double quotes. The difference between these two forms is that when constructing literals, Ruby differs in how much processing strings are done. Ruby handles very little single quote strings. With very few exceptions. Type the contents of the string literal form the value of the string.
RubyMore processing for double quote strings。 First, it looks for sequences that start with a backslash and replaces them with binary values. The most common of these is \n, which is replaced by a carriage return line break. When a string with a carriage return line break is output, \n forces a line break.
puts "And good night, \nGrandma"
Output result:
And good night, Grandma
The second thing Ruby does with double quoted strings is expression interpolation within the string, and the #{expression} sequence will be replaced by the value of the "expression". The previous method can be rewrited in this way.
def say_goodnight(name) result = "Good night,#{name}" return result end puts say_goodnight('Pa')
Output result:
Good night, Pa
When Ruby builds this string object, it finds the current value of name and replaces it into the string. Any complex expression is allowed to be placed in the #{...} structure. Here, the capitalize method defined in all strings is called, and the first letter of the parameter is changed to uppercase and output.
def say_goodnight(name) result = "Good night,#{}" return result end puts say_goodnight('uncle')
Output result:
Good night, Uncle
For convenience, curly braces are not required if the expression is just a global instance or class variable.
$greeting = "Hello" #$greeting is a global variable @name = "Prudence" #@name is an instance variable puts "#$greeting,#@name"
Output result:
Hello,Prudence
This method can be further simplified. The value returned by the Ruby method is the value of the last evaluation expression, so both the temporary variable and the return statement can be removed.
def say_goodnight(name) "Good night,#{name}" end puts say_goodnight('Ma')
Output result:
Good night, Ma
Ruby uses a naming convention to distinguish the purpose of a name: the first character of the name shows how the name is used. Local variables, method parameters, and method names must all start with lowercase letters or underscores. Global variables are prefixed with the dollar sign ($), while instance variables start with the "at" (@) sign. Class variables start with two "at" (@@) symbols. Finally, the class name, module name, and constant must all start with a capital letter.
Starting from the initial character specified above, the name may be any combination of letters, numbers and underscores (but the symbols following the @ symbol cannot be numbers). But by convention, instance variable names containing multiple words are connected with underscores between words, and class variable names containing multiple words are mixed case (each single-first capitalized). Is the method name OK? ,! and = characters end.