mardi 24 mars 2015

Why everything is getting true expect false in ruby if condition (e.g: puts "Hello World" if 1)

In Ruby,


Why all the true, numberic,floats and strings are getting true while checking with if condition, Except false?


Examples:



1.9.3-p551 :031 > puts "Hello World" if 0
Hello World
=> nil

1.9.3-p551 :032 > puts "Hello World" if 1
Hello World
=> nil

1.9.3-p551 :033 > puts "Hello World" if 2
Hello World
=> nil

1.9.3-p551 :034 > puts "Hello World" if 200
Hello World
=> nil

1.9.3-p551 :035 > puts "Hello World" if "Ruby"
(irb):35: warning: string literal in condition
Hello World
=> nil

1.9.3-p551 :036 > puts "Hello World" if "Rails"
(irb):36: warning: string literal in condition
Hello World
=> nil
1.9.3-p551 :037 > puts "Hello World" if true
Hello World
=> nil


Except



1.9.3-p551 :038 > puts "Hello World" if false
=> nil
1.9.3-p551 :039 >

Aucun commentaire:

Enregistrer un commentaire