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

Add `to_b` and `to_h` to `JS::Object`

Open terrablue opened this issue 2 years ago • 3 comments

I would like to suggest adding to_b and to_h to JS::Object, to convert into a ruby boolean respective hash.

to_b → boolean Converts self to true or false:

JS.eval("return true").to_b        # => true
JS.eval("return false").to_b       # => false

to_h → hash Converts self to a Hash:

JS.eval("return {foo: 'bar'}").to_h         # => { "foo" => "bar" }

terrablue avatar Dec 16 '23 16:12 terrablue

For to_b, I intentionally didn't add it. See https://github.com/ruby/ruby.wasm/pull/247 for details.

kateinoigakukun avatar Dec 16 '23 16:12 kateinoigakukun

I understand. So in that case I can implement it myself for my use case. What about to_h? It's already used in this meaning in ruby, no?

terrablue avatar Dec 16 '23 16:12 terrablue

Right, to_h sounds reasonable to me. I think that would be something like https://github.com/ruby/ruby.wasm/pull/270. Note that the conversion should be shallow, not recursive, to follow the usual Ruby to_h convention. See OpenStruct#to_h as an example.

kateinoigakukun avatar Dec 16 '23 16:12 kateinoigakukun