Learn Ruby 2011
     Session 4
Our Sponsors
Flatsourcing, iSeatz, Koda, and Launchpad
Objects, Oh My
Objects, Attributes, Inheritance & More
Questions
Having any problems? Discover anything cool?
Classes

• Classes are the blueprints of Objects
• Classes define the way an Object keeps
  state, what it’s value is
• Classes define the way an Object behaves,
  what methods it provides
Classes & Attributes

class BookInStock
  def initialize(isbn, price)
    @isbn = isbn
    @price = Float(price)
  end
end
Classes & Attributes


book = BookInStock.new(“1234”, 3.00)
p book

#<BookInStock:... @isbn=”1234”, @price=3.00>
Classes & Attributes

class BookInStock
  def initialize(isbn, price)
    @isbn = isbn
    @price = Float(price)
  end
  def to_s
    “ISBN: #{@isbn}, price: #{@price}”
  end
end
Classes & Attributes


book = BookInStock.new(“1234”, 3.00)
puts book

ISBN: 1234, price: 3.00
Classes & Attributes

class BookInStock
  attr_reader :isbn, :price

  def initialize(isbn, price)
    @isbn = isbn
    @price = Float(price)
  end
end
Classes & Attributes


book = BookInStock.new(“1234”, 3.00)
puts “ISBN: #{book.isbn}, price: #{book.price}”

ISBN: 1234, price: 3.00
Classes & Attributes

class BookInStock
  attr_reader :isbn
  attr_accessor :price

  def initialize(isbn, price)
    @isbn = isbn
    @price = Float(price)
  end
end
Classes & Attributes

book = BookInStock.new(“1234”, 3.00)
puts “ISBN: #{book.isbn}, price: #{book.price}”

ISBN: 1234, price: 3.00

book.price = 4.00
puts “ISBN: #{book.isbn}, price: #{book.price}”

ISBN: 1234, price: 4.00
Classes & Attributes
class BookInStock
  attr_reader :isbn
  #attr_accessor :price
  attr_reader :price
  attr_writer :price

  def initialize(isbn, price)
    @isbn = isbn
    @price = Float(price)
  end
end
Classes & Attributes
class BookInStock
  attr_reader :isbn, :price

  def initialize(isbn, price)
    @isbn = isbn
    @price = Float(price)
  end

  def price=(new_price)
    @price = Float(new_price)
  end
end
Classes & Attributes
class BookInStock
  def initialize(isbn, price)
    #...
    @@count += 1
  end

  #...

  def self.count
    @@count
  end
end
Classes & Attributes

book1 = BookInStock.new(“1234”, 3.00)
puts BookInStock.count

1

book2 = BookInStock.new(“1235”, 5.19)
book3 = BookInStock.new(“1236”, 4.00)
puts BookInStock.count

3
Access Control

• Public methods (the default) can be called by
  anyone in any context
• Protected methods can only be called by the
  Class that defines them or its Subclasses
• Private methods can only be called by the
  within the Class that defines them
Access Control

class BookInStock
  #...

private
  #...
protected
  #...
public
  #...
end
Access Control

class BookInStock
  #...

  private :method1
  protected :method2, :method3
  public :method4
end
Inheritance


• Inheritance allows a Subclass to inherit the
  behavior of it’s parent Class
• Ruby is a single-inheritance language
Inheritance

class BookOnSale < BookInStock
end

book = BookOnSale.new(“12349”, 1.00)
puts book
puts book.class

ISBN: 12349, price: 1.00
BookOnSale
Modules


• Modules provide an additional means of
  encapsulation
• Modules facilitate Mixins.
Modules
module Trig
  PI = 3.141592654

  def Trig.sin(x)
    #...
  end

  def Trig.cos(x)
    #...
  end
end

y = Trig.sin(Trig::PI/4)
Mixins

• Mixins overcome the limitations of single-
  inheritance
• Mixins allow behavior to be added to a class
  within requiring inheritance
Mixins
module OnSale
  def on_sale?
    true
  end
end

class BookOnSale < BookInStock
  include OnSale
end

b = BookOnSale.new(“12340”, 1.00)
puts “Price $#{b.price}!” unless b.on_sale?
puts “ONLY $#{b.price}!” if b.on_sale?

ONLY $1.00!
Reviewing

• Classes & Attributes
• Access Control
• Inheritance
• Modules
• Mixins
For Next Week
For the New to Programming

•   Read Chapters 9, 10, 11, 12 & 13 in LtP

•   Complete exercises for each chapter
For Everyone

•   Read Chapter 3, 4, 5 & 6 in PR1.9

•   Work through Ruby Koans: about_classes,
    about_open_classes, about_inheritance,
    about_modules
Next Week


• More about Blocks and Iterators
• Handling Errors
Exercises

• Work on specific Ruby Koans:
 • ruby about_classes.rb
 • ruby about_open_classes.rb
 • ruby about_inheritance.rb
 • ruby about_modules.rb

Learn Ruby 2011 - Session 4 - Objects, Oh My!

  • 1.
  • 2.
  • 3.
    Objects, Oh My Objects,Attributes, Inheritance & More
  • 4.
    Questions Having any problems?Discover anything cool?
  • 5.
    Classes • Classes arethe blueprints of Objects • Classes define the way an Object keeps state, what it’s value is • Classes define the way an Object behaves, what methods it provides
  • 6.
    Classes & Attributes classBookInStock def initialize(isbn, price) @isbn = isbn @price = Float(price) end end
  • 7.
    Classes & Attributes book= BookInStock.new(“1234”, 3.00) p book #<BookInStock:... @isbn=”1234”, @price=3.00>
  • 8.
    Classes & Attributes classBookInStock def initialize(isbn, price) @isbn = isbn @price = Float(price) end def to_s “ISBN: #{@isbn}, price: #{@price}” end end
  • 9.
    Classes & Attributes book= BookInStock.new(“1234”, 3.00) puts book ISBN: 1234, price: 3.00
  • 10.
    Classes & Attributes classBookInStock attr_reader :isbn, :price def initialize(isbn, price) @isbn = isbn @price = Float(price) end end
  • 11.
    Classes & Attributes book= BookInStock.new(“1234”, 3.00) puts “ISBN: #{book.isbn}, price: #{book.price}” ISBN: 1234, price: 3.00
  • 12.
    Classes & Attributes classBookInStock attr_reader :isbn attr_accessor :price def initialize(isbn, price) @isbn = isbn @price = Float(price) end end
  • 13.
    Classes & Attributes book= BookInStock.new(“1234”, 3.00) puts “ISBN: #{book.isbn}, price: #{book.price}” ISBN: 1234, price: 3.00 book.price = 4.00 puts “ISBN: #{book.isbn}, price: #{book.price}” ISBN: 1234, price: 4.00
  • 14.
    Classes & Attributes classBookInStock attr_reader :isbn #attr_accessor :price attr_reader :price attr_writer :price def initialize(isbn, price) @isbn = isbn @price = Float(price) end end
  • 15.
    Classes & Attributes classBookInStock attr_reader :isbn, :price def initialize(isbn, price) @isbn = isbn @price = Float(price) end def price=(new_price) @price = Float(new_price) end end
  • 16.
    Classes & Attributes classBookInStock def initialize(isbn, price) #... @@count += 1 end #... def self.count @@count end end
  • 17.
    Classes & Attributes book1= BookInStock.new(“1234”, 3.00) puts BookInStock.count 1 book2 = BookInStock.new(“1235”, 5.19) book3 = BookInStock.new(“1236”, 4.00) puts BookInStock.count 3
  • 18.
    Access Control • Publicmethods (the default) can be called by anyone in any context • Protected methods can only be called by the Class that defines them or its Subclasses • Private methods can only be called by the within the Class that defines them
  • 19.
    Access Control class BookInStock #... private #... protected #... public #... end
  • 20.
    Access Control class BookInStock #... private :method1 protected :method2, :method3 public :method4 end
  • 21.
    Inheritance • Inheritance allowsa Subclass to inherit the behavior of it’s parent Class • Ruby is a single-inheritance language
  • 22.
    Inheritance class BookOnSale <BookInStock end book = BookOnSale.new(“12349”, 1.00) puts book puts book.class ISBN: 12349, price: 1.00 BookOnSale
  • 23.
    Modules • Modules providean additional means of encapsulation • Modules facilitate Mixins.
  • 24.
    Modules module Trig PI = 3.141592654 def Trig.sin(x) #... end def Trig.cos(x) #... end end y = Trig.sin(Trig::PI/4)
  • 25.
    Mixins • Mixins overcomethe limitations of single- inheritance • Mixins allow behavior to be added to a class within requiring inheritance
  • 26.
    Mixins module OnSale def on_sale? true end end class BookOnSale < BookInStock include OnSale end b = BookOnSale.new(“12340”, 1.00) puts “Price $#{b.price}!” unless b.on_sale? puts “ONLY $#{b.price}!” if b.on_sale? ONLY $1.00!
  • 27.
    Reviewing • Classes &Attributes • Access Control • Inheritance • Modules • Mixins
  • 28.
    For Next Week Forthe New to Programming • Read Chapters 9, 10, 11, 12 & 13 in LtP • Complete exercises for each chapter For Everyone • Read Chapter 3, 4, 5 & 6 in PR1.9 • Work through Ruby Koans: about_classes, about_open_classes, about_inheritance, about_modules
  • 29.
    Next Week • Moreabout Blocks and Iterators • Handling Errors
  • 30.
    Exercises • Work onspecific Ruby Koans: • ruby about_classes.rb • ruby about_open_classes.rb • ruby about_inheritance.rb • ruby about_modules.rb