Class: ArrayFloe::Floe

Inherits:
Object
  • Object
show all
Defined in:
lib/array_floe/floe.rb

Overview

The class for the “floe” object constructed by Array#each_with_floe and Array#each_with_index_floe

Instance Method Summary collapse

Constructor Details

#initialize(i, size) ⇒ Floe

:nodoc:



6
7
8
9
10
# File 'lib/array_floe/floe.rb', line 6

def initialize(i, size) # :nodoc:
  @first = i == 0
  @last = i == size -1
  @odd = i.odd?
end

Instance Method Details

#even?Boolean

Returns true for even-numbered elements

Returns:

  • (Boolean)


28
29
30
# File 'lib/array_floe/floe.rb', line 28

def even?
  !@odd
end

#first?Boolean

Returns true for the first element

Returns:

  • (Boolean)


13
14
15
# File 'lib/array_floe/floe.rb', line 13

def first? 
  @first
end

#last?Boolean

Returns true for the last element

Returns:

  • (Boolean)


18
19
20
# File 'lib/array_floe/floe.rb', line 18

def last?
  @last
end

#odd?Boolean

Returns true for odd-numbered elements

Returns:

  • (Boolean)


23
24
25
# File 'lib/array_floe/floe.rb', line 23

def odd?
  @odd
end

#to_sObject

Returns a space-separated list of “first”, “last”, “odd”, and “even” as appropriate for the current element.



34
35
36
# File 'lib/array_floe/floe.rb', line 34

def to_s
  @str ||= [@first && "first", @last && "last", @odd ? "odd" : "even"].select{|x| x}.join(' ')
end