Skip to content

Instantly share code, notes, and snippets.

@delagoya
Created October 14, 2011 14:09
Show Gist options
  • Save delagoya/1287217 to your computer and use it in GitHub Desktop.
Save delagoya/1287217 to your computer and use it in GitHub Desktop.
Example for each() Enumerable
class MyFasta
include Enumerable
attr :fh
def initialize(f)
@fh = File.open(f)
end
def self.open(f)
MyFasta.new(f)
end
def rewind
@fh.rewind
end
def next_entry
return nil if @fh.eof?
pos = @fh.pos
e = @fh.readline()
while([email protected]?)
pos = @fh.pos
l = @fh.readline()
if l[0..0] == ">"
@fh.pos = pos
break
end
e += l
end
e
end
def each &block
while(e = next_entry())
yield(e)
end
end
end
test_fasta = MyFasta.open(ARGV[0])
count = 0
test_fasta.each_with_index do |entry,i|
puts "#{i} => #{entry.split(/\n/)[0]}"
puts "********************************"
count += 1
end
puts "Count = " + count.to_s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment