ruby.wasm icon indicating copy to clipboard operation
ruby.wasm copied to clipboard

Writing to console.log specifically

Open rubyFeedback opened this issue 1 year ago • 2 comments

In the FAQ currently we have code like this:

 $stdout = Object.new.tap { |obj|
  def obj.write(i)
    JS.global[:document].write(i)
  end
}

Then we can simply use puts, which is convenient.

But how to write to console.log?

Past this point if we use:

JS.global[:document].write "foobar"

It will no longer write to console.log, instead just to primary stdout in the browser.

Is there a way to still write to console.log somehow? Could this be added to FAQ?

If not, could there be a way to show this? I'd like to get debug-like output right via console.log() as well, so being able to write onto console.log even when having modified $stdout, would be convenient, in my opinion. Could such a way be added in addition to ruby.wasm?

rubyFeedback avatar Mar 03 '24 08:03 rubyFeedback

Hmm it seems "pp" can be used to do so. Still I think it may be useful to add it to a FAQ entry.

On a semi-related note, when I use "pp", the console also says:

Ignoring debug-1.9.1 because its extensions are not built. Try: gem pristine debug --version 1.9

And so forth. Not sure why. I am using:

https://cdn.jsdelivr.net/npm/@ruby/[email protected]/dist/browser.script.iife.js

rubyFeedback avatar Mar 03 '24 09:03 rubyFeedback

console is a JS global just like document so you can access it the same:

JS.global[:console].log('hello')

Feel free to make whatever shortcut you find comfortable:

# global variable
$console = JS.global[:console]
$console.log('hello')

# Kernel patch
# (this is where `puts` is defined too)
module Kernel
  def console
    JS.global[:console]
  end
end

console.log('hello')
console.error('bad')

sfcgeorge avatar May 06 '24 22:05 sfcgeorge