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?
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.
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.
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.
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.
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.
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.
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.
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.
There's no difference between size
and length
of strings. Which one you prefer is essentially a matter of style.
They are identical. It's just alias.
Check this article for more info, also on count
.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With