Métodos y Clases: Construyendo un Iterador

A class is a collection of related methods that operate on the state of an object.

An object’s state is held by its instance variables: variables whose names begin with @ and whose values are specific to that particular object.

This example class demonstrates how to write iterator methods and define operators.

lpp@nereida:~/src/rubytesting/TheRubyProgrammingLanguage$ cat -n Sequence.rb 
     1  # The Ruby programming Language Flanagan & Matsumoto
     2  # page 8
     3  class Sequence
     4    include Enumerable
     5    def initialize(from, to, by)
     6      @from, @to, @by = from, to, by
     7    end
     8
     9    def each
    10      x = @from
    11      while x <= @to
    12        yield x                                                                                       
    13        x +=@by
    14      end                                                                                                             
    15    end
    16                                                                                                                              
    17    def length
    18      return 0 if @from > @to                                                                                                             
    19      Integer((@to-@from)/@by)+1                                                                                                          
    20    end
    21                                                                                                                                                    
    22    alias size length
    23
    24    # override the array-access operator to give random access to the sequence                                                                               
    25
    26    def [](index)                                                                                                                                              
    27      return nil if index < 0
    28      v = @from+index*@by
    29      return v if v <= @to
    30      return nil
    31    end
    32
    33    def *(factor) 
    34      Sequence.new(@from*factor, @to*factor, @by*factor)
    35    end
    36
    37    def +(offset) 
    38      Sequence.new(@from+offset, @to+offset, @by)
    39    end
    40  end

Here is some code that uses this Sequence class:

lpp@nereida:~/src/rubytesting/TheRubyProgrammingLanguage$ cat -n useSequence.rb 
     1  #!/usr/bin/env ruby
     2  require "Sequence.rb"
     3
     4  a = Sequence.new(1,10,2)
     5  a.each { |x| print x,"\n" } #  1 3 5 7 9
     6
     7  puts "Length = #{a.length}" # 5
     8  puts "Size = #{a.size}"     # 5
     9
    10  (0..(a.size-1)).each { |i|    # Podemos indexar 
    11    puts "a[#{i}] = '#{a[i]}'"  # un objeto Sequence
    12  }
    13
    14  puts ""
    15
    16  b = a * 2 # b is a new Sequence object
    17  b.each { |x| print x,"\n" } # 2 6 10 14 18
    18
    19  puts ""
    20
    21  b = a + 2 # b is a new Sequence object
    22  b.each { |x| print x,"\n" } # 3 5 7  9 11

lpp@nereida:~/src/rubytesting/TheRubyProgrammingLanguage$ ./useSequence.rb 
1
3
5
7
9
Length = 5
Size = 5
a[0] = '1'
a[1] = '3'
a[2] = '5'
a[3] = '7'
a[4] = '9'

2
6
10
14
18

3
5
7
9
11

Casiano Rodriguez León 2015-06-18