Mercurial > repos > other > SevenLanguagesInSevenWeeks
diff 1-Ruby/logic.rb @ 8:700c167cad9f
Add some basic logic examples
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Mon, 02 Jan 2017 20:56:58 +0000 |
parents | |
children | e5b84cc7bc29 |
line wrap: on
line diff
--- /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