Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

string

ruby

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?

like image 727
thinkingmonster Avatar asked Mar 05 '16 16:03

thinkingmonster


People also ask

What is the difference between length and size?

Size and length both returns the number of element in an object. But length is faster than the size because length is a property and size is a method.

What is the difference between length () and size () in C++?

The size() function is consistent with other STL containers (like vector, map, etc.) and length() is consistent with most peoples intuitive notion of character strings like a word, sentence or paragraph. We say a paragraph'ss length not its size, so length() is to make things more readable.

What is the difference between the length field for an array and the size () method for an ArrayList?

ArrayList doesn't have length() method, the size() method of ArrayList provides the number of objects available in the collection. Array has length property which provides the length or capacity of the Array. It is the total space allocated during the initialization of the array.

What is the length method?

The length() method is a static method of String class. The length() returns the length of a string object i.e. the number of characters stored in an object. String class uses this method because the length of a string can be modified using the various operations on an object.


4 Answers

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.

like image 100
Todd A. Jacobs Avatar answered Oct 18 '22 01:10

Todd A. Jacobs


There's no difference between size and length of strings. Which one you prefer is essentially a matter of style.

like image 42
Mureinik Avatar answered Oct 18 '22 01:10

Mureinik


They are identical. It's just alias.

Check this article for more info, also on count.

like image 2
Harfangk Avatar answered Oct 17 '22 23:10

Harfangk


Contrary to what others have said, there is at least one differences between .length and .size in terms of usage.

.size may be used on integers, whereas .length raises a NoMethodError.

like image 2
jonschlinkert Avatar answered Oct 18 '22 00:10

jonschlinkert