========================================================== ==========================================
1. Can the methods in the Controller call each other?
2. The @ variable defined in the control method is not an instance variable. It seems that it can only be applied in the view corresponding to the Controller method and cannot be used by other views.
3. text_field and other value passing methods, and form value passing Methods
4. Relationship ing of activerecord
========================================================== ==========================================
Code in ruby: each statement is placed on a separate number of rows. You do not need to add a semicolon at the end of each sentence.
Ruby variables do not need to be declared in advance. You can assign values directly when using them, and determine the variable type based on the assigned type.
The ruby string contains expressions or variables using # {expression}. If it is a global variable or an instance variable, no curly brackets are required.
$ Greeting = "helllo"
@ Name = "Prudence"
Puts "# $ greeting, # @ name"
The method is expressed in the format of Def... end.
The value returned by the ruby method is the value of the last evaluated expression. return can be omitted.
Ruby uses a naming convention to differentiate the purpose of a name: the first character of the name shows how the name is used,
The local variables, method parameters, and method names must all Use lowercase letters or underscores. The global variables are prefixed with the dollar sign ($;
The Strength Variable starts with the at (@) symbol, and the class variable starts with two @ 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 name after the @ symbol cannot be a number. By convention, the instance variable names that contain multiple words are in the word and word
The class variable names that contain multiple words are case-insensitive. The method name can be? ,! , = Character ends.
Array: A = [1, 2, 3] a [0] = 1,
A = % wi am OK a [0] = "I" A [1] = "am" A [2] = "OK"
Hash: A = {"1" => 1, "2" => 2, "3" => 3} A ["1"]
Control statement: if it is only a simple if or while control statement, for example, there is only one sentence, you can write the expression first, and then write if or while
For example, puts "OK" If 3> 2
Ruby features: block and iterator
A block is a code block that can be associated with a method, almost the same as a parameter.
Block is just a set of code between curly braces and do... end.
{Puts "hello"} # This is a block
Or
Do
Puts "hello"
End
It is a Convention to use do... end for multiple rows and {} for a single row. Once a block is created, it can be associated with the square method. Block is placed at the end of the source code containing the method call.
And Method Association. For example, the following code:
Greet {puts 'Hi '}
If the method has parameters:
Greet ("Jim") {puts 'Hi '}
Using Ruby's yield statement, a method can call the associated block once or multiple times. It can imagine or compare block to a method call. It calls the block associated with the method of the yield statement.
Def call_block
Puts "starts of method"
Yield
Yield
Puts "End of method"
End
Call_block {puts "in the block "}
Result:
Starts of Method
In the block
In the block
End of Method
As you can see, the code in the block is called with the appearance of yield. Of course, we can also provide yield with parameters that will be passed to the block. In the block, vertical bars | are given between parameter names to accept these parameters from yield.
Def call_block
Yield ("hello", 99)
End
Call_block {| STR, num | ...}
The ruby library uses a large number of blocks to implement the iterator. The iterator is a method for returning elements from a collection, such as an array.
Animals = % W (ANT bee cat dog elk)
Animals. Each {| E | puts e}
After reading the use of block and yield, it is not difficult to infer that each is actually a method like this:
Def each
For each element # pseudocode
Yield (element)
End
End
5. Times {puts "*"}
1. upto (6) {| I | puts I. to_s}
Definition method:
Def... end
Ruby allows you to specify the default value of a method parameter-if the caller does not specify
Ruby allows you to specify the default value of a method parameter-if the caller does not explicitly specify the value used when passing in the parameter, you can use the copy OPERATOR:
Def cool_dude (arg1 = "Miles", arg2 = "Coltrane", arg3 = "roach ")
"# {AG1}, # {ag2}, #{ag3 }."
End
Cool_dude
Cool_dude ("Bart")-> miles, Coltrane, Roach
Cool_dude ("Bart", "Elwood")-> Bart, Coltrane, Roach
Cool_dude ("Bart", "Elwood", "Linus")-> Bart, Elwood, Linus
Variable Length Parameter List
Def varags (arg1, * rest)
"Got # {arg1} and # {rest. Join (',')}"
End
Varags ("one")-> got one and two
Varags ("one", "two", "three")-> got one and two, three
If the last parameter of the method is &, the associated block is converted into a proc object and assigned to this parameter.
Class taxcalculator
Def initialize (name, & Block)
@ Name, @ block = Name, Block
End
Def get_tax (amount)
"# @ Name on # {amount }=#{ @ block. Call (amount )}"
End
End
TC = taxcalculator. New ("sales tax") {| AMT x 0.75}
TC. get_tax (100) #-> sales tax on 100 = 7.5
TC. get_tax (250) #-> sales tax on 250 = 18.75
The method returns multiple values, and returns multiple values in the form of arrays.
Def meth_three
100. Times do | num |
Square = num * num
Return num, square if square> 1000
End
End
Meth_three-> [32,1024]
Num, square = meth_three-> num = 32, square = 1024