Mercurial > repos > other > SevenLanguagesInSevenWeeks
changeset 8:700c167cad9f
Add some basic logic examples
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Mon, 02 Jan 2017 20:56:58 +0000 |
parents | b8f4577db289 |
children | 19fd9fd373d7 |
files | 1-Ruby/logic-irb.output 1-Ruby/logic-ruby.output 1-Ruby/logic.rb |
diffstat | 3 files changed, 91 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/1-Ruby/logic-irb.output Mon Jan 02 20:56:58 2017 +0000 @@ -0,0 +1,50 @@ +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>
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/1-Ruby/logic-ruby.output Mon Jan 02 20:56:58 2017 +0000 @@ -0,0 +1,7 @@ +x equals 4 +x equals 4 +x equals 4 +x not equal 5 +x not equal 5 +x not equal 5 +x equal 4
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/1-Ruby/logic.rb Mon Jan 02 20:56:58 2017 +0000 @@ -0,0 +1,34 @@ +#! /usr/bin/env ruby + +x = 4 + +# "if" block is "if… end" - no "then" required +if x == 4 + puts "x equals 4" +end + +# Python- and Perl-like following logic also works +puts "x equals 4" if x == 4 +puts "x equals 4" unless x != 4 + +# Negation has many forms. +# Simple logic: +if x != 5 + puts "x not equal 5" +end + +# Two options on negation +if !(x == 5) + puts "x not equal 5" +end + +if not x == 5 + puts "x not equal 5" +end + +# "unless" blocks instead of "if" +unless x != 4 + puts "x equal 4" +else + puts "x not equals 4" +end \ No newline at end of file