Presentation is loading. Please wait.

Presentation is loading. Please wait.

OOP n Ruby 101 By: Dr. Mathias Fonkam 1.

Similar presentations


Presentation on theme: "OOP n Ruby 101 By: Dr. Mathias Fonkam 1."— Presentation transcript:

1 OOP n Ruby 101 By: Dr. Mathias Fonkam 1

2 Topics Why OOP? Key ideas & Principles: Example Ruby class code & use
Classes Objects Identity State Behavior object relationships object interactions class relationships Encapsulation Inheritance Example Ruby class code & use Example with Inheritance Ruby Object Model Objects in Ruby…. (Posted whole Chapter on this!)

3 Why Objects & OOP? To help manage growing complexity of software development & maintenance modularization Abstraction code reuse

4 Key OOP Ideas & Principles: example view
Classes Objects Identity State Behavior object relationships

5 Classes & Objects: UML diagram – Encapsulation!

6 Class/Object: code & use
# good_dog.rb class GoodDog def initialize(name) @name = name end def get_name @name def set_name(name) # can be written as: name=(n) def speak says arf!" sparky = GoodDog.new("Sparky") puts sparky.speak puts sparky.get_name sparky.set_name = "Spartacus"

7 Class/Object Relationships: Inheritance
# p033mammal.rb  class Mammal     def breathe       puts "inhale and exhale"     end   end      class Cat < Mammal     def speak       puts "Meow"   rani = Cat.new   rani.breathe   rani.speak  

8 Class/Object Relationships: Inheritance – UML?
# p035inherit.rb .. continued # class F sub-class of GF   class F < GF    def initialize      puts 'In F class'    end   end      # class S sub-class of F   class S < F     puts 'In S class'   son = S.new   son.gfmethod   # p035inherit.rb class GF     def initialize      puts 'In GF class'    end     def gfmethod      puts 'GF method call'    end   end     # tbc

9 Ruby Object Model

10 Ruby Object Model in Details

11 Objects in Ruby Where is puts from? # simple_ints.rb puts 6.object_id
# simple.rb puts "Ruby language" Where is puts from? # simple_ints.rb puts 6.object_id puts 6.even? puts 6.zero? puts 6.class # simple2.rb Kernel.puts "Ruby language" Kernel.puts "Ruby language".size

12 Objects in Ruby: object creation
May be implicit (with object literals) or explicit # Example literals.rb 4.times { puts "Ruby" } puts "Ruby".size puts "Ruby".downcase puts [1, 2, 3].include? 3 puts [1, 2, 3].empty? puts :name.class puts :name.frozen? puts (1..6).class puts (1..6).include? 4 # Example formal.rb s1 = String.new "Ruby" puts s1.size puts s1.downcase a1 = Array.new a1.push 1, 2, 3 puts a1.include? 3 puts a1.empty? r1 = Range.new 1, 6 puts r1.class puts r1.include? 4

13 Objects in Ruby: the toplevel – main
May be implicit (with object literals) or explicit # Example toplevel.rb – lies outside everything n1 = 3 n2 = 5 puts local_variables Kernel.puts self puts self.class

14 Objects in Ruby: the toplevel – main
# Example toplevel2.rb – run & try to explain # =========== Another example========= @name = "Jane" @age = 17 def info is years old" end puts self.instance_variables puts self.private_methods.include? :info puts info


Download ppt "OOP n Ruby 101 By: Dr. Mathias Fonkam 1."

Similar presentations


Ads by Google