Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
120 views
in Technique[技术] by (71.8m points)

if(!x) vs if(x==false) in ruby

I don't understand the following code:

ruby-1.9.1-p378 > puts "nil is false" unless nil
nil is false
 => nil 
ruby-1.9.1-p378 > puts "nil isn't false" unless nil == false
nil isn't false
 => nil 

In most languages (of a C-base, at least), if(!cond) and if(cond==false) evaluate the same. What's going on here to make that not the case?

(I'd like the details of why, I understand that that is how it is.)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Ruby considers that false and nil are the only two "falsy" values, while everything else is "truthy". This is by definition and can not be modified (at least in MRI). This definition is used for all builtin operators like if, unless, while, until, cond ? if_truthy : if_falsey, ||, &&, ...

Writing foo == bar will always call the == method on foo with bar as an argument. By default, nil, false, true and all other immediates like symbols, etc..., are only equal to themselves. This could be changed, though:

def nil.==(bar)
  super || bar == false
end
puts "nil == false" if nil == false  # => "nil == false"

In Ruby 1.9, you can also redefine the operator !, so unless foo is not necessarily the same as if !foo or the contrary of if foo:

def true.!
  true
end

puts "True?"   if  true # => "True?"
puts "or not?" if !true # => "or not?"

Not that anybody would recommend doing anything like this...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...