Ruby learning points (basic)

--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

Ruby learning points (basic)
Ruby learning 4
Ruby learning 3
Ruby learning 2
Ruby learning 6
Ruby learning 1
Ruby basic terms
ruby basic syntax memo
Java learning memo (basic)
Java basic learning content 7 (exception)
Ruby on rails learning record -2020.10.03
Ruby on rails learning record -2020.10.04
Ruby on rails learning record -2020.10.05
Ruby on rails learning record -2020.10.09
Java basic learning content 5 (modifier)
Ruby on rails learning record-2020.10.07 ②
[Ruby] List of basic commands
Ruby on rails learning record-2020.10.07 ①
Ruby on rails learning record -2020.10.06
Ruby methods and classes (basic)
Java Basic Learning Content 8 (Java API)
Ruby on Rails Basic Memorandum
Java basic learning content 4 (repetition)
Learning Ruby with AtCoder 6 [Contest 168 Therefore]
Basic knowledge of Ruby on Rails
Java basic learning content 3 (operator / ternary operator)
Java basic learning content 9 (lambda expression)
Java basic learning content 2 (array / ArrayList)
With ruby ● × Game and Othello (basic review)
Learning Ruby with AtCoder 7 [Contest 168 Triple Dots]
Rails Tutorial Chapter 0: Preliminary Basic Knowledge Learning 5
Basic cheat sheet by language (Ruby, PHP)
Java basic learning content 1 (literals, variables, constants)