# hello_world.rb
puts "Hello, World!"
ruby hello_world.rb
# typing.rb
y = "hello"
y = 3
z = 4
puts y + z
ruby typing.rb
Ruby uses dynamic and latent typing
Type Checking: The process of determining a variable's type
Dynamic typing: type checking is performed at run-time
Static typing: type checking is performed at compile-time
Manifest (explicit) typing: explicitly telling the compiler the type of new variables
Types are associated with variables
Latent (implicit) typing: not needing to give a type to a variable
Types are associated with values
Everything is a class
a = "Hello"
a.class
Including Primitives
3.class
Objects have methods
3.methods
Even Ruby's equivalent to Java's Null
puts nil.to_s
Reminders about Object Oriented Programming
Values are references to Objects
Objects are instances of Classes
Classes inherit from each other
class Square
def initialize(size)
@size = size
end
def area
@size*@size
end
end
s = Square.new(5)
puts s.area
class Square
def initialize(size)
@size = size
end
def area
@size*@size
end
end
s = Square.new(5)
puts s.area
public class Square{
private int size;
public Square(size){
this.size = size;
}
public int area(){
return size*size;
}
}
public static void main(String[] args){
Square s = new Square(5);
System.out.println(s.area);
}
Things to Note
Instance variables are private by default
last line in a method is returned
No Method Overloading
Things to Note
Instance variables are private by default
# getter
def x
@x
end
# setter
def x=(x)
@x=x
end
# instead
attr_accessor :x
last line in a method is returned
No Method Overloading
Things to Note
Instance variables are private by default
last line in a method is returned
def my_s
s = "to string"
return s
end
def my_s
s = "to string"
s
end
def my_s
"to string"
end
No Method Overloading
Things to Note
Instance variables are private by default
last line in a method is returned
No Method Overloading
def my_s
"String 1"
end
def my_s
"string 2"
end
puts my_s
1.class #Integer
(2+3).class #Integer
1.0.class #Float
(1.5*2).class #Float
"hello"
"hello".length
#structural equality
"hello" == "hello"
# can substitute variables
name = "cliff"
puts "My name is #{name}"
# can format strings
puts sprintf("is %s %d?","cliff",18)
:"any_valid_string23"
# like a string except
# stored differently
"structural" == "structural"
"physical".equal? "physical"
:structural == :structural
:physical.equal? :physical
# bracket syntax
arr = [1,2,3,4]
arr = []
# can be heterogenous
arr = [1,"hello",3.0]
# OOP behaviour too
arr = Array.new
arr = Array.new(3)
arr = Array.new(3,"a")
# bracket indexing
arr = [1,2,3,4]
puts arr[1]
puts arr[-1]
# dynamic size
arr = []
arr[3] = 3
arr[5] = 6
puts arr
arr.delete_at(1)
puts arr
arr.delete(3)
puts arr
# multidimensional
a = []
a[0] = []
puts a
a[][] # error
a = Array.new(3){Array.new(3)}
# like sets
a = [1,2,3,4]
b = [4,5,6,7]
puts a+b
puts a|b
puts a&b
puts a-b
# stack and queue behaviour
a = [1,2,3]
a.push(4)
a.pop
a.unshift(0)
a.shift
# Curly Brace syntax
h = {}
h = {"key"=>"value",}
# bracket indexing
puts h["key"]
h[:key2] = "value2"
# defaulting and nil
puts h[3] # is nil
h2 = Hash.new(5)
puts h2['a'] # is 5
# multidimensional
h = {}
h[0] = {}
h[0][0] = 1
puts h
puts true.class
puts false.class
#syntax
if false
puts "not here"
elsif nil
puts "nor here"
else
puts "Here I am"
end
# while loop
count = 0
while count < 5
puts count
count += 1
end
# for loop
# must go through an iterator
a = [1,2,3]
for value in a
puts value
end