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

jimmykiang
This test is broken right out of the box… — FAIL: TestAgent (7.82s) agent_test.go:77: Error Trace: agent_test.go:77 agent_test.go:...
New
ianwillie
Hello Brian, I have some problems with running the code in your book. I like the style of the book very much and I have learnt a lot as...
New
alanq
This isn’t directly about the book contents so maybe not the right forum…but in some of the code apps (e.g. turbo/06) it sends a TURBO_ST...
New
oaklandgit
Hi, I completed chapter 6 but am getting the following error when running: thread 'main' panicked at 'Failed to load texture: IoError(O...
New
tkhobbes
After some hassle, I was able to finally run bin/setup, now I have started the rails server but I get this error message right when I vis...
New
Keton
When running the program in chapter 8, “Implementing Combat”, the printout Health before attack was never printed so I assumed something ...
New
a.zampa
@mfazio23 I’m following the indications of the book and arriver ad chapter 10, but the app cannot be compiled due to an error in the Bas...
New
bjnord
Hello @herbert ! Trying to get the very first “Hello, Bracket Terminal!" example to run (p. 53). I develop on an Amazon EC2 instance runn...
New
mcpierce
@mfazio23 I’ve applied the changes from Chapter 5 of the book and everything builds correctly and runs. But, when I try to start a game,...
New
roadbike
From page 13: On Python 3.7, you can install the libraries with pip by running these commands inside a Python venv using Visual Studio ...
New

Other popular topics Top

ohm
Which, if any, games do you play? On what platform? I just bought (and completed) Minecraft Dungeons for my Nintendo Switch. Other than ...
New
New
PragmaticBookshelf
Rust is an exciting new programming language combining the power of C with memory safety, fearless concurrency, and productivity boosters...
New
AstonJ
Just done a fresh install of macOS Big Sur and on installing Erlang I am getting: asdf install erlang 23.1.2 Configure failed. checking ...
New
AstonJ
Inspired by this post from @Carter, which languages, frameworks or other tech or tools do you think is killing it right now? :upside_down...
New
DevotionGeo
The V Programming Language Simple language for building maintainable programs V is already mentioned couple of times in the forum, but I...
New
mafinar
This is going to be a long an frequently posted thread. While talking to a friend of mine who has taken data structure and algorithm cou...
New
PragmaticBookshelf
Author Spotlight Rebecca Skinner @RebeccaSkinner Welcome to our latest author spotlight, where we sit down with Rebecca Skinner, auth...
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
PragmaticBookshelf
Author Spotlight: Sophie DeBenedetto @SophieDeBenedetto The days of the traditional request-response web application are long gone, b...
New

Sub Categories: