ruby/ruby.wasm

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

terrablue opened this issue · 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" }

For to_b, I intentionally didn't add it. See #247 for details.

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?

Right, to_h sounds reasonable to me. I think that would be something like #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.