class Basher::Cursor
Helper class that allows storing movement state inside a collection.
Attributes
collection[R]
Frozen reference of the word.
index[R]
Position of the cursor inside collection
. Default is 0.
Public Class Methods
new(collection)
click to toggle source
Returns a Cursor
instance for the given collection. The cursor’s index is defaulted to 0.
# File lib/basher/cursor.rb, line 11 def initialize(collection) @collection = collection @index = 0 end
Public Instance Methods
advance!()
click to toggle source
Advance the cursor one step.
# File lib/basher/cursor.rb, line 32 def advance! @index += 1 unless finished? item end
finished?()
click to toggle source
Returns true if the cursor is at the last index, or false otherwise.
# File lib/basher/cursor.rb, line 45 def finished? index == finish end
item()
click to toggle source
Gets the item at the cursor.
# File lib/basher/cursor.rb, line 17 def item collection[index] end
previous()
click to toggle source
Gets the items before the cursor.
# File lib/basher/cursor.rb, line 22 def previous collection[0...index] end
remaining()
click to toggle source
Gets remaining items, including item at the cursor.
# File lib/basher/cursor.rb, line 27 def remaining collection[index..-1] end
rewind!()
click to toggle source
Rewind the cursor back to start, and return the item.
# File lib/basher/cursor.rb, line 38 def rewind! @index = start item end
Private Instance Methods
finish()
click to toggle source
# File lib/basher/cursor.rb, line 55 def finish collection.size end
start()
click to toggle source
# File lib/basher/cursor.rb, line 51 def start 0 end