I have a function which prosecces first N files in the direcory:
def restore(cnt)
$LOG.debug "store_engine : restore tweets from cache (cnt = #{cnt})"
result = TweetCollection.new
Dir["cache/*"].each do |path|
cnt = cnt - 1
File.open(path) do |f|
result.append(Tweet.construct(:friends, :yaml, f.read))
end
if cnt == 0
return result
end
end
result
end
I’m just wondering if there is a more ruby-way method to write this function?
Slice the array with
[]and useinjectto collect allTweetobjects in theTweetCollection. UseFile.readto return the contents of the file at the givenpathin one method call.I have also replaced your global variable by an instance variable; I don’t know the context of your method, so this may not be possible.