Add `to_b` and `to_h` to `JS::Object`
terrablue opened this issue · 3 comments
terrablue commented
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" }
kateinoigakukun commented
For to_b
, I intentionally didn't add it. See #247 for details.
terrablue commented
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?
kateinoigakukun commented
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.