For Ruby introduction, see: http://www.ruby-lang.org/zh_CN/about/
I. IRB
Interactive Ruby help and consoleProgramIn Windows.
You can directly execute Ruby in IRB.Code. For example:
IRB (main ): 001 : 0 > " Hello World "
=> " Hello World "
Ii. Puts commands and nil
IRB (main ): 002 : 0 > Puts " Hello World "
Hello World
=> Nil
Puts is the basic command used for printing in ruby. That=> Nil
What is it? It is actually the return value of the command.Puts
The command always returns nil, and nil is also a ruby null value.
Iii. Expression execution
The preceding "Hello world" is an expression and the result is itself. Puts "Hello World" can be considered as a command statement, it can return no value (that is, return nil ).
Examples of other expressions:
IRB (main ): 003 : 0 > 3 + 2
=> 5
IRB (main ):004: 0> 3*2
=> 6
IRB (main ):005: 0> 3**2
=> 9
IRB (main ):006: 0>Math. SQRT (9)
=> 3.0
IRB (main ): 007 : 0 > A = 3 ** 2
=> 9
IRB (main): 00 8 : 0 > B = 4 ** 2
=> 16
IRB (main): 00 9 : 0 > Math. SQRT ( + B)
=> 5.0
** Indicates the power, and math. SQRT indicates square open.
Iv. Concepts of modules
That is, the code group.
Math
Is a built-in Mathematical Module. The module has two functions in ruby. First, put functions with similar functions under the same name.Math
ModulesSin ()
AndTan ()
Such a function.
Next is a point. What is the point?Point is used to tell a receiver the information it wants to accept.. What is information? In this example, the information isSQRT (9)
Is calledSQRT
Function, and give it9
As a parameter. Of courseSQRT
Square Root is short for square root.
The return value of this function is3.0
. You must have discovered that it is not just3
, With more decimals. This is because in most cases, the result of the opening is not an integer. SQRT
Always Returns a floating point number..
V. Variables
What if we want to remember the operation results? Save it to the variable.
IRB (main ): 007 : 0 > A = 3 ** 2
=> 9
IRB (main): 00 8 : 0 > B = 4 ** 2
=> 16
IRB (main): 00 9 : 0 > Math. SQRT ( + B)
=> 5.0
Now, let's write the first article here. I am taking notes while reading official documents. If I have any mistakes, please correct me!