Created
October 14, 2011 14:09
-
-
Save delagoya/1287217 to your computer and use it in GitHub Desktop.
Example for each() Enumerable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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