Skip to content

Instantly share code, notes, and snippets.

@mikecmpbll

mikecmpbll/qq.rb Secret

Last active January 19, 2016 13:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikecmpbll/1152878079ba0f175b24 to your computer and use it in GitHub Desktop.
Save mikecmpbll/1152878079ba0f175b24 to your computer and use it in GitHub Desktop.
def chunk
chunks = []
break_points.each_cons(2) do |start_byte, end_byte|
chunk = @file.read(end_byte - start_byte)
if block_given?
# more memory efficient, process the chunks one by one
yield chunk
else
chunks << chunk
end
end
end
def chunk2
# returns Enumerable lazy so we can process one chunk at a time.
break_points.each_cons(2).lazy.map do |start_byte, end_byte|
@file.read(end_byte - start_byte)
end
end
@apeiros
Copy link

apeiros commented Jan 19, 2016

IMO rewrite this as:

def chunk
  return enum_for(__method__) unless block_given?
  break_points.each_cons(2) do |start_byte, end_byte|
    yield(@file.read(end_byte - start_byte))
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment