ruby中的bind方法_Ruby中的方法别名

ruby中的bind方法

To alias a method or variable name in Ruby is to create a second name for the method or variable. Aliasing can be used either to provide more expressive options to the programmer using the class or to help override methods and change the behavior of the class or object. Ruby provides this functionality with the "alias" and "alias_method" keywords.

在Ruby中为方法或变量名加上别名是为该方法或变量创建第二个名称。 可以使用别名来为使用类的程序员提供更具表达力的选项,或者用于帮助重写方法并更改类或对象的行为。 Ruby通过“ alias”和“ alias_method”关键字提供此功能。

创建一个名字 ( Create a Second Name )

The alias keyword takes two arguments: the old method name and the new method name. The method names should be passed as labels, as opposed to strings. Labels are used to refer to methods and variables without directly referencing them. If you're a new Ruby programmer, the concept of labels may seem odd, but whenever you see a label like ":methodname," just read it as "the thing called methodname." The following example declares a new class and creates an alias for the on method called start.

alias关键字带有两个参数:旧方法名称和新方法名称。 方法名称应作为标签传递,而不是字符串。 标签用于引用方法和变量,而无需直接引用它们。 如果您是新的Ruby程序员,标签的概念可能看起来很奇怪,但是只要看到“:methodname”之类的标签,就将其读为“方法名称”。 以下示例声明一个新类,并为名为start的on方法创建一个别名。


#!/usr/bin/env ruby
class Microwave
def on
puts "The microwave is on"
end
alias :start :on
end
m = Microwave.new
m.sta

改变班级的行为 ( Change the Behavior of a Class )

There may be times when you want to change the behavior of a class after it's been declared. You can alias and add new methods to an existing class by creating second class declaration that has the same name as the existing class declaration. You can also add aliases and methods to individual objects using a syntax similar to the inherited class syntax. The behavior of any class can be changed by creating an alias for any method and then creating a new method (with the original method name) that calls the method with the alias.

有时您可能想要在声明一个类后更改其行为。 您可以通过创建与现有类声明同名的第二个类声明来别名并向现有类添加新方法。 您还可以使用类似于继承的类语法的语法向各个对象添加别名和方法。 通过为任何方法创建别名,然后创建一个使用别名调用该方法的新方法(具有原始方法名称),可以更改任何类的行为。

In the following example, a microwave class is declared and an instance is created. The second class declaration uses the alias method to change the behavior of the "on" method in order to add a warning message. The third class declaration is used to change the behavior of the specific microwave instance to add an even more stern warning. When aliasing a method multiple times, be sure to use different method names to store the old method.

在下面的示例中,声明了微波类并创建了一个实例 。 第二类声明使用别名方法更改“ on”方法的行为,以便添加警告消息。 第三类声明用于更改特定微波实例的行为,以添加更严厉的警告。 多次使用方法别名时,请确保使用不同的方法名称来存储旧方法。


#!/usr/bin/env rubyclass Microwave
def on    puts "Microwave is on"  end
endm = Microwave.newm.onclass Microwave  alias :old_on1 :on
def on    puts "Warning: Do not insert metal objects!"    old_on1  end
end
m.on
# Message for this specific microwave
class <  def on
puts "This microwave is weak, add extra time"
old_on2
end
end
m.on # Displays extra message
m2 = Microwave.new
m2.on # Does not disp

翻译自: https://www.thoughtco.com/aliasing-in-ruby-2908190

ruby中的bind方法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值