From: "sidonath (Damir Zekic) via ruby-core" Date: 2024-02-13T14:59:18+00:00 Subject: [ruby-core:116699] [Ruby master Feature#19905] Introduce `Queue#peek` Issue #19905 has been updated by sidonath (Damir Zekic). I would like to provide another use case for `Queue#peek`. I'm using `Queue` in a project that's running commands prone to failures. I have a queue of commands, a worker and a monitor threads. The worker pops the commands, runs them in sequence and the monitor thread reports on the activity and restarts the worker if it observes a failure. Having ability to "peek" into the command list would allow informative output about what is the next command that will be executed. Obviously, having `#to_a` or `#each` would be as helpful here. The queue is not long for me to worry about looking up all elements versus the first one. If there's a race condition and the proposed `#peek` doesn't lock, then the display of the "next command" might be briefly inaccurate at times, but that's not something I would consider an issue in this project. If it were locking, then I would need to use the retrieved instance outside of the locked block. This would beat the purpose of the lock, but that would be the conscious choice of me as the author of the code. I guess whether `#peek` locks or not would not be important in my situation. ---------------------------------------- Feature #19905: Introduce `Queue#peek` https://bugs.ruby-lang.org/issues/19905#change-106714 * Author: hi@joaofernandes.me (Joao Fernandes) * Status: Open * Priority: Normal ---------------------------------------- This ticket proposes the introduction of the `Queue#peek` method, similar to what we can find in other object oriented languages such as Java and C#. This method is similar to `Queue#pop`, but does not change the data, nor does it require a lock. ``` q = Queue.new([1,2,3]) => # q.peek => 1 q.peek => 1 ``` I have felt the need of this for debugging, but I think that it can also be of practical use for presentation. I believe that the only drawback could be that newcomers could misuse it in multi-threaded work without taking into account that this method is not thread safe. I also volunteer myself to implement this method. -- https://bugs.ruby-lang.org/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/