Sha256: b2b5a3b8439ae5bed238f1bade80702f0637e11f684e52d4ebad9ffeeea19ab3

Contents?: true

Size: 818 Bytes

Versions: 2

Compression:

Stored size: 818 Bytes

Contents

module ArrayFloe

  # The class for the "floe" object constructed by
  # Array#each_with_floe and Array#each_with_index_floe
  class Floe
    def initialize(i, size) # :nodoc:
      @first = i == 0
      @last = i == size -1
      @odd = i.odd?
    end

    # Returns true for the first element
    def first? 
      @first
    end

    # Returns true for the last element
    def last?
      @last
    end

    # Returns true for odd-numbered elements
    def odd?
      @odd
    end

    # Returns true for even-numbered elements
    def even?
      !@odd
    end

    # Returns a space-separated list of "first", "last", "odd", and "even"
    # as appropriate for the current element.
    def to_s
      @str ||= [@first && "first", @last && "last", @odd ? "odd" : "even"].select{|x| x}.join(' ')
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
array_floe-1.1.3 lib/array_floe/floe.rb
array_floe-1.1.2 lib/array_floe/floe.rb