BernardK

BernardK

Programming Ruby 3.2 (5th Edition): B1.0 page 205, Only then do we create

@noelrappin

page 205, paragraph after the code, line 2 :

then blocks at Ractor.receive. Only then do we create the reading ractor

Because of “Only then” I thought that creating the second Ractor could only take place when the first is waiting on receive. But the trace shows that both Ractors are created, then the blocks are scheduled.

On page 204 (par. 5, code for one, line 1) I liked “Moving down the file”, for a possible replacement of “Only then”.

Having said that, it’s impressive to see the reader sending several times without waiting for a take.

def displayWord(p_word)
    p_word.nil? ? "^nil^" : "|#{p_word}|"
end
    
puts "*** (main) about to create counter"
counter = Ractor.new(name: "counter") do
  puts ">>> ccccc in counter block"
  result = Hash.new(0)
  while (word = Ractor.receive)
    puts "ccccc in counter received word=#{displayWord(word)}"
    result[word] += 1
    puts "ccccc looping"
  end
  puts "ccccc in counter while ended word=#{displayWord(word)}"
  result
end

puts "*** (main) about to create reader"
Ractor.new(counter, name: "reader") do |counter|
  puts ">>> rrrrr in reader block"
  File.foreach("testfile") do |line|
    puts "rrrrr line=#{line}"
    line.scan(/\w+/) do |word|
      puts "rrrrr about to send word=#{displayWord(word)}"
      counter.send(word.downcase)
      puts "rrrrr after send"
    end
  end
  puts "rrrrr about to send nil"
  counter.send(nil)
end

puts "*** (main) about to take counter"
counts = counter.take
counts.keys.sort.each { |k| print "#{k}:#{counts[k]} " }
puts
% ruby -w ractor_word_count_flipped.rb 
*** (main) about to create counter
<internal ... experimental ...
*** (main) about to create reader
*** (main) about to take counter
>>> ccccc in counter block
>>> rrrrr in reader block
rrrrr line=This is line one
rrrrr about to send word=|This|
rrrrr after send
rrrrr about to send word=|is|
rrrrr after send
rrrrr about to send word=|line|
ccccc in counter received word=|this|
...

Popular Prag Prog topics Top

Alexandr
Hi everyone! There is an error on the page 71 in the book “Programming machine learning from coding to depp learning” P. Perrotta. You c...
New
raul
Hi Travis! Thank you for the cool book! :slight_smile: I made a list of issues and thought I could post them chapter by chapter. I’m rev...
New
joepstender
The generated iex result below should list products instead of product for the metadata. (page 67) iex&gt; product = %Product{} %Pento....
New
swlaschin
The book has the same “Problem space/Solution space” diagram on page 18 as is on page 17. The correct Problem/Solution space diagrams ar...
New
leba0495
Hello! Thanks for the great book. I was attempting the Trie (chap 17) exercises and for number 4 the solution provided for the autocorre...
New
adamwoolhether
When trying to generate the protobuf .go file, I receive this error: Unknown flag: --go_opt libprotoc 3.12.3 MacOS 11.3.1 Googling ...
New
patoncrispy
I’m new to Rust and am using this book to learn more as well as to feed my interest in game dev. I’ve just finished the flappy dragon exa...
New
fynn
This is as much a suggestion as a question, as a note for others. Locally the SGP30 wasn’t available, so I ordered a SGP40. On page 53, ...
New
jskubick
I’m under the impression that when the reader gets to page 136 (“View Data with the Database Inspector”), the code SHOULD be able to buil...
New
andreheijstek
After running /bin/setup, the first error was: The foreman' command exists in these Ruby versions: That was easy to fix: gem install fore...
New

Other popular topics Top

AstonJ
poll poll Be sure to check out @Dusty’s article posted here: An Introduction to Alternative Keyboard Layouts It’s one of the best write-...
New
AstonJ
I have seen the keycaps I want - they are due for a group-buy this week but won’t be delivered until October next year!!! :rofl: The Ser...
New
AstonJ
Do the test and post your score :nerd_face: :keyboard: If possible, please add info such as the keyboard you’re using, the layout (Qw...
New
PragmaticBookshelf
“A Mystical Experience” Hero’s Journey with Paolo Perrotta @nusco Ever wonder how authoring books compares to writing articles?...
New
AstonJ
We’ve talked about his book briefly here but it is quickly becoming obsolete - so he’s decided to create a series of 7 podcasts, the firs...
New
AstonJ
If you get Can't find emacs in your PATH when trying to install Doom Emacs on your Mac you… just… need to install Emacs first! :lol: bre...
New
AstonJ
If you want a quick and easy way to block any website on your Mac using Little Snitch simply… File &gt; New Rule: And select Deny, O...
New
husaindevelop
Inside our android webview app, we are trying to paste the copied content from another app eg (notes) using navigator.clipboard.readtext ...
New
PragmaticBookshelf
Author Spotlight: Bruce Tate @redrapids Programming languages always emerge out of need, and if that’s not always true, they’re defin...
New
PragmaticBookshelf
A Ruby-Centric Chat with Noel Rappin @noelrappin Once you start noodling around with Ruby you quickly figure out, as Noel Rappi...
New

Latest in PragProg

View all threads ❯