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

Feature: puts & p can handle native JS objects not wrapped by Native for simple debugging/troubleshooting #2630

Open
AndyObtiva opened this issue Jan 14, 2024 · 0 comments
Labels

Comments

@AndyObtiva
Copy link

AndyObtiva commented Jan 14, 2024

Is your feature request related to a problem? Please describe.
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]

With Opal 1.8.2, if I try to use puts or p on a native JS object like {a: 1, b: 2}, the Opal application crashes with either an error about respond_to? not existing to test existence of to_s, or inspect not existing.

Trying this Ruby code in an Opal frontend (Rails Asset Pipeline Sprockets Opal app):

p `{a: 1, b: 2}`

Causes this error:

kernel.source.rb:631 Uncaught TypeError: obj.$inspect is not a function
    at $$15 (kernel.source.rb:631:39)
    at Opal.yield1 (runtime.source.js:1715:1)
    at Array.$$each (array.source.rb:991:1)
    at Opal.send2 (runtime.source.js:1958:1)
    at Opal.send (runtime.source.js:1946:1)
    at klass.$$p (kernel.source.rb:631:9)
    at Opal.modules.sample_selector (sample_selector.source.rb:6:1)
    at Opal.load_normalized (runtime.source.js:2752:1)
    at Opal.require (runtime.source.js:2787:1)
    at (index):46:6

Trying this Ruby code in Opal:

puts `{a: 1, b: 2}`

Causes this error:

helpers.source.rb:20 Uncaught TypeError: object.$respond_to? is not a function
    at Function.$Opal_coerce_to$ques$2 (helpers.source.rb:20:25)
    at Function.$$String (kernel.source.rb:807:11)
    at klass.$$puts (io.source.rb:76:32)
    at Opal.send2 (runtime.source.js:1958:1)
    at Opal.send (runtime.source.js:1946:1)
    at klass.$$puts (kernel.source.rb:627:12)
    at Opal.modules.sample_selector (sample_selector.source.rb:7:1)
    at Opal.load_normalized (runtime.source.js:2752:1)
    at Opal.require (runtime.source.js:2787:1)
    at (index):46:6

This can be very frustrating for Ruby developers used to puts and p who are new to Opal and might have received a JS/JSON object from a Web API ajax call or some third party JS library. Those methods not working when a simple console.log works in JS drives Opal newbs crazy!

Describe the solution you'd like
A clear and concise description of what you want to happen.

I think this can be handled automatically by puts & p by having them defer to console.log(nativeObj) automatically if the Ruby way of handling things fails (no respond_to? method, to_s method, or inspect method).

@hmdne said the following about this solution in the Opal Slack channel:

hmdne: yes and I will surely think about this idea, because I like it 😄

Describe alternatives you've considered
A clear and concise description of any alternative solutions or features you've considered.

For now, I can drop down to native JS with ticks and use console.log in this situation.

Additional context
Add any other context or screenshots about the feature request here.

JRuby tries to shield Ruby developers from Java details as much as possible. It would be cool if at least, in puts && p, Opal developers are shielded from JavaScript low-level details relating to the Ruby implementations of these methods like not having to_s, respond_to? or inspect. Defaulting to console.log in those situations should be a good enough solution to avoid driving Opal newbies crazy.

@AndyObtiva AndyObtiva changed the title Feature: puts, p, and pp can handle native JS objects not wrapped by Native for simple debugging/troubleshooting Feature: puts & p can handle native JS objects not wrapped by Native for simple debugging/troubleshooting Jan 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant