Methods (functions) in Ruby, ruby Functions

Source: Internet
Author: User

Methods (functions) in Ruby, ruby Functions

1. The method name and variable name naming rules in Ruby are the same (Start with a lower-case letter, and separate words with "_",), but you can add it behind the method name! (Major method) or? (Boolean return value ).

2. Use () for method parameters (),All parameters are local variables. You can set the default values for these parameters. Parameters with "*" are optional dynamic parameters. One or more parameters can be input. If there are common parameters, there are optional parameters, the optional parameters must be placed at the end, and the following sample code is provided.

Copy codeThe Code is as follows:
Def read_book (chinese = 'water margin ', english = 'Jane Eyre', * others)
Puts "chinese books: # {chinese}, English book: # {english }"
Temp =''
Others. each do | o |
Temp <o <''
End
Puts "other optional books: # {temp }"

End

Read_book
# Output: Chinese books: Water Margin, English book: Jane Eyre
Read_book ')
# Output:
# Chinese books: travel to the West, English book: Rails
# Other optional books: The Three Kingdoms of A Dream of Red Mansions

3. the return value of the method does not need to be declared. By default, the last line of code is the return value.If conditions are met to determine the returned results, use the return statement, as shown in the following sample code:

Copy codeThe Code is as follows:
Def late_for_work? (Minutes)
Return true if minutes> = 15
False
End

Late_for_work? (10) # false
Late_for_work? (15) # true

4. The method with "!" after the method name The normal method returns a copy of the original object.While the heavy method always returns the original object, the following sample code:

Copy codeThe Code is as follows:
X = 'hello'
Y = x. upcase
Puts "x :#{ x}, y :#{ y}" # output: x: hello, y: HELLO x has not changed.

X = 'hello'
Y = x. upcase!
Puts "x :#{ x}, y :#{ y}" # output: x: HELLO, y: HELLO x has changed.

5. You can use the alias keyword "alias" to generate a copy of the method., Even if the original method has changed, the alias copy will not change, as shown in the following sample code:

Copy codeThe Code is as follows:
Def show_alias
Puts 'before alias! '
End
Alias alias_foo show_alias
Def show_alias
Puts 'after alias! '
End
Show_alias # output: After alias!
Alias_foo # output: Before alias!

6. the Ruby method can return multiple values at a time.But essentially, it only seems that multiple values are returned. In essence, an array is returned, and multiple return values are stored in the array. When receiving, multiple variables can be directly defined for one-time receiving, sample Code:

Copy codeThe Code is as follows:
Def more_result_foo
X = 5
Y = x * x
Z = y-x + 2
Return x, y, z # returns three values: x, y, and z.
End
A, B, c = more_result_foo # a, B, c receives the returned x, y, z
M = more_result_foo # m receives the returned x, y, z
Puts a, B, c # output: 5 25 22
Puts m # output: 5 25 22
Puts m. class # output: Array


Mathematical second day function learning methods and knowledge points Summary

  
Knowledge Point Summary
1. concepts related to functions:
1. Variables and constants
In a change process, the amount of different values can be called a variable, and the constant amount is called a constant.
Note: variables and constants are usually relatively speaking. In different research processes, the identities of constants and variables can be converted to each other.
In a change process, there are two variables x and y. If each value of x has a unique value corresponding to it, then x is the independent variable, y is a function of x.
Note: A function represents a process of change. In this process, we must focus on the following three points:
(1) only two variables are allowed.
(2) The value of one variable changes with the value of another variable.
(3) For each definite value of the independent variable, the function has a unique value corresponding to it.
2. Determine the expression and expression of the function:
There are three methods to express the function relationship:
1 .. resolution method: the relationship between two variables can sometimes be expressed by an equation containing these two variables. This representation method is called resolution method. when a functional relationship is represented by an analytical method, the dependent variable y is placed on the left of the equation, and the algebraic form of the independent variable y is placed on the right. The essence is to use the algebraic form of x to represent y;
Note: The parsing method is simple and clear, which can accurately reflect the relationship between variables and dependent variables during the entire change process, but it is not intuitive, and some functional relationships may not be expressed using the parsing method.
2. List Method: The list method is used to list a series of values of the independent variable x and corresponding values of Function y into a table to represent the function relationship;
Note: The list method is easy to understand and use, but its corresponding values are limited, and it is not easy to see the corresponding laws between independent variables and functions from the table.
3. Image Method: The method for expressing functional relationships with images is called the image method. The image method is intuitive and is an important method for studying functions.
3. value range of function (or independent variable) and function Independent Variable

2. Function evaluate methods:
(1) When a function is represented by a function expression, the value of the function is an algebraic value;
(2) When the function value and expression are known, the essence of the bet's value is to solve the equation;
(3) When the value range of the function value is given and the value range of the corresponding independent variable is obtained, the essence is to solve the inequality (group ).
3 .. the value range of a function independent variable is the whole value of a meaningful independent variable. the value range of the independent variable is usually considered from two aspects: one is to make the analytical expression of the function meaningful; the other is to conform to the objective reality. the following describes how to determine the self-variable range in simple function expressions.
(1) When the analytical expression of a function is an integer, the independent variable takes any real number (that is, all real numbers );
(2) When the analytical expression of the function is fraction, the value of the independent variable is any real number that makes the denominator not zero;
(3) When the analytical expression of a function is an irrational formula of square open, the value of the independent variable is a real number that makes the formula of the square open non-negative;
(4) When the self-variable in the analytical expression of the function appears at the bottom of the zero power or negative integer power, the value of the independent variable is the real number that makes the bottom number not zero.
Note: When a function expression represents an actual or geometric problem, the value range of the independent variable must not only make the function expression meaningful, but also conform to the actual or geometric meaning.
In a function relationship, if there are several algebraic expressions at the same time, the value range of the function independent variable should be the public part of the value range of the Self-variables in various algebraic forms.
Iv. Function Image
1. Method of embedding Images
Determine the analytical expression of the function, and draw the image of the function. Generally, there are three steps:
(1) List: Calculate the corresponding function value from some values of the variable. From this series of corresponding values to a series of ordered real number pairs;
(2) points: In the Cartesian coordinate system, draw the corresponding points of these ordered real number pairs;
(3) Link: connect these points with a smooth curve to obtain the image of this function.
These are the outline of our teacher's review and hope to help you!

Common examination methods: (1) examine the concepts of functions;
(2) Evaluate the value range of the function value or independent variable .... Remaining full text>

Learning methods and experiences of the Function

First of all, you need to be clear about the mentality. It is not easy for everyone to learn the function of Gao Yi. Because it is different from the function in the beginning. So don't worry. Analyze it carefully and ask more teachers and classmates.
Second, carefully analyze the definition of the function and the representative elements of the function to understand the profound meaning of the independent variable. Then, take notes and ask the teacher. Note that it is important to record your experiences and classify and summarize questions. Let's take a look at the course. It takes a long time to understand.
In addition, the necessary questions are required, but you do not need to worry about too many questions. However, you must thoroughly understand each question and draw a line from each other.
Build confidence. Don't worry. Come on.

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.