CMSC330

Ruby

Ruby

Intro
Typing
Object Orientation
Data Types and Syntax

Intro

Intro

Our First Program
# hello_world.rb
puts "Hello, World!"
          
  • Comments use #
  • no semicolons needed
  • puts to print
ruby hello_world.rb
          

Typing

Our Second Program
# typing.rb
y = "hello"
y = 3
z = 4
puts y + z
          
ruby typing.rb
          
  • No error?
  • No variable type?

Ruby uses dynamic and latent typing

Type Checking

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

Typing

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

Object Oriented Programming

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 Creation
class Square
  def initialize(size)
    @size = size
  end

  def area
    @size*@size
  end
end 

s = Square.new(5)
puts s.area
          
Class Creation
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 initialize(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 to_s
                s = "to string"
                return s
              end
              

              def to_s
                s = "to string"
                s
              end
              

              def to_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 to_s
                "String 1"  
              end

              def to_s
                "string 2" 
              end
              puts to_s
              
Fun Examples

Data Types and Syntax

Integers and Floats

          1.class       #Integer
          (2+3).class   #Integer
          1.0.class     #Float
          (1.5*2).class #Float 
          
Strings

          "hello"
          "hello".length

          #structual equality
          "hello" == "hello"

          # can substitute variables
          name = "cliff"
          puts "My name is #{name}"

          # can format strings
          puts sprintf("is %s %d?","cliff",18)
          
Symbols

          :"any_valid_string23"
          
          # like a string except
          # stored differently

          "structual" == "structural"
          "physical".equal? "physical"

          :structual == :structural
          :physical.equal? :physical
          
Arrays

          # 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
          
Hashes

          # 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
          
Conditionals

          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