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

ruby 类方法与实例方法

Ruby方法 (Ruby methods)

Generally, methods tell the behavior of objects. A method is a set of predefined code which can be invoked any time in the code by its name. The method reduces redundancy in the code as we don't have to write code again and again for the same task. When methods are the part of the class, it sets performance guidelines for the instances of that particular class.

通常, 方法告诉对象的行为。 方法是一组预定义的代码,可以在代码中随时通过其名称调用该方法。 该方法减少了代码中的冗余,因为我们不必为同一任务重复编写代码。 当方法是类的一部分时,它将为该特定类的实例设置性能准则。

In Ruby, the general syntax of the method is as follows:

在Ruby中,该方法一般语法如下:

    def method_name
        #code block
    end

Example:

例:

# method to print text 
def printn
    puts "Hello friends!"
end

# calling the method
printn()

Output

输出量

Hello friends!

Above is syntax with no parameter list. If you want to specify the parameter list then follow the syntax given below:

上面是没有参数列表的语法。 如果要指定参数列表,请遵循以下语法:

带有参数列表的方法 (Method with parameters list)

We can also pass the parameters while calling the method in Ruby programming like other programming languages, here is the syntax to define a method in Ruby with parameters list.

我们还可以像在其他编程语言中一样在Ruby编程中调用方法时传递参数,这是在Ruby中使用参数列表定义方法的语法。

    def method_name(value 1, value2, ...)
        #expressions
    end

Example:

例:

# method with a parameter
def printn(name)
    puts "Hello #{name}"
end

# method calling with different values
printn("Amisha")
printn("Kamlesh")
printn("Satyam")	

Output

输出量

Hello Amisha
Hello Kamlesh
Hello Satyam

默认参数 (Default Parameters)

Ruby supports the feature of default parameters which means that if no value is provided to the method then the method will be invoked with default parameters in the following way:

Ruby支持默认参数的功能,这意味着如果没有为该方法提供任何值,则将通过以下方式使用默认参数来调用该方法:

    def method_name(param1=value, param2=value, ...)
        #code block
    end

Example:

例:

# method definition with default parameter
# if there is no parameter with method call
# method with use default parameter
def printn(name="Stranger")
    puts "Hello #{name}"
end

# method calling 
printn()            #invoking with default value
printn("Amisha")
printn("Kamlesh")
printn("Satyam")	

Output

输出量

Hello Stranger
Hello Amisha
Hello Kamlesh
Hello Satyam

You can invoke a method by using () as well as you can call it without using it when you don't need to pass any parameter list like,

您可以使用()调用方法,也可以在不需要传递任何参数列表(例如,

example (1):

范例(1):

# method definition with default parameter
# if there is no parameter with method call
# method with use default parameter
def printn(name="Stranger")
    puts "Hello #{name}"
end

# another method definition without using ()
def sayhello
    puts "Hi! You are at Includehelp.com"
end

# method calling with and without ()
printn
sayhello
printn("Amisha")
printn("Kamlesh")
printn("Satyam")

Output

输出量

Hello Stranger
Hi! You are at Includehelp.com
Hello Amisha
Hello Kamlesh
Hello Satyam

Example (2):

范例(2):

# method definition with default parameter
# if there is no parameter with method call
# method with use default parameter
def printn(name="Stranger")
    puts "Hello #{name}"
end

# method definition without using ()
def sayhello
    puts "Hi! You are at Includehelp.com"
end

# method calling with ()
printn()
sayhello()
printn("Amisha")
printn("Kamlesh")
printn("Satyam")

Output

输出量

Hello Strange
Hi! You are at Includehelp.com
Hello Amisha
Hello Kamlesh
Hello Satyam

Both the above syntaxes are supported by Ruby. The compiler will not reflect an error in any of the cases.

Ruby支持以上两种语法。 在任何情况下,编译器都不会反映错误。

返回值 (Returning Values)

This is one of the most important properties of methods that they return a value implicitly. That value is the last value of the expression like:

这是方法隐式返回值的最重要属性之一。 该值是表达式的最后一个值,例如:

Example:

例:

# method definition with default parameter
# if there is no parameter with method call
# method with use default parameter
def printn(name="Stranger")
    puts "Hello #{name}"
end


def sayhello
    puts "Hi! You are at Includehelp.com"
end

# method calling 
printn()
sayhello()
k = sayhello
print k

Output

输出量

Hello Stranger
Hi! You are at Includehelp.com
Hi! You are at Includehelp.com

In the above case, you can observe that we are not returning any value explicitly but still the value of the last expression is being returned from the method.

在上述情况下,您可以观察到我们没有显式返回任何值,但是仍然从method返回最后一个表达式的值

For explicit return, we can use 'return' statement at the last of the method definition in the following way:

对于显式返回,我们可以通过以下方式在方法定义的最后使用“ return”语句

Example:

例:

# method definition with return value
def marks
	return 34
end

# method calling 
m1=marks()
puts "#{m1}"

Output

输出量

34

You can also return multiple values through return statement like,

您还可以通过return语句返回多个值,例如,

Example:

例:

# method definition returning multiple values
def marks
	return 34,45,66
end

# method calling 
m1=marks()
puts "#{m1}"

The compiler would not give an error while compiling the above code, the output will be like:

编译上面的代码时,编译器不会给出错误,输出将类似于:

[34, 45, 66]

类中方法的实现 (Implementation of methods in Classes)

Ruby is purely object-oriented. A class may have n number of methods to define the behavior of its objects. When the method is defined outside the class, its scope is private by default but if it is defined inside the class body, it is public by default.

Ruby纯粹是面向对象的。 一个类可能有n种方法来定义其对象的行为。 当在类外部定义方法时,默认情况下其范围是私有的,但是如果在类主体内部定义了方法,则默认情况下它是公共的。

You can access a method by creating an object of the class with the help of . operator like: instance_name.method_name

您可以在的帮助下通过创建类的对象来访问方法。 运算子,例如: instance_name.method_name

Let us have a more clear vision with the help of an example:

借助示例让我们有一个更清晰的愿景:

# class definition
class Example
	# class method that will return marks
	def marks
		return 34,45,66
	end
	# class method that will print greeting messahe
	def greet
		puts "Hello Fam!"
	end
end

# object/instance creation
instance1 = Example.new

# calling methods 
instance1.greet()
m = instance1.marks()
puts "The marks are: " "#{m}"

Output

输出量

Hello Fam!
The marks are: [34, 45, 66]


翻译自: https://www.includehelp.com/ruby/methods.aspx

ruby 类方法与实例方法

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值