Closed sposin closed 8 years ago
So, predicate methods returning nil
values were mentioned before (like #245). Ruby doesn't really have an opinion on this. The community does.
Since nil
is a falsy value just like false
itself, I don't think we should coerce values. Just don't type check on your end and you should be fine. If you do need boolean values, you can always !!value
on your side too.
About using String#match?
vs String#include?
, can you point me to places where we can replace the former with the latter? I'll do some benchmarks to see if it's faster, and if so I can change all references.
If you do need boolean values, you can always !!value on your side too.
It's not so much about "needing" boolean values as it is a consistent API:
rails [3.2.21] (rails32-test-app)$ Browser.new("").device.public_methods(false).select { |n| n.to_s.end_with?("?") }.map { |n| p "#{n}: #{b.device.public_send(n).inspect}" }
"tablet?: false"
"mobile?: nil"
"ipad?: false"
"unknown?: true"
"ipod_touch?: false"
"ipod?: false"
"iphone?: false"
"ps3?: false"
"playstation3?: false"
"ps4?: false"
"playstation4?: false"
"psp?: false"
"playstation_vita?: false"
"vita?: false"
"psp_vita?: false"
"kindle?: false"
"kindle_fire?: false"
"nintendo_wii?: false"
"wii?: false"
"nintendo_wiiu?: false"
"wiiu?: false"
"blackberry_playbook?: false"
"playbook?: false"
"surface?: false"
"tv?: false"
"silk?: nil"
"xbox?: nil"
"xbox_360?: false"
"xbox_one?: false"
"playstation?: false"
"nintendo?: false"
"console?: false"
In this case 91% of the methods return true
or false
and the other 9% return true
or nil
.
Happy bug hunting! 🐛 🐞 :neckbeard:
P.S. You should update the docs or at least define your interpretation of Boolean
.
This just bit me. It's unexpected behavior. The downsides have been pointed out and I struggle to see any upside. I'd appreciate ensuring the API always returns a bool for all those ?
methods.
Using browser 2.1.0
When the browser is InternetExplorer a check for
safari?
returnsnil
instead offalse
. It seems to work fine with Chrome and Edge.Question, why on the various
.match?
methods do you use regex which returns nil rather than.include?