Skip to content

Instantly share code, notes, and snippets.

@muhammadyana
Last active June 9, 2017 13:33
Show Gist options
  • Save muhammadyana/5a1bbdb176c302551da4a628fa83fc0b to your computer and use it in GitHub Desktop.
Save muhammadyana/5a1bbdb176c302551da4a628fa83fc0b to your computer and use it in GitHub Desktop.
#take every sentence
counts = Hash.new(0)
File.foreach("sample.txt") do
|line|
line.scan(/\w+/) do
|word|
word = word.downcase
counts[word] += 1
end
end
counts.keys.sort.each{
|k|
p "#{k}:#{counts[k]}"
}
#another way take every sentece in file
words = Fiber.new do
File.foreach("sample.txt") do
|line|
line.scan(/\w+/) do
|word|
Fiber.yield word.downcase
end
end
nil
end
counts = Hash.new(0)
while word = words.resume
counts[word] += 1
end
counts.keys.sort.each {
|k|
p "#{k}:#{counts[k]}"
}
two = Fiber.new do
num = 2
loop do
Fiber.yield(num) unless num % 3 == 8
num += 2
end
end
2.times {print two.resume, " "}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment