SlideShare a Scribd company logo
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

More Related Content

What's hot (15)

PDF
OOP in PHP
Alena Holligan
 
PPTX
javascript
Kaya Ota
 
PDF
JRuby e DSL
jodosha
 
PDF
JavaScript - Chapter 8 - Objects
WebStackAcademy
 
PPTX
JavaScript Fundamentals & JQuery
Jamshid Hashimi
 
PDF
Elm dev front-end
Helder Pinto
 
PDF
Advanced Reflection in Pharo
Marcus Denker
 
PPTX
Object Oriented JavaScript - II
TO THE NEW | Technology
 
PPT
OOP in JavaScript
manugoel2003
 
PPT
Class 7 - PHP Object Oriented Programming
Ahmed Swilam
 
PPTX
Javascript Basics by Bonny
Bonny Chacko
 
PPT
04 inheritance
Sumit Srivastava
 
PPT
Php Oop
mussawir20
 
PPT
Xml dom & sax by bhavsingh maloth
Bhavsingh Maloth
 
OOP in PHP
Alena Holligan
 
javascript
Kaya Ota
 
JRuby e DSL
jodosha
 
JavaScript - Chapter 8 - Objects
WebStackAcademy
 
JavaScript Fundamentals & JQuery
Jamshid Hashimi
 
Elm dev front-end
Helder Pinto
 
Advanced Reflection in Pharo
Marcus Denker
 
Object Oriented JavaScript - II
TO THE NEW | Technology
 
OOP in JavaScript
manugoel2003
 
Class 7 - PHP Object Oriented Programming
Ahmed Swilam
 
Javascript Basics by Bonny
Bonny Chacko
 
04 inheritance
Sumit Srivastava
 
Php Oop
mussawir20
 
Xml dom & sax by bhavsingh maloth
Bhavsingh Maloth
 

Viewers also liked (20)

PPT
Group7
vishalkedia2009
 
PPTX
Ects 637, session seven, integration of global perspectives for marketing and...
Resort Opportunities(tm)
 
ODP
Digital Natives
guestbde5c5
 
PPT
Katanas Japonesas[1]
edgar
 
PPT
WCOM School Board Presentastion
Menache
 
DOC
Supremo Autobiography
erikasupremo
 
PDF
FT Investing In Turkey | Alan Greenhalgh
Shinesquad
 
PPTX
Skil storm testing at the speed of business 2
Glen Noesen
 
PPS
Thousand Islands
guest6067361
 
PDF
Effective Pair Programming
James Thompson
 
PPTX
Octubre
Olivia Romero
 
PDF
Programme guide en
Hélder Silva
 
PPTX
501, LP2 demonstration discovery - serps breakdown, saxe
Resort Opportunities(tm)
 
KEY
Learn Ruby 2011 - Session 1
James Thompson
 
PPTX
Richard Sykula
Richard Sykula
 
DOC
A New Paradigm In Linux Debug From Viosoft Corporation
art_lee
 
PDF
Representing Uncertainty in Situation Maps for Disaster Management
hje
 
PPTX
Prc Compassion La Intro
Tim Grant
 
PPTX
Relationship Building
Edventures1 Learning Solutions
 
PPTX
The Dancer voor een Gezonde Organisatie
thedancercoaching
 
Ects 637, session seven, integration of global perspectives for marketing and...
Resort Opportunities(tm)
 
Digital Natives
guestbde5c5
 
Katanas Japonesas[1]
edgar
 
WCOM School Board Presentastion
Menache
 
Supremo Autobiography
erikasupremo
 
FT Investing In Turkey | Alan Greenhalgh
Shinesquad
 
Skil storm testing at the speed of business 2
Glen Noesen
 
Thousand Islands
guest6067361
 
Effective Pair Programming
James Thompson
 
Octubre
Olivia Romero
 
Programme guide en
Hélder Silva
 
501, LP2 demonstration discovery - serps breakdown, saxe
Resort Opportunities(tm)
 
Learn Ruby 2011 - Session 1
James Thompson
 
Richard Sykula
Richard Sykula
 
A New Paradigm In Linux Debug From Viosoft Corporation
art_lee
 
Representing Uncertainty in Situation Maps for Disaster Management
hje
 
Prc Compassion La Intro
Tim Grant
 
Relationship Building
Edventures1 Learning Solutions
 
The Dancer voor een Gezonde Organisatie
thedancercoaching
 
Ad

Similar to Learn Ruby 2011 - Session 4 - Objects, Oh My! (20)

PDF
Ruby — An introduction
Gonçalo Silva
 
PPTX
Object-Oriented Programming with PHP (part 1)
Bozhidar Boshnakov
 
PPTX
Object Oriented Programming.pptx
SAICHARANREDDYN
 
PPTX
Ruby object model - Understanding of object play role for ruby
Tushar Pal
 
PDF
Introduction to Ruby Programming Language
Nicolò Calcavecchia
 
PPTX
About Python
Shao-Chuan Wang
 
PDF
A peek into Python's Metaclass and Bytecode from a Smalltalk User
Koan-Sin Tan
 
KEY
Metaprogramming Primer (Part 1)
Christopher Haupt
 
PPTX
Java
Raghu nath
 
PDF
Lecture 1 - Objects and classes
Syed Afaq Shah MACS CP
 
PPTX
From Ruby to Scala
tod esking
 
PPTX
Is2215 lecture2 student(2)
dannygriff1
 
PDF
Model Manipulation Using Embedded DSLs in Scala
Filip Krikava
 
PDF
Advanced Reflection in Pharo
Pharo
 
PPTX
JAVA Module 2____________________--.pptx
Radhika Venkatesh
 
PPTX
6 Object Oriented Programming
Deepak Hagadur Bheemaraju
 
PDF
The Dark Art of Rails Plugins (2008)
lazyatom
 
PPTX
Python 2. classes- cruciql for students objects1.pptx
KiranRaj648995
 
KEY
Ruby objects
Reuven Lerner
 
PPTX
Object Oriented Programming Class and Objects
rubini8582
 
Ruby — An introduction
Gonçalo Silva
 
Object-Oriented Programming with PHP (part 1)
Bozhidar Boshnakov
 
Object Oriented Programming.pptx
SAICHARANREDDYN
 
Ruby object model - Understanding of object play role for ruby
Tushar Pal
 
Introduction to Ruby Programming Language
Nicolò Calcavecchia
 
About Python
Shao-Chuan Wang
 
A peek into Python's Metaclass and Bytecode from a Smalltalk User
Koan-Sin Tan
 
Metaprogramming Primer (Part 1)
Christopher Haupt
 
Lecture 1 - Objects and classes
Syed Afaq Shah MACS CP
 
From Ruby to Scala
tod esking
 
Is2215 lecture2 student(2)
dannygriff1
 
Model Manipulation Using Embedded DSLs in Scala
Filip Krikava
 
Advanced Reflection in Pharo
Pharo
 
JAVA Module 2____________________--.pptx
Radhika Venkatesh
 
6 Object Oriented Programming
Deepak Hagadur Bheemaraju
 
The Dark Art of Rails Plugins (2008)
lazyatom
 
Python 2. classes- cruciql for students objects1.pptx
KiranRaj648995
 
Ruby objects
Reuven Lerner
 
Object Oriented Programming Class and Objects
rubini8582
 
Ad

More from James Thompson (13)

PPTX
Interfaces Not Required — RubyHACK 2018
James Thompson
 
PDF
Bounded Contexts for Legacy Code
James Thompson
 
PDF
Beyond Accidental Arcitecture
James Thompson
 
PPTX
Wrapping an api with a ruby gem
James Thompson
 
PPTX
Microservices for the Monolith
James Thompson
 
PDF
Mocking & Stubbing
James Thompson
 
KEY
Learn Ruby 2011 - Session 5 - Looking for a Rescue
James Thompson
 
KEY
Learn Ruby 2011 - Session 3
James Thompson
 
KEY
Learn Ruby 2011 - Session 2
James Thompson
 
KEY
Rails: Scaling Edition - Getting on Rails 3
James Thompson
 
KEY
Ruby For Web Development
James Thompson
 
KEY
Ruby Testing: Cucumber and RSpec
James Thompson
 
KEY
Introducing Ruby
James Thompson
 
Interfaces Not Required — RubyHACK 2018
James Thompson
 
Bounded Contexts for Legacy Code
James Thompson
 
Beyond Accidental Arcitecture
James Thompson
 
Wrapping an api with a ruby gem
James Thompson
 
Microservices for the Monolith
James Thompson
 
Mocking & Stubbing
James Thompson
 
Learn Ruby 2011 - Session 5 - Looking for a Rescue
James Thompson
 
Learn Ruby 2011 - Session 3
James Thompson
 
Learn Ruby 2011 - Session 2
James Thompson
 
Rails: Scaling Edition - Getting on Rails 3
James Thompson
 
Ruby For Web Development
James Thompson
 
Ruby Testing: Cucumber and RSpec
James Thompson
 
Introducing Ruby
James Thompson
 

Recently uploaded (20)

PDF
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
PDF
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
PDF
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
PDF
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PDF
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PDF
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
PDF
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PDF
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
PPTX
✨Unleashing Collaboration: Salesforce Channels & Community Power in Patna!✨
SanjeetMishra29
 
PPTX
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
PDF
Timothy Rottach - Ramp up on AI Use Cases, from Vector Search to AI Agents wi...
AWS Chicago
 
PDF
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
PDF
July Patch Tuesday
Ivanti
 
PDF
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
✨Unleashing Collaboration: Salesforce Channels & Community Power in Patna!✨
SanjeetMishra29
 
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
Timothy Rottach - Ramp up on AI Use Cases, from Vector Search to AI Agents wi...
AWS Chicago
 
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
July Patch Tuesday
Ivanti
 
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 

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

  • 1. Learn Ruby 2011 Session 4
  • 3. Objects, Oh My Objects, Attributes, Inheritance & More
  • 4. Questions Having any problems? Discover anything cool?
  • 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. Classes & Attributes class BookInStock 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 class BookInStock 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 class BookInStock 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 class BookInStock 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 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
  • 15. 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
  • 16. Classes & Attributes class BookInStock 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 • 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. Access Control class BookInStock #... private #... protected #... public #... end
  • 20. Access Control class BookInStock #... private :method1 protected :method2, :method3 public :method4 end
  • 21. Inheritance • Inheritance allows a 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 provide an 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 overcome the 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 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
  • 29. Next Week • More about Blocks and Iterators • Handling Errors
  • 30. Exercises • Work on specific Ruby Koans: • ruby about_classes.rb • ruby about_open_classes.rb • ruby about_inheritance.rb • ruby about_modules.rb

Editor's Notes