--Enclose the character string in quotation marks ('or "). Also, always describe the quotation marks in half-width characters.
--It is necessary to leave a half-width space after puts.
--Comment out by writing "#" at the beginning of the line
--Variables must be used only after the variables have been defined (variable name = value). (Because the program is executed in order from the top)
--Variable names can be decided freely, but there are some naming rules in Ruby. In particular, use an underscore (_) when assigning a variable name that combines two or more words.
--There is a way to include the value of the variable in the string. By using # {variable name} in the character string, the variable can be replaced with the assigned value and included in the character string. This is called "variable expansion".
--Variable expansion is only possible for strings that use double quotes. Note that in the case of single quotation, variable expansion is not performed and it is output as a character string as it is.
--Since the array is also one value, it can be assigned to a variable. By convention, variable names that assign arrays are often plural.
-Use the each statement for iterative processing. By using each statement, the elements of the array can be extracted in order and processed. Each statement is "array.each do |Variable name|, And write the process you want to execute before "end". Each statement iterates as many times as there are elements in the array. The elements of the array are entered into the variables enclosed in "|" one by one, and the processing in each statement is executed on that. The variable name (name) in each statement can be given any name, but it is customary to use the singular form of the variable name (names) of the array.
--The range in which each variable can be used is called the scope.
--As shown in the figure below, the hash can be written with a colon ":" at the beginning instead of a character string. This way of writing ": name" is called a symbol. A symbol is a way of writing a character with a ":" at the beginning instead of enclosing it in "" "or"'". Strings and symbols are strictly different, but they can basically be used in the same way.
--When using a symbol for the hash key, you can omit it. Specifically, ": key =>" can be omitted as "key:". Even if it is abbreviated, the key is a symbol, so when getting an element, it is necessary to use a symbol like puts user [: name].
--In an instance method, "self" refers to the instance itself.
--You need an explicit receiver to call the method. In the case of a class method, self is specified as the receiver.
--Unique value for each instance: Instance variable Common value for each instance: Class variable
Recommended Posts