Mercurial > repos > other > SevenLanguagesInSevenWeeks
view 1-Ruby/logic-irb.output @ 11:e0a92558400f
Add some truthiness testing
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Mon, 02 Jan 2017 21:13:00 +0000 |
parents | 700c167cad9f |
children | e5b84cc7bc29 |
line wrap: on
line source
logic.rb(main):001:0> #! /usr/bin/env ruby logic.rb(main):002:0* logic.rb(main):003:0* x = 4 => 4 logic.rb(main):004:0> logic.rb(main):005:0* # "if" block is "if… end" - no "then" required logic.rb(main):006:0* if x == 4 logic.rb(main):007:1> puts "x equals 4" logic.rb(main):008:1> end x equals 4 => nil logic.rb(main):009:0> logic.rb(main):010:0* # Python- and Perl-like following logic also works logic.rb(main):011:0* puts "x equals 4" if x == 4 x equals 4 => nil logic.rb(main):012:0> puts "x equals 4" unless x != 4 x equals 4 => nil logic.rb(main):013:0> logic.rb(main):014:0* # Negation has many forms. logic.rb(main):015:0* # Simple logic: logic.rb(main):016:0* if x != 5 logic.rb(main):017:1> puts "x not equal 5" logic.rb(main):018:1> end x not equal 5 => nil logic.rb(main):019:0> logic.rb(main):020:0* # Two options on negation logic.rb(main):021:0* if !(x == 5) logic.rb(main):022:1> puts "x not equal 5" logic.rb(main):023:1> end x not equal 5 => nil logic.rb(main):024:0> logic.rb(main):025:0* if not x == 5 logic.rb(main):026:1> puts "x not equal 5" logic.rb(main):027:1> end x not equal 5 => nil logic.rb(main):028:0> logic.rb(main):029:0* # "unless" blocks instead of "if" logic.rb(main):030:0* unless x != 4 logic.rb(main):031:1> puts "x equal 4" logic.rb(main):032:1> else logic.rb(main):033:1* puts "x not equals 4" logic.rb(main):034:1> endlogic.rb(main):034:1> x equal 4 => nil logic.rb(main):034:0>