Ruby series (1): Basic Ruby knowledge

Source: Internet
Author: User

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=> NilWhat is it? It is actually the return value of the command.PutsThe 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.

MathIs a built-in Mathematical Module. The module has two functions in ruby. First, put functions with similar functions under the same name.MathModulesSin ()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 calledSQRTFunction, and give it9As a parameter. Of courseSQRTSquare 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. SQRTAlways 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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.