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?
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?
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.
length
and count
as well (w.r.t. arrays)? –
Dhiman 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
.
They are identical. It's just alias.
Check this article for more info, also on count
.
"".method(:size).original_name # => :size
. "".method(:length).original_name # => :length
. –
Airframe I prefer size since it saves me typing/reading 2 characters.
In RUBY
there is no difference between size
and length
.
"length"
and "size"
are aliases for each other. FYI
But in Ruby On Rails(ROR), there is a big difference between them.
Returns the size of the collection calling size on the target. If the collection has been already loaded, length and size are equivalent. If not and you are going to need the records anyway this method will take one less query. Otherwise size is more efficient.
Returns the size of the collection. If the collection hasn't been loaded, it executes a SELECT COUNT(*) query. Else it calls collection.size.
If the collection has been already loaded size and length are equivalent. If not and you are going to need the records anyway length will take one less query. Otherwise size is more efficient.
when you are implementing relation group
, there is a very big difference between them.
Differences query.length:
Retrieves all the grouped records and then counts them. This can be slower and uses more memory as it involves retrieving and storing the full dataset before counting.
query.size:
Returns a count of the grouped results, not the actual records. This is faster and uses less memory since it doesn't need to load the actual records into memory.
(byebug) Role.group(:name, :id).length
Role Load (54.3ms) SELECT "roles".* FROM "roles" WHERE "roles"."type" = $1 GROUP BY "roles"."name", "roles"."id" [["type", "Role"]]
6
(byebug) Role.group(:name, :id).size
(50.4ms) SELECT COUNT(*) AS count_all, "roles"."name" AS roles_name, "roles"."id" AS roles_id FROM "roles" WHERE "roles"."type" = $1 GROUP BY "roles"."name", "roles"."id" [["type", "Role"]]
{["yue", 6]=>1, ["staff", 1]=>1, ["partner", 3]=>1, ["forbidden", 5]=>1, ["admin", 2]=>1, ["external_staff", 4]=>1}
(byebug)
Role.group(:name, :id).length
execute the SQLRole Load (54.3ms) SELECT "roles".* FROM "roles" WHERE "roles"."type" = $1 GROUP BY "roles"."name", "roles"."id" [["type", "Role"]]
and gets result
6
Role.group(:name, :id).size
does this:SELECT COUNT(*) AS count_all, "roles"."name" AS roles_name, "roles"."id" AS roles_id FROM "roles" WHERE "roles"."type" = $1 GROUP BY "roles"."name", "roles"."id" [["type", "Role"]]
gets result
{["yue", 6]=>1, ["staff", 1]=>1, ["partner", 3]=>1, ["forbidden", 5]=>1, ["admin", 2]=>1, ["external_staff", 4]=>1}
© 2022 - 2024 — McMap. All rights reserved.
rb_str_length
. – Amsterdam