`size` 和 `length` 方法有什么区别 [英] What is the difference between `size` and `length` methods

查看:43
本文介绍了`size` 和 `length` 方法有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我对字符串运行 sizelength 时,它们返回相同的值.

When I ran size and length on a string, they returned the same value.

"akash".size # => 5
"akash".length # => 5

这两种方法有什么区别?

What is the difference between these two methods?

推荐答案

总结

在 Ruby 中,方法可以被覆盖,因此有些类中存在多个导致相同结果的方法,因此可以轻松地在一个方法中覆盖行为而不会影响另一个.一些类使用单独的方法来实现这一点,而其他类则以别名的形式实现这种行为.

Summary

In Ruby, methods can be overridden, so there are classes where there are multiple methods that lead to the same results so that behavior can be easily overridden in one method without affecting the other. Some classes do this using separate methods, while other classes implement this behavior as aliases.

哪个是哪个,为什么,通常是一个语言实现决策,如果不询问实现代码的 Ruby Core 团队成员,就无法得到规范的回答.因此,问题的那部分超出了 Stack Overflow 的范围.假设别名方法不会像类似工作的方法那样经常被猴子修补是一个合理的假设,但这只是:一个假设.

Which is which, and why, is often a language implementation decision that can't be answered canonically without asking the Ruby Core team members who implemented the code. As such, that portion of the question is out of scope for Stack Overflow. Assuming that aliased methods are not expected to be monkey-patched as often as work-alike methods is a reasonable assumption, but it is only that: an assumption.

如果您需要一个真正规范的答案,则必须深入挖掘 SVN 源代码、搜索错误跟踪器讨论或直接询问核心团队.但是,我在下面提供了实用分析.

If you need a truly canonical answer, you will have to dig through the SVN source, search the bug tracker discussions, or ask the Core Team directly. However, I provide a pragmatic analysis below.

例如,Ruby String#sizeString#length 方法实际上是单独的方法,但在内部 Ruby 调用相同的 C 源代码来实现它们:

For example, the Ruby String#size and String#length methods are actually separate methods, but internally Ruby calls the same C source code to implement them both:

rb_str_length(VALUE str)
{
    return LONG2NUM(str_strlen(str, NULL));
}

这纯粹是一个实现细节.从 Ruby VM 的角度来看,它们实际上是独立的方法,只是碰巧共享底层 C 实现以提高速度.您应该能够在 String 对象上重新定义 #size 或 #length 而不改变两者的行为,尽管这样做通常会干扰 REPL,例如 Pry 或 IRB.

This is purely an implementation detail. From the Ruby VM's point of view, they are really separate methods that just happen to share an underlying C implementation for speed. You should be able to redefine #size or #length on a String object without changing the behavior of both, although doing so often interferes with a REPL such as Pry or IRB.

另一方面,一些类将#size 和#length 实现为别名.例如,Array#size 是明确定义的作为 Array#length 的别名.因此,这会创建一个作为#size 的原始方法名称的副本,因此您应该能够重新定义别名版本而无需更改原始#length 方法的行为.

On the other hand, some classes implement #size and #length as aliases. For example, Array#size is explicitly defined as an alias for Array#length. As a result, this creates a copy of the original method name as #size, so you should be able to redefine the aliased version without changing the behavior of the original #length method.

这个问题实际上是实现上的差异,而不是行为上的差异.在实践中,似乎唯一有意义的区别在于哪个 Ruby 组件实现了类似工作的行为.可能有遗留原因、性能原因,也可能只是一个没有人足够关心提交或修复的错误.

This issue is really a difference of implementation, not behavior. In practice, it would appear the only meaningful distinction lies in which Ruby component implements the work-alike behavior. There may be legacy reasons, performance reasons, or it may simply be a bug that no one has cared enough about to file or fix.

由于行为是理智的,并且并没有真正违反原则最少惊喜,我会将其视为次要的语言怪癖.但是,任何对此感觉更强烈的人都应该提交错误.

Since the behavior is sane, and doesn't really violate the Principle of Least Surprise, I'd treat it as a minor language quirk. However, anyone who feels more strongly about it should definitely file a bug.

这篇关于`size` 和 `length` 方法有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆