ruby to_s_ex_Ruby中的alias vs alias_method

ruby to_s_ex

别名vs别名方法 (alias vs alias_method)

Before differentiating alias and alias_method, it is required to understand what an alias method does in any programming language? So, alias identifies a memory location which can be accessed by different symbolic notations or you can say by creating an alias, you provide the same memory to different symbols. The data stored in the variable are accessible to many symbolic names in a program. If the programmer makes any change in the data through a symbol, then the data will be changed for all the aliased symbols. This makes the program complex and difficult to be understood.

在区分alias和alias_method之前,需要了解别名方法在任何编程语言中的作用? 因此, 别名标识可以通过不同的符号表示法访问的内存位置,或者可以说通过创建别名为不同的符号提供相同的内存。 程序中的许多符号名称都可以访问变量中存储的数据。 如果程序员通过符号对数据进行了任何更改,则所有别名符号的数据都将被更改。 这使程序变得复杂且难以理解。

Ruby provides you two ways by which you can create an alias. First one is an alias and the second one is alias_method. Now let us see the code for each and will analyze, which one is better later on.

Ruby为您提供了两种创建别名的方法。 第一个是别名 ,第二个是alias_method 。 现在,让我们查看每个代码并进行分析,稍后再看哪个更好。

1)别名方法示例 (1) alias method example)

class Human

    def Human_age
        puts "Enter the age"
        @ag = gets.chomp
        puts "Age is #{@ag}"
    end
    
    alias age Human_age
end

Human.new.age

Output

输出量

Enter the age
12
Age is 12

In the above code, you can observe that we are creating an alias of method Human_age by the name age. When we are creating its object and calling the age, it is ultimately calling the Human_age method.

在上面的代码中,您可以看到我们正在使用名称age来创建方法 Human_age的别名 。 当我们创建其对象并调用age时 ,它最终将调用Human_age方法。

It is only creating an alias.

它只是创建一个别名。

2)alias_method示例 (2) alias_method example)

class Human

    def Human_age
        puts "Enter the age"
        @ag = gets.chomp
        puts "Age is #{@ag}"
    end
    
    alias_method :age, :Human_age
end

Human.new.age

Output

输出量

Enter the age
67
Age is 67

In the above code, instead of creating an alias, we are now creating an alias method by the name age. When we are calling age with the instance of class Human, we are calling the Human_age method.

在上面的代码中,我们没有创建别名,而是创建了一个名为age的别名方法。 当使用类Human的实例调用age时,我们将调用Human_age方法。

Now let us figure our differences between them.

现在让我们弄清它们之间的差异。

别名和alias_method之间的区别 (Differences between alias and alias_method)

  • We need to put , between old_method_name and new_method_name in the case of alias_method which directly shows that alias_method requires String as well as a symbol as arguments.

    alias_method的情况下,我们需要在old_method_name和new_method_name之间放置,以直接表明alias_method需要String以及符号作为参数。

  • If we talk about the scope of alias and alias_method, then we can conclude that alias is linguistically scoped which simply means that it considers itself as the value of "self" during the source code is read whereas if we talk about alias_method, it considers value of self as the value determined at the Run time.

    如果我们谈论alias和alias_method的范围,那么我们可以得出结论,别名在语言上是作用域的,这仅意味着在读取源代码期间,它会将自己视为“ self”的值,而如果我们谈论alias_method ,它将考虑值自定义为运行时确定的值。

  • alias is a keyword whereas alias_method is a method defined in the Module. Due to this fact, alias_method can be overridden.

    alias是关键字,而alias_method是模块中定义的方法。 由于这个事实, alias_method可以被覆盖。

Since alias_method facilitates you with more versatility and flexibility, it is better to use alias_method instead of alias keyword.

由于alias_method可以为您提供更多的功能和灵活性, 因此最好使用alias_method代替alias keyword

翻译自: https://www.includehelp.com/ruby/alias-vs-alias_method-in-ruby.aspx

ruby to_s_ex

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值