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

1,270 views
1,160 views

Published on

In this session we covered Classes, Modules and Mixins in more depth. Ruby's inheritance mechanism was covered, as well as the uses of instance and class variables.

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,270
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
18
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Learn Ruby 2011 - Session 4 - Objects, Oh My!

    1. 1. Learn Ruby 2011 Session 4
    2. 2. Our SponsorsFlatsourcing, iSeatz, Koda, and Launchpad
    3. 3. Objects, Oh MyObjects, Attributes, Inheritance & More
    4. 4. QuestionsHaving any problems? Discover anything cool?
    5. 5. 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
    6. 6. Classes & Attributesclass BookInStock def initialize(isbn, price) @isbn = isbn @price = Float(price) endend
    7. 7. Classes & Attributesbook = BookInStock.new(“1234”, 3.00)p book#<BookInStock:... @isbn=”1234”, @price=3.00>
    8. 8. Classes & Attributesclass BookInStock def initialize(isbn, price) @isbn = isbn @price = Float(price) end def to_s “ISBN: #{@isbn}, price: #{@price}” endend
    9. 9. Classes & Attributesbook = BookInStock.new(“1234”, 3.00)puts bookISBN: 1234, price: 3.00
    10. 10. Classes & Attributesclass BookInStock attr_reader :isbn, :price def initialize(isbn, price) @isbn = isbn @price = Float(price) endend
    11. 11. Classes & Attributesbook = BookInStock.new(“1234”, 3.00)puts “ISBN: #{book.isbn}, price: #{book.price}”ISBN: 1234, price: 3.00
    12. 12. Classes & Attributesclass BookInStock attr_reader :isbn attr_accessor :price def initialize(isbn, price) @isbn = isbn @price = Float(price) endend
    13. 13. Classes & Attributesbook = BookInStock.new(“1234”, 3.00)puts “ISBN: #{book.isbn}, price: #{book.price}”ISBN: 1234, price: 3.00book.price = 4.00puts “ISBN: #{book.isbn}, price: #{book.price}”ISBN: 1234, price: 4.00
    14. 14. Classes & Attributesclass BookInStock attr_reader :isbn #attr_accessor :price attr_reader :price attr_writer :price def initialize(isbn, price) @isbn = isbn @price = Float(price) endend
    15. 15. Classes & Attributesclass BookInStock attr_reader :isbn, :price def initialize(isbn, price) @isbn = isbn @price = Float(price) end def price=(new_price) @price = Float(new_price) endend
    16. 16. Classes & Attributesclass BookInStock def initialize(isbn, price) #... @@count += 1 end #... def self.count @@count endend
    17. 17. Classes & Attributesbook1 = BookInStock.new(“1234”, 3.00)puts BookInStock.count1book2 = BookInStock.new(“1235”, 5.19)book3 = BookInStock.new(“1236”, 4.00)puts BookInStock.count3
    18. 18. 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
    19. 19. Access Controlclass BookInStock #...private #...protected #...public #...end
    20. 20. Access Controlclass BookInStock #... private :method1 protected :method2, :method3 public :method4end
    21. 21. Inheritance• Inheritance allows a Subclass to inherit the behavior of it’s parent Class• Ruby is a single-inheritance language
    22. 22. Inheritanceclass BookOnSale < BookInStockendbook = BookOnSale.new(“12349”, 1.00)puts bookputs book.classISBN: 12349, price: 1.00BookOnSale
    23. 23. Modules• Modules provide an additional means of encapsulation• Modules facilitate Mixins.
    24. 24. Modulesmodule Trig PI = 3.141592654 def Trig.sin(x) #... end def Trig.cos(x) #... endendy = Trig.sin(Trig::PI/4)
    25. 25. Mixins• Mixins overcome the limitations of single- inheritance• Mixins allow behavior to be added to a class within requiring inheritance
    26. 26. Mixinsmodule OnSale def on_sale? true endendclass BookOnSale < BookInStock include OnSaleendb = 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. 27. Reviewing• Classes & Attributes• Access Control• Inheritance• Modules• Mixins
    28. 28. For Next WeekFor the New to Programming• Read Chapters 9, 10, 11, 12 & 13 in LtP• Complete exercises for each chapterFor Everyone• Read Chapter 3, 4, 5 & 6 in PR1.9• Work through Ruby Koans: about_classes, about_open_classes, about_inheritance, about_modules
    29. 29. Next Week• More about Blocks and Iterators• Handling Errors
    30. 30. Exercises• Work on specific Ruby Koans: • ruby about_classes.rb • ruby about_open_classes.rb • ruby about_inheritance.rb • ruby about_modules.rb

    ×