-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Array#reject! shouldn't return Nil #4994
Comments
arr = [1, 2, 4, 5]
arr = arr
.reject { |e| e % 2 == 0 }
.reject { |e| e == 1 }
puts arr # => [5] This shouldn't cause any sensible performance impact. |
Hmm... |
@tbrand Actually, Crystal is safer here. If # Ruby
arr = [1, 5]
arr
.reject! { |e| e % 2 == 0 }
.reject! { |e| e == 1 }
puts arr Output:
Exactly the same error Crystal gives you, but Ruby tells you at runtime time, Crystal at compile time. There's no need to use reject, there's always arr = [1, 2, 4, 5]
arr
.reject! { |e| e % 2 == 0 }
.try &.reject! { |e| e == 1 }
puts arr |
💯
|
Hold on... why would |
That way you know if |
OK but there are other ways to get this information. Other methods don't have such a quirk. |
Carried from Ruby. How else would you do that? Maybe that info is not important...? |
Well, the obvious thing is to return |
By the way, how to get that information -- get size before and size after |
I think it's better to always return |
Modifies Array based on discussion in crystal-lang#4994. `Array#reject!`, `Array#select!`, and `Array#compact!` now return self in all scenarios.
Modifies Array based on discussion in crystal-lang#4994. `Array#reject!`, `Array#select!`, and `Array#compact!` now return self in all scenarios.
Now we need the same changes in Ruby 😅 |
Reproducible code
When execute this code in ruby, it returns [5].
In Crystal, it raises a following error.
The problem is not the compatibility, but the usability.
In many real cases, we use such methods in chains.
So
reject!
should return an empty array whenreject!
removes all of the elements.Needless to say, using
not_nil!
is not safety.Thanks
The text was updated successfully, but these errors were encountered: