Basic Ruby syntax and ruby syntax
There are multiple ways to create a String object. The most common method is to use the string literal (literals), that is, the character sequence between a group of single quotes or double quotes. The difference between the two forms is that Ruby processes strings differently when constructing a literal. Ruby rarely processes single quotes. Except a few exceptions. The literal content of the string constitutes the value of this string.
Ruby provides more processing for double quotes. First, it looks for the sequence starting with a backslash and replaces them with binary values. The most common one is \ n, which will be replaced by the carriage return line break. When a string output containing the carriage return linefeed, \ n forces a line break.
puts "And good night, \nGrandma"
Output result:
And good night,Grandma
The second thing Ruby does for double quotation marks is expression interpolation in the string. # {expression} is replaced by the value of "expression. You can use this method to override the previous method.
def say_goodnight(name) result = "Good night,#{name}" return resultendputs say_goodnight('Pa')
Output result:
Good night, Pa
When Ruby constructs this string object, it finds the current value of name and replaces it with the string. Any complex expressions can be placed in the # {...} structure. Here, the capitalize method defined in all strings is called, and the initial letter of the parameter is changed to uppercase and then output.
def say_goodnight(name) result = "Good night,#{name.capitalize}" return resultendputs say_goodnight('uncle')
Output result:
Good night, Uncle
For convenience, you do not need to provide curly brackets if the expression is just a global instance or class variable.
$ Greeting = "Hello" # $ greeting is the global variable @ name = "Prudence" # @ name is the 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 evaluated expression, so you can remove this temporary variable and return statement.
def say_goodnight(name) "Good night,#{name}"endputs say_goodnight('Ma')
Output result:
Good night, Ma
Ruby uses a naming convention to differentiate 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 start with lowercase letters or underscores. Global variables are prefixed with the dollar sign ($), and instance variables start with the "at" (@) symbol. The class variable starts with two "at" (@) symbols. Finally, the class name, Module name, and constant must start with an uppercase letter.
The name can be any combination of letters, numbers, and underscores (but the symbol following the @ symbol cannot be a number) starting from the initial character specified above ). However, by convention, the name of an instance variable that contains multiple words is connected by an underscore between the word and the word. The name of a class variable that contains multiple words is case-insensitive (uppercase for each single letter ). What is the method name? ,! And =.