Class: Rb::RubyIterator

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

Instance Method Summary collapse

Constructor Details

#initialize(x) ⇒ RubyIterator

Returns a new instance of RubyIterator.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/lib/rb/ruby_iterator.rb', line 7

def initialize(x)
  if x.is_a?(Hash) 
    @ref = x.values.each
    @at = 0
    @len = x.size
  elsif x.respond_to?("each") 
    @ref = x.each
    @at = 0
    @len = x.size
  elsif x.respond_to?("iterator") 
    @ref = x.iterator
    @at = -1
    @at = -2 if !@ref.respond_to?("has_next")
  else 
    @ref = x
    @at = -2
  end
end

Instance Method Details

#_nextObject



40
41
42
43
44
45
# File 'lib/lib/rb/ruby_iterator.rb', line 40

def _next 
  return @ref[:_next].call if @at == -1
  return @ref[:_next][:call].call if @at == -2
  @at+=1
  @ref.next
end

#has_nextObject



34
35
36
37
38
# File 'lib/lib/rb/ruby_iterator.rb', line 34

def has_next 
  return @ref[:has_next].call if @at == -1
  return @ref[:has_next][:call].call if @at == -2
  @at < @len
end