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]
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|