:note

!! で true

Sinatra Best Practices: Part One | The Carbon Emitter をみていて、今まで !! ってつかったことなかったのでメモ

jsx(javascript)

#target "illustrator"
var session = {};
session['usr'] = "hello";
session['undefined_usr']; // undefined, nullでも同様

$.writeln( session['usr'] );    // "hello"
$.writeln( !session['usr'] );   // false
$.writeln( !!session['usr'] );  // true
$.writeln( session['undefined_usr'] );    // undefined
$.writeln( !session['undefined_usr'] );   // true
$.writeln( !!session['undefined_usr'] );  // false

ruby

session = {}
session[:usr] = "hello"
session[:nousr] = nil

p session[:usr]     # "hello"
p !session[:usr]    # false
p !!session[:usr]   # true
p session[:nousr]     # nil
p !session[:nousr]    # true
p !!session[:nousr]   # false

あいまいな判定をきっちり Booleanでできる。

 
comments powered by Disqus
back to top