Ruby/Development/Generator
Материал из Wiki.crossplatform.ru
(Различия между версиями)
Версия 17:10, 26 мая 2010
Generate from array
require "generator" l = ["junk1", 1, "junk2", 2, "junk3", "junk4", 3, "junk5"] g = Generator.new(l) g.next # => "junk1" g.next # => 1 g.next # => "junk2"
Generator based on regualr expression
require "generator" l = ["junk1", 1, "junk2", 2, "junk3", "junk4", 3, "junk5"] g = Generator.new { |g| l.each { |e| g.yield e unless e =~ /^junk/ } } g.next # => 1 g.next # => 2 g.next # => 3