Search code examples
rubystring

What is the difference between `size` and `length` methods


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?


Solution

  • 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.

    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.

    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.

    String Class: Different Methods

    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));
    }
    

    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.

    Array Class: Aliased Methods

    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.

    Parting Thoughts

    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.