Skip to content
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

Appending to unique list with limit should trim from the end #56

Merged
merged 1 commit into from Dec 2, 2021

Conversation

excid3
Copy link
Contributor

@excid3 excid3 commented Dec 2, 2021

When appending to a unique list with a limit, ltrim was clearing out the list every time.

Out of range indexes will not produce an error: if start is larger than the end of the list, or start > end, the result will be an empty list (which causes key to be removed).

This changes it to calculate the limit from the end to preserve the list properly.

@kaspth

@@ -16,7 +16,7 @@ def append(elements)
multi do
remove elements
super
ltrim (limit - 1), -1 if limit
ltrim -limit, -1 if limit
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting that going from the tail end isn't zero-indexed, e.g. the base is -1 and not -0.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it matches Ruby with ranges:

irb(main):001:0> [1,2,3,4][-3..-1]
=> [2, 3, 4]

@kaspth kaspth merged commit db8fc9a into rails:main Dec 2, 2021
@excid3 excid3 deleted the fix-unique-list-with-limit branch December 2, 2021 19:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants