20 minutes to teach you Ruby QuickStart tutorials 1th/4 page _ruby topics

Source: Internet
Author: User
Tags square root
Suddenly I found out that Ruby was good too, I was kind of thinking about learning
Introduced
This is a short introduction to Ruby and it takes only 20 minutes to complete the reading. This assumes that the reader has already installed Ruby, and if you are not installing it, please visit the official Ruby website for download and installation before reading.

Interactive Ruby

Open irb (interactive ruby Shell):

If you are using Mac OS X, open the Terminal window to enter IRB;

If you use Linux, open the shell and enter IRB;

If you are using Windows, please find Ruby->fxri in the Start menu and execute it.



OK, after you open IRB, enter "Hello World" in it.



Ruby, listen to your schedule!

What happened? Have we just written the world's most short "Hello Worlds" program? That's not exactly true. The second line of output is IRB tells us: The evaluation result of the previous expression. If we want to print "Hello world", we need a little more effort:



Puts is a simple print output command in Ruby. What is the "=> nil" behind it? --that's the result of an expression. Puts always returns nil, a way in which Ruby represents "absolutely no value" (absolutely-positively-nothing value), and looks somewhat like a null in Java.

your free calculator is here!

Without doing anything, we can use IRB as a simple calculator:



This will be able to calculate 3+2. It's simple enough! So how about 3 times 2? You can continue to enter 3*2 below, or you can go back to the above (3+2) to rewrite the formula you just entered. Use the UP arrow on the keyboard to make the cursor reach the 3+2 line, then use the left arrow to move the cursor to the plus sign, and then use the SPACEBAR to make changes.



Now, let's try to compute the square of 3:



In the Ruby language, * * represents a power operation. So how do you compute the square root?



Ok, wait a minute, what does the sqrt (9) In the expression mean? You can probably guess this is the square root of the calculation 9. And what does math mean? Don't worry, let's take a closer look at modules like math.

Modules--code grouped by subject

Math is a mathematical module built in Ruby. In Ruby, a module provides two roles: one role is to gather similar methods in the same "family" name. Therefore, math also includes methods such as sin and tan. The second role is a dot (dot), which marks the recipient of the message. What is a message? In the above example, sqrt (9) is the message, which means that the Sqrt method is invoked to take out the square root of 9.

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 there is a float here.

So how do we remember the results of these calculations? --assigns the result to the variable.


Current 1/4 page 1234 Next read the full text
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.