ruby 类方法与实例方法
Methods are also called as Functions. Methods are used to execute specific set of instructions
方法也称为函数 。 方法用于执行特定的指令集
例如, (For example,)
File Menu in Word has different operations like Save, Open etc.,
Word中的“ 文件菜单 ”具有不同的操作,例如“ 保存” ,“ 打开”等,
Save is used to specifically save the document and contains corresponding code for it.
保存用于专门保存文档,并包含相应的代码。
Open command is used to open the document.
打开命令用于打开文档。
Functions take arguments and have return type.
函数接受参数并具有返回类型 。
Arguments are like an input given to the function and return type is the output of the function.
参数就像给函数的输入 , 返回类型是函数的输出 。
Return type can be two types in ruby:
返回类型可以是Ruby中的两种类型:
By using the expression
通过使用表达式
By using
return
keyword.通过使用
return
关键字。
Syntax of method:
方法的语法:
def methodname(arguments)
statements
return
end
![defining a method in Ruby](https://i-blog.csdnimg.cn/blog_migrate/468d34a9b7b5f9a14cab9265799a1960.png)
In the above code, there is a method named square
with one argument. A variable named number
is assigned with the value of 3. Another variable called numbersqr
is assigned with the value returned by the function. Here, we call the function by specifying the name and passing the argument.
在上面的代码中,有一个名为square
的方法,带有一个参数。 分配给变量number
的值为3 。 另一个名为numbersqr
变量被分配了函数返回的值。 在这里,我们通过指定名称并传递参数来调用函数。
square(number). It is called function calling statement.
The variable number
is passed to the function which is assigned to the local variable num
in the function, the number is multiplied and the function returns the value which is assigned to the variable numbersqr
.
将变量number
传递给在函数中分配给局部变量num
的函数,将数字相乘,然后函数返回分配给变量numbersqr
。
This is the output of the above program :
这是上面程序的输出:
![defining a method in Ruby](https://i-blog.csdnimg.cn/blog_migrate/b560e1c82afae4ae375d85ef499c3748.png)
The value 9 is returned from the function and assigned to variable numbersqr
and displayed using print
method. In this example, the return type of the function is using expression.
从函数返回值9 ,并将其分配给变量numbersqr
并使用print
方法显示。 在此示例中,函数的返回类型使用表达式。
带有多个参数的Ruby方法 (Ruby Method with more than one parameter)
![defining a method with more than one parameter in Ruby](https://i-blog.csdnimg.cn/blog_migrate/8b5f0a1c8d76c7e37300b6bdd3315982.png)
In this program, there are two methods square
and power
. Power method takes two arguments. This method returns the value after raising the value present in the base variable to the value present in the exp
variable. This method explicitly returns the value. That is, the value is returned using return
keyword.
在此程序中,有square
和power
两种方法。 幂方法有两个参数。 在将基本变量中存在的值提高到exp
变量中存在的值之后,此方法返回该值。 此方法显式返回值。 即,使用return
关键字返回该值。
def power (base, exp)
product = 1
while exp > 0
product *= base
exp -= 1
end
return product
end
When this function is called by passing the following parameters: power (3, 3)
. It returns the value of 3 raised to the power 3.
通过传递以下参数调用此函数时: power (3, 3)
。 它将3的值返回到幂3。
In this program, the function calling statement is written inside print method. This specifies that function called be anywhere in the program.
在此程序中,函数调用语句写在print方法内部。 这指定了在程序中任何位置调用的函数。
First, the square method is called, it returns the value and stored in the variable numbersqr
. Now, when calling power method, we are passing the variables numbersqr
and number
. Therefore, the value of numbersqr ^ number
is returned.
首先,调用square方法,它返回值并将其存储在变量numbersqr
。 现在,当调用power方法时,我们将传递变量numbersqr
和number
。 因此,返回numbersqr ^ number
的值。
In our case, 9^3
or 9*9*9
which is 729 is returned from the function.
在我们的例子中,从函数返回9^3
或9*9*9
即729 )。
![defining a method in Ruby](https://i-blog.csdnimg.cn/blog_migrate/bf4b08552036e54fae9c394a4883ef31.png)
ruby 类方法与实例方法