Ruby Programming

This page contains some of my notes on Ruby programming... it is not intended as a 'Tutorial'
I have some scripts set up for easy command line testing, linked with Notepad++ like my regular tutorials... Get them here:
Download the Devtools


Bases

Decimal 100
0d100
Hexadecimal 0x1FF
Binary 0b10101010
Octal 0o1234567

Operators

Operatior 
Description Example
+ Add a + b will give 30
Subtract a - b will give -10
* Multiply a * b will give 200
/ Divide b / a will give 2
% Modulus − Get Remainder b % a will give 0
** Exponent − Power a**b will give 10 to the power 20
= Simple assignment operator, assigns values from right side operands to left side operand. c = a + b will assign the value of a + b into c
+= Add AND assignment operator, adds right operand to the left operand and assign the result to left operand. c += a is equivalent to c = c + a
-= Subtract AND assignment operator, subtracts right operand from the left operand and assign the result to left operand. c -= a is equivalent to c = c - a
*= Multiply AND assignment operator, multiplies right operand with the left operand and assign the result to left operand. c *= a is equivalent to c = c * a
/= Divide AND assignment operator, divides left operand with the right operand and assign the result to left operand. c /= a is equivalent to c = c / a
%= Modulus AND assignment operator, takes modulus using two operands and assign the result to left operand. c %= a is equivalent to c = c % a
**= Exponent AND assignment operator, performs exponential (power) calculation on operators and assign value to the left operand. c **= a is equivalent to c = c ** a
= Simple assignment operator, assigns values from right side operands to left side operand. c = a + b will assign the value of a + b into c
+= Add AND assignment operator, adds right operand to the left operand and assign the result to left operand. c += a is equivalent to c = c + a
-= Subtract AND assignment operator, subtracts right operand from the left operand and assign the result to left operand. c -= a is equivalent to c = c - a
== Checks if the value of two operands are equal or not, if yes then condition becomes true. (a == b) is not true.
=== Used to test equality within a when clause of a case statement. (1...10) === 5 returns true.
!= Checks if the value of two operands are equal or not, if values are not equal then condition becomes true. (a != b) is true.
> Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. (a > b) is not true.
< Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. (a < b) is true.
>= Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. (a >= b) is not true.
<= Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. (a <= b) is true.
<=> Combined comparison operator. Returns 0 if first operand equals second, 1 if first operand is greater than the second and -1 if first operand is less than the second. (a <=> b) returns -1.
.eql? True if the receiver and argument have both the same type and equal values. 1 == 1.0 returns true, but 1.eql?(1.0) is false.
equal? True if the receiver and argument have the same object id. if aObj is duplicate of bObj then aObj == bObj is true, a.equal?bObj is false but a.equal?aObj is true.
=> Hashmap literal definition key1 => value1
& Binary AND Operator copies a bit to the result if it exists in both operands. (a & b) will give 12, which is 0000 1100
| Binary OR Operator copies a bit if it exists in either operand. (a | b) will give 61, which is 0011 1101
^ Binary XOR Operator copies the bit if it is set in one operand but not both. (a ^ b) will give 49, which is 0011 0001
~ Binary Ones Complement Operator is unary and has the effect of 'flipping' bits. (~a ) will give -61, which is 1100 0011 in 2's complement form due to a signed binary number.
<< Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand. a << 2 will give 240, which is 1111 0000
>> Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand. a >> 2 will give 15, which is 0000 1111
? : Conditional Expression If Condition is true ? Then value X : Otherwise value Y
.. Creates a range from start point to end point inclusive. 1..10 Creates a range from 1 to 10 inclusive.
... Creates a range from start point to end point exclusive. 1...10 Creates a range from 1 to 9.
=~ matches a String against a regular expression, returns the position/index if found else nil "Hello World" =~ /Wo/
!~ 'Does not contain� "Hello World" !~ /Mo/




Notes

Print Hello World puts "Hello World!"
Print Character putc "!"
String concatenation str = "Billy" + " Bob"
Rem statement # puts "don't show this"
Multiple commands on one line test ="hello"; puts test
Comma in numbers 10,000 test =10_000;puts test
Using Quotes in strings puts "Quote: \"!"
puts %(Quote: "!)
Get user input name=gets
puts "Hello "+name
Remove a newline name=gets.chomp
Print variable puts "value = #{var}"
puts "value = " + var
v= var.to_s
Loop for i in 0..5
    puts i
end
Loop [1,2].each do |i|
  puts i
end
Loop Break for i in 0..10
    if i==5
        break
    end
    puts i
end
Loop Next
While i = 1
while i<=3 do
  puts i; i+=1
end
until begin
i+=1
end until i>4
Break while i  = 0
while true do
  puts i;   i+= 1
  break if i>5
end
If if var<3
    puts "Low"
elsif var<10
    puts "Mid"
else
    puts "High"
end
If Not unless 2==1
  puts 'Not True'
end
case case var
when 1
    puts "one"
when 2..4
    puts "2/3/4"
else
    puts "something"
end
function with arg def myfunc(param = "default",param2=123t)
end

myfunc(1,2)
Return a value def test
    return "Hello World"
end
def test2
    "Goodbye cruel world"
end
 
puts test
puts test2
set multiple objects to the same object a=b=c
create identifier as symbol object :name
Run a program Load "File.rb"
NULL / Empty object nil
INC / DEC V+=1
V-=1
Convert object to string to_s
convert integer to float
convert string to integer
9 / 5.to_f
"7".to_i
New object A= MyObj.new
Class definition class MyObj
  def initialize(name)
    @myname=name
    puts "Created "+name
  end
end

o = MyObj.new("Test")
function of the class #in MyObj
  def myname
    return @myname
  end

puts "My Name is "+o.myname
Local (instance var) @var
Class Var
(Shared across all instances of class)
class MyObj
  def initialize(name)
    @@myname=name
    puts "Created "+name
  end
  def name ;   return @@myname ;  end
end
o = MyObj.new("Test1") ; p = MyObj.new("Test2")
puts o.name ; puts p.name
Module
(Prevents namespace clashes)
module MyMod
    MyVar="Test"
end
puts MyMod::MyVar
Include file
(for modules)
require "/sources/mymod.rb"
puts MyMod::MyVar
Global var $myglobal=1
Constant
(upper case first letter
Const=1
Current object self
Random
Psuedorandom
rand
srand
File load File.open("testfile.txt") do |f|
    lines=f.readlines
    lines.each do |l|
        puts l
    end
end
Substring (mid) test="<<<Hello>>>"
puts test[3...8]
puts test[-3,3]
Length,
Uppercase
Lowercase
puts test.length
puts test.upcase
puts test.downcase
contains Instr puts test =~ /llo/
Multiline string definition str = <<TEST
Line 1
Line 2
Line 3
TEST
puts str
2D Array box=Array.new(3){ Array.new(3,'-') }
box[0][0]="["; box[0][2]="["; box[2][0]="]"; box[2][2]="]"
for y in 0..2
    for x in 0..2 ; putc box[x][y] ; end
    puts ""
end
Paralell assignment black,white=0,255



 

View Options
Default Dark
Simple (Hide this menu)
Print Mode (white background)

Top Menu
***Main Menu***
Youtube channel
Patreon
Introduction to Assembly (Basics for absolute beginners)
Amazon Affiliate Link
AkuSprite Editor
ChibiTracker
Dec/Bin/Hex/Oct/Ascii Table

Alt Tech
Archive.org
Bitchute
Odysee
Rumble
DailyMotion
Please note: I wlll upload more content to these alt platforms based on the views they bring in

Z80 Content
***Z80 Tutorial List***
Learn Z80 Assembly (2021)
Learn Z80 Assembly (old)
Hello World
Simple Samples
Advanced Series
Multiplatform Series
Platform Specific Series
ChibiAkumas Series
Grime Z80
Z80 Downloads
Z80 Cheatsheet
Sources.7z
DevTools kit
Z80 Platforms
Amstrad CPC
Elan Enterprise
Gameboy & Gameboy Color
Master System & GameGear
MSX & MSX2
Sam Coupe
TI-83
ZX Spectrum
Spectrum NEXT
Camputers Lynx

6502 Content
***6502 Tutorial List***
Learn 6502 Assembly
Advanced Series
Platform Specific Series
Hello World Series
Simple Samples
Grime 6502
6502 Downloads
6502 Cheatsheet
Sources.7z
DevTools kit
6502 Platforms
Apple IIe
Atari 800 and 5200
Atari Lynx
BBC Micro
Commodore 64
Commodore PET
Commander x16
Super Nintendo (SNES)
Nintendo NES / Famicom
PC Engine (Turbografx-16)
Vic 20

68000 Content
***68000 Tutorial List***
Learn 68000 Assembly
Hello World Series
Platform Specific Series
Simple Samples
Grime 68000
68000 Downloads
68000 Cheatsheet
Sources.7z
DevTools kit
68000 Platforms
Amiga 500
Atari ST
Neo Geo
Sega Genesis / Mega Drive
Sinclair QL
X68000 (Sharp x68k)

8086 Content
Learn 8086 Assembly
Platform Specific Series
Hello World Series
Simple Samples
8086 Downloads
8086 Cheatsheet
Sources.7z
DevTools kit
8086 Platforms
Wonderswan
MsDos

ARM Content
Learn ARM Assembly
Learn ARM Thumb Assembly
Platform Specific Series
Hello World
Simple Samples
ARM Downloads
ARM Cheatsheet
Sources.7z
DevTools kit
ARM Platforms
Gameboy Advance
Nintendo DS
Risc Os

Risc-V Content
Learn Risc-V Assembly
Risc-V Downloads
Risc-V Cheatsheet
Sources.7z
DevTools kit

MIPS Content
Learn Risc-V Assembly
Platform Specific Series
Hello World
Simple Samples
MIPS Downloads
MIPS Cheatsheet
Sources.7z
DevTools kit
MIPS Platforms
Playstation
N64

PDP-11 Content
Learn PDP-11 Assembly
Platform Specific Series
Simple Samples
PDP-11 Downloads
PDP-11 Cheatsheet
Sources.7z
DevTools kit
PDP-11 Platforms
PDP-11
UKNC

TMS9900 Content
Learn TMS9900 Assembly
Platform Specific Series
Hello World
TMS9900 Downloads
TMS9900 Cheatsheet
Sources.7z
DevTools kit
TMS9900 Platforms
Ti 99

6809 Content
Learn 6809 Assembly
Learn 6309 Assembly
Platform Specific Series
Hello World Series
Simple Samples
6809 Downloads
6809/6309 Cheatsheet
Sources.7z
DevTools kit
6809 Platforms
Dragon 32/Tandy Coco
Fujitsu FM7
TRS-80 Coco 3
Vectrex

65816 Content
Learn 65816 Assembly
Hello World
Simple Samples
65816 Downloads
65816 Cheatsheet
Sources.7z
DevTools kit
65816 Platforms
SNES

eZ80 Content
Learn eZ80 Assembly
Platform Specific Series
eZ80 Downloads
eZ80 Cheatsheet
Sources.7z
DevTools kit
eZ80 Platforms
Ti84 PCE

IBM370 Content
Learn IBM370 Assembly
Simple Samples
IBM370 Downloads
IBM370 Cheatsheet
Sources.7z
DevTools kit

Super-H Content
Learn SH2 Assembly
Hello World Series
Simple Samples
SH2 Downloads
SH2 Cheatsheet
Sources.7z
DevTools kit
SH2 Platforms
32x
Saturn

PowerPC Content
Learn PowerPC Assembly
Hello World Series
Simple Samples
PowerPC Downloads
PowerPC Cheatsheet
Sources.7z
DevTools kit
PowerPC Platforms
Gamecube

Work in Progress
ChibiAndroids

Misc bits
Ruby programming









Buy my Assembly programming book
on Amazon in Print or Kindle!


Buy my Assembly programming book





Available worldwide!
Search 'ChibiAkumas' on
your local Amazon website!
Click here for more info!


























































































Buy my Assembly programming book
on Amazon in Print or Kindle!


Buy my Assembly programming book





Available worldwide!
Search 'ChibiAkumas' on
your local Amazon website!
Click here for more info!



































































































Buy my Assembly programming book
on Amazon in Print or Kindle!


Buy my Assembly programming book





Available worldwide!
Search 'ChibiAkumas' on
your local Amazon website!
Click here for more info!