ruby 类方法与实例方法_Ruby方法

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中的两种类型:

  1. By using the expression

    通过使用表达式

  2. By using return keyword.

    通过使用return关键字。

Syntax of method:

方法的语法:

def methodname(arguments)
    statements
    return
end
defining a method in Ruby

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

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

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.

在此程序中,有squarepower两种方法。 幂方法两个参数。 在将基本变量中存在的值提高到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方法时,我们将传递变量numbersqrnumber 。 因此,返回numbersqr ^ number的值。

In our case, 9^3 or 9*9*9 which is 729 is returned from the function.

在我们的例子中,从函数返回9^39*9*9729 )。

defining a method in Ruby

翻译自: https://www.studytonight.com/ruby/methods-in-ruby

ruby 类方法与实例方法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值