Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Writing to console.log specifically #406

Open
rubyFeedback opened this issue Mar 3, 2024 · 2 comments
Open

Writing to console.log specifically #406

rubyFeedback opened this issue Mar 3, 2024 · 2 comments

Comments

@rubyFeedback
Copy link

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
Copy link
Author

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

@sfcgeorge
Copy link

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')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants