view 1-Ruby/logic-irb.output @ 97:85a5e9a6ef5c

Add more notes on extracting Just/Maybe values
author IBBoard <dev@ibboard.co.uk>
date Sun, 30 Jun 2019 16:09:44 +0100
parents e5b84cc7bc29
children
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> end
x equal 4
=> nil
logic.rb(main):035:0> 
logic.rb(main):036:0* # "then" is optional, except in single line statement
logic.rb(main):037:0* if x == 4 then puts "x equal 4" end
x equal 4
=> nil
logic.rb(main):038:0> if x == 4 then
logic.rb(main):039:1* 	puts "x equal 4"
logic.rb(main):040:1> end
x equal 4
=> nil
logic.rb(main):041:0> if x == 4
logic.rb(main):042:1> 	puts "x equal 4"
logic.rb(main):043:1> end
x equal 4
=> nil
logic.rb(main):044:0> # The following line gives:
logic.rb(main):045:0* #   logic.rb:…: syntax error, unexpected tIDENTIFIER, expecting keyword_then or ';' or '\n'
logic.rb(main):046:0* #   if x == 4 puts "x equal 4" end
logic.rb(main):047:0* #
logic.rb(main):048:0* # if x == 4 puts "x equal 4" endlogic.rb(main):048:0# 
logic.rb(main):048:0# 
logic.rb(main):048:0# 
=> nil