I suddenly found that ruby is also good, and I want to learn it.
Introduction
This is a short Ruby entry, and it takes only 20 minutes to complete. Here we assume that you have installed Ruby. If you have not installed Ruby, visit the official Ruby website to download and install it.
Interactive Ruby
Open IRB (Interactive Ruby shell ):
If you use Mac OS X, open the terminal window and enter irb;
If you are using Linux, Open shell and enter irb;
If you are using windows, find Ruby-> fxri in the Start menu and execute it.
OK. After enabling IRB, enter "Hello World ".
Ruby follows your schedule!
What happened? Have we compiled the shortest "Hello World" program in the World just now? This is not accurate. The second line of output shows the evaluation result of the previous expression. If we want to print "Hello World", we still need to work hard:
Puts is a simple print output command in Ruby. What does "=> nil" next to it mean? -- That is the result of the expression. Puts always returns nil. This is the method in Ruby that indicates "absolute no value" (absolutely-positively-nothing value). It looks like null in Java.
Your free calculator is here!
Without doing anything, we can use IRB as a simple calculator:
In this way, 3 + 2 can be calculated. Simple enough! So how is 3 multiplied by 2? You can enter 3*2 below, or go back to the above (3 + 2) and modify the formula you just entered. Use the up key on the keyboard to move the cursor to the 3 + 2 line, move the cursor to the plus sign with the left button, and then use the space key for modification.
Next, let's calculate the square of 3:
In Ruby, ** indicates a power operation. How can we calculate the square root?
OK. Wait, what is sqrt (9) in the expression? You can guess that this is the square root of 9. What does Math mean? Don't worry. Let's take a closer look at modules like Math.
Module-code grouped by topic
Math is a built-in Mathematical Module of Ruby. In Ruby, the module provides two roles: A Role aggregates similar methods under the same "family. Therefore, Math also includes methods such as sin and tan. The second role is a dot, which marks the receiver of the message. What is a message? In the above example, sqrt (9) is the message, which means that the square root of 9 is obtained by calling the sqrt method.
The result of the Sqrt method call is 3.0. You may notice that it is not 3. This is because in most cases, the square root of a number is not an integer, so a floating point number is returned.
So how do we remember these computing results? -- Assign the result to the variable.