Skip to content

kernel: Kernel#putc + #p/#print/#puts forward to $stdout (core/kernel +15) - #604

Merged
tannevaled merged 2 commits into
mainfrom
wave22/kernel
Sep 10, 2026
Merged

tannevaled merged 2 commits into
mainfrom
wave22/kernel

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

What

Kernel output methods did not honour a reassigned or mocked $stdout, #p lost its multi-argument return value and rendered container elements through the built-in formatter, and Kernel#putc was missing entirely.

  • Kernel#putc — new. Coerces its argument like rb_io_putc (a String's first character, else the low byte of #to_int; nil/false/true raise TypeError) and delivers the one-character String through $stdout#write, so a mocked $stdout or a closed stream (IOError) is observed. Registered in the module_function set → private instance method of Kernel and public Kernel module method.
  • Kernel#puts — forwards to $stdout.puts (rb_f_puts) so a reassigned/mocked $stdout receives the call, with the recv==$stdout guard writing straight to the object to avoid recursing.
  • Kernel#print — writes through $stdout#write (rb_f_print → rb_io_print), applying $_ (no-arg), field separator $, and record separator $\.
  • Kernel#p — returns the argument for one, a new Array for several, and renders each value through its own #inspect (rb_inspect), coercing a non-String result with #to_s (rb_obj_as_string) — so an overridden #inspect and per-element Array/Hash #inspect are honoured.
  • A $stdout answering no #write (which MRI rejects at assignment but rbgo tolerates) falls back to the underlying stream rather than raising.

Bibliography

ruby/ruby v3_4_0: io.c (rb_f_putc, rb_f_puts, rb_f_print, rb_io_print, rb_io_putc), object.c (rb_inspect, rb_obj_as_string). ruby/spec core/kernel/{putc,p,print,puts}_spec.rb and shared/io/putc.rb. Every change verified byte-for-byte against MRI 4.0.5.

Result (ruby/spec core/kernel, MRI 4.0.5 oracle)

spec before after
putc_spec 0 11
p_spec 6 8
print_spec 3 4
puts_spec 2 3

Suite total: 1829 → 1844 passing (+15); fail 194→184, error 128→123, no regressions. The one remaining p_spec failure ("flushes output if receiver is a File", a # TODO in ruby/spec itself) is File write-buffering, out of scope here.

Tests / gates

  • go build ./... and GOWORK=off go test ./... green (pre-existing 127.0.0.1:1 network tests aside).
  • New whitebox tests give 100% coverage of every new branch (nativeP/nativePrint/nativePuts/pInspect and the putc closure).
  • gofmt -l internal/vm/ empty; wasip1 build OK.
  • Updated one stale pinned test (p of a non-String #inspect now coerces via #to_s, matching MRI).

🤖 Generated with Claude Code

… +15)

Kernel#p/#print/#puts/#putc did not honour a reassigned or mocked $stdout, #p
lost the array return value and rendered container elements through the built-in
formatter instead of dispatching #inspect, and Kernel#putc was missing entirely.

- Add Kernel#putc: coerces its argument like rb_io_putc (a String's first
  character, else the low byte of #to_int; nil/false/true raise TypeError) and
  delivers the one-character String through $stdout#write, so a mocked $stdout or
  a closed stream (IOError) is observed. Registered in the module_function set so
  it is a private instance method of Kernel and a public Kernel module method.
- Kernel#puts forwards to $stdout.puts (rb_f_puts) so a reassigned/mocked $stdout
  receives the call, with the recv==$stdout guard writing straight to the object
  to avoid recursing.
- Kernel#print writes through $stdout#write (rb_f_print -> rb_io_print), applying
  $_ (no-arg), the field separator $, and the record separator $\.
- Kernel#p returns the argument for one, a new Array for several, and renders each
  value through its own #inspect (rb_inspect), coercing a non-String result with
  #to_s (rb_obj_as_string) — so an overridden #inspect and per-element Array/Hash
  #inspect are honoured.
- A $stdout that answers no #write (which MRI rejects at assignment but rbgo
  tolerates) falls back to the underlying stream rather than raising.

References: ruby/ruby v3_4_0 io.c (rb_f_putc, rb_f_puts, rb_f_print, rb_io_print,
rb_io_putc), object.c (rb_inspect, rb_obj_as_string). Verified byte-for-byte
against MRI 4.0.5. Updated one stale pinned test (p of a non-String #inspect now
coerces via #to_s, as MRI does). core/kernel: putc 0->11, p 6->8, print 3->4,
puts 2->3; ruby/spec core/kernel passing 1829 -> 1844.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@tannevaled
tannevaled force-pushed the wave22/kernel branch 2 times, most recently from f131dfe to 861bc4d Compare September 10, 2026 09:25
…erwise

pInspect dispatched #inspect unconditionally. The Go-backed binding values
(Matrix, NDArray, IPAddr, BigDecimal, Date, Bag, Rolify) register no Ruby
#inspect of their own, so dispatch rendered them through the generic one and
left every Go Inspect() unreachable — 0.0% on the CI merged profile, dropping
the 100% coverage gate below the line on both POSIX lanes while windows (same
tests, no gate) stayed green.

Dispatch for RObject and for Array/Hash — whose Ruby #inspect renders each
element through the element's own #inspect, so  still shows an
overridden one — and use the value's Go Inspect() otherwise, as inspectStr does.

Verified vs MRI 4.0.5 (10/10): IPAddr/BigDecimal/Date render byte-identically;
overridden #inspect on a plain object, on a built-in subclass, a non-String
#inspect coerced with #to_s, and per-element inspect inside both Array and Hash
all match. Every binding Inspect() is back at 100.0%, level with main.
core/kernel +15.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@tannevaled
tannevaled merged commit ca745db into main Sep 10, 2026
7 checks passed
@tannevaled
tannevaled deleted the wave22/kernel branch September 10, 2026 19:47
tannevaled added a commit that referenced this pull request Sep 10, 2026
…606)

First run past 20,000 passing ruby/spec examples.

Wave-22 merged three bibliography-first conformance PRs:
- #605 core/io: IO.pipe encodings, IO#write/write_nonblock transcoding, bom|
  handling, off_t RangeError, IO.foreach $./$_, IO#readpartial, IO.copy_stream
  with duck-typed sources and destinations — +77.
- #604 core/kernel: Kernel#putc (new), #p/#print/#puts forwarding through
  $stdout so a mocked stream observes them, #p rendering per MRI rb_inspect — +15.
- #603 VM: OpOpenSingletonClass now resets defaultVis, so a private in one
  'class << self' body no longer leaks into a later reopened one — +4.

Measured total on merged main: 20093 passing. Local measure at TIMEOUT=25;
CI runs TIMEOUT=60 so its total is >= this floor.

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant