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|
...

First Post!

noelrappin

noelrappin

Author of Modern Front-End Development for Rails

Thanks!

Where Next?

Popular Pragmatic Bookshelf topics Top

jimschubert
In Chapter 3, the source for index introduces Config on page 31, followed by more code including tests; Config isn’t introduced until pag...
New
jeffmcompsci
Title: Design and Build Great Web APIs - typo “https://company-atk.herokuapp.com/2258ie4t68jv” (page 19, third bullet in URL list) Typo:...
New
jdufour
Hello! On page xix of the preface, it says there is a community forum "… for help if your’re stuck on one of the exercises in this book… ...
New
Chrichton
Dear Sophie. I tried to do the “Authorization” exercise and have two questions: When trying to plug in an email-service, I found the ...
New
brian-m-ops
#book-python-testing-with-pytest-second-edition Hi. Thanks for writing the book. I am just learning so this might just of been an issue ...
New
AndyDavis3416
@noelrappin Running the webpack dev server, I receive the following warning: ERROR in tsconfig.json TS18003: No inputs were found in c...
New
jgchristopher
“The ProductLive.Index template calls a helper function, live_component/3, that in turn calls on the modal component. ” Excerpt From: Br...
New
adamwoolhether
I’m not quite sure what’s going on here, but I’m unable to have to containers successfully complete the Readiness/Liveness checks. I’m im...
New
Charles
In general, the book isn’t yet updated for Phoenix version 1.6. On page 18 of the book, the authors indicate that an auto generated of ro...
New
redconfetti
Docker-Machine became part of the Docker Toolbox, which was deprecated in 2020, long after Docker Desktop supported Docker Engine nativel...
New

Other popular topics Top

Exadra37
I am asking for any distro that only has the bare-bones to be able to get a shell in the server and then just install the packages as we ...
New
Exadra37
Oh just spent so much time on this to discover now that RancherOS is in end of life but Rancher is refusing to mark the Github repo as su...
New
PragmaticBookshelf
Build highly interactive applications without ever leaving Elixir, the way the experts do. Let LiveView take care of performance, scalabi...
New
New
PragmaticBookshelf
Author Spotlight James Stanier @jstanier James Stanier, author of Effective Remote Work , discusses how to rethink the office as we e...
New
PragmaticBookshelf
Author Spotlight Rebecca Skinner @RebeccaSkinner Welcome to our latest author spotlight, where we sit down with Rebecca Skinner, auth...
New
New
AnfaengerAlex
Hello, I’m a beginner in Android development and I’m facing an issue with my project setup. In my build.gradle.kts file, I have the foll...
New
NewsBot
Node.js v22.14.0 has been released. Link: Release 2025-02-11, Version 22.14.0 'Jod' (LTS), @aduh95 · nodejs/node · GitHub
New
mindriot
Ok, well here are some thoughts and opinions on some of the ergonomic keyboards I have, I guess like mini review of each that I use enoug...
New

Sub Categories: