Mercurial > repos > other > SevenLanguagesInSevenWeeks
view 1-Ruby/logic.rb @ 103:98be775c533c default tip
An odd "non-determinism" example from StackOverflow
It is clever, but doesn't make much sense as to how it gets its results
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sun, 14 Jul 2019 13:44:13 +0100 |
parents | e5b84cc7bc29 |
children |
line wrap: on
line source
#! /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 # "then" is optional, except in single line statement if x == 4 then puts "x equal 4" end if x == 4 then puts "x equal 4" end if x == 4 puts "x equal 4" end # The following line gives: # logic.rb:…: syntax error, unexpected tIDENTIFIER, expecting keyword_then or ';' or '\n' # if x == 4 puts "x equal 4" end # # if x == 4 puts "x equal 4" end