Ruby in man's tears (1)

Source: Internet
Author: User

I want to be a professional technical writer, but I am too emotional to write rigorous technology.Article, Always like to show too many emotional colors in your own articles. This series of articles shows my recent feelings from the name, and it is undeniable that I have been suffering recently and often shed tears. I don't know what Ruby is like in a man's tears. Do you want to know? Check out my series of articles.

I. Hate? World

I remember that most articles always like to use Hello world when introducing languages.ProgramThat program can display the structure of the language in the best way. It is very classic, but I feel as though I do not like the world very much. I may hate the heartless world, the hypocritical world, or? World. Take a look at the followingCode:

Print('Enter the world name (ex sad) ') worldname =Gets()Puts("Hate the # {worldname} world")

This code indicates entering the name of the world you hate most according to the prompt, and then outputting the hate? World. A simple program changes the Hello world. In this small program, we can talk about four points:

    1. Print ('') is to output the characters in single quotes. It does not wrap a line after the output string.
    2. Gets () gets the input string and assigns it to other variables.
    3. Puts ('') is to output the content in the quotation marks and wrap the line after the output. This is different from print.
    4. . I saidInsideIt means that it is not necessarily a variable or an expression or function.
Ii. Methods

In Oop, we always say that objects communicate through messages. What do I have to do after an object obtains a message and wants to process it. Call the corresponding methods to respond to the message. This introduces our methods. In non-oop, or process-oriented programming, our method becomes a function. In Ruby, how does one define a methods? In fact, it looks similar to Python. For the code segment, you can fully understand it:

DefShowmynamePuts('Jesse zhao ')EndDefShowyourname (aname)Puts("Your name is # {aname}")EndDefShowourname (aname, bname)Puts("My name is # {aname}, your name is # {bname}")End

After reading this code, you can understand the method writing method. The three examples show how to write a method without parameters, parameters, and multiple parameters. Here I want to talk about the parentheses behind the method name of the def in puts. It is my habit to add parentheses, and I personally feel that the Code will not look too messy.

Iii. Number and condition: If then

I read an example in a little book of Ruby, which is very good and well written. Let's take a look. I feel like I can get to know more about the objects in ruby.

Taxrate = 0.175Print("Enter price:") S =Gets() Subtotal = S. to_fIf(Subtotal <0.0)ThenSubtotal = 0.0EndTax = subtotax * taxratePuts"Tax on $ # {subtotal} is $ # {tax}, so grand total is $ # {subtotal * Tax}"

To explain:

    1. Taxrate is directly used if the variable is not defined, because Ruby allows you to create a variable as needed, and Ruby can perceive the type of the variable on its own.
    2. S. to_f. Because Ruby is completely Oo, the string and integer types are all methods. The to_f method is the string type s method, which can convert sting to the float type.
    3. If then end, there is no need to explain it. You can understand some programming experience.
    4. String to float. If string is a numeric format, it can be directly converted to float. If it is not a numeric format, it is programmed to convert it to float by 0.0.
Iv. Follow-up

Let's talk about this today and start class tomorrow. Baby, I still love you. Please give me another chance.

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.