What would be the Moose version of this (a queue that maintains a fixed size by evicting the oldest items) ?
class FixedSizeQueue
def initialize(size)
@max_size = size
@items = []
end
def pop
@items.shift
end
def push(item)
@items.push item
if @items.size > @max_size
pop
end
end
end
With usage
2:16% irb -r ./fsq.rb
irb(main):001:0> q = FixedSizeQueue.new(3)
=> #<FixedSizeQueue:0x9317c6c @max_size=3, @items=[]>
irb(main):002:0> q.push 2
=> nil
irb(main):003:0> q.push 3
=> nil
irb(main):004:0> q.push 5
=> nil
irb(main):005:0> q
=> #<FixedSizeQueue:0x9317c6c @max_size=3, @items=[2, 3, 5]>
irb(main):006:0> q.push 7
=> 2
irb(main):007:0> q
=> #<FixedSizeQueue:0x9317c6c @max_size=3, @items=[3, 5, 7]>
irb(main):009:0> FixedSizeQueue.instance_methods(false)
=> [:pop, :push]
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
|
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.