How to make a Ruby variable that will evaluate to false in conditional expressions

I want my variable (proxy object) to evaluate to false when used in conditional sentences. I know I can use .nil? and even var == nil, but I think this is not very good. I am trying to do this:

if myproxy # not only myprroxy.nil? or myproxy == nil, but just that # myproxy backend object is not nil else # myproxy backend object is nil end 

Any ideas?

+4
source share
1 answer

The only two objects that evaluate to false-ish in a boolean context in Ruby are nil , a single instance of NilClass and false , a single instance of FalseClass . Every other object evaluates the true value.

If you want the else branch to be accepted, then myproxy should evaluate to nil or false .

If you want something logical, that you have actual control, you can try the case expression and redefine the case subquery operator MyProxy#=== .

+4
source

Source: https://habr.com/ru/post/1299628/


All Articles