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

OOP in PHP
OOP in PHPOOP in PHP
OOP in PHP
Alena Holligan
 
javascript
javascript javascript
javascript
Kaya Ota
 
JRuby e DSL
JRuby e DSLJRuby e DSL
JRuby e DSL
jodosha
 
JavaScript - Chapter 8 - Objects
 JavaScript - Chapter 8 - Objects JavaScript - Chapter 8 - Objects
JavaScript - Chapter 8 - Objects
WebStackAcademy
 
JavaScript Fundamentals & JQuery
JavaScript Fundamentals & JQueryJavaScript Fundamentals & JQuery
JavaScript Fundamentals & JQuery
Jamshid Hashimi
 
Elm dev front-end
Elm   dev front-endElm   dev front-end
Elm dev front-end
Helder Pinto
 
Advanced Reflection in Pharo
Advanced Reflection in PharoAdvanced Reflection in Pharo
Advanced Reflection in Pharo
Marcus Denker
 
Object Oriented JavaScript - II
Object Oriented JavaScript - IIObject Oriented JavaScript - II
Object Oriented JavaScript - II
TO THE NEW | Technology
 
OOP in JavaScript
OOP in JavaScriptOOP in JavaScript
OOP in JavaScript
manugoel2003
 
Class 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented ProgrammingClass 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented Programming
Ahmed Swilam
 
Javascript Basics by Bonny
Javascript Basics by BonnyJavascript Basics by Bonny
Javascript Basics by Bonny
Bonny Chacko
 
04 inheritance
04 inheritance04 inheritance
04 inheritance
Sumit Srivastava
 
Php Oop
Php OopPhp Oop
Php Oop
mussawir20
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Xml dom & sax by bhavsingh maloth
Xml dom & sax by bhavsingh malothXml dom & sax by bhavsingh maloth
Xml dom & sax by bhavsingh maloth
Bhavsingh Maloth
 

What's hot (15)

OOP in PHP
OOP in PHPOOP in PHP
OOP in PHP
 
javascript
javascript javascript
javascript
 
JRuby e DSL
JRuby e DSLJRuby e DSL
JRuby e DSL
 
JavaScript - Chapter 8 - Objects
 JavaScript - Chapter 8 - Objects JavaScript - Chapter 8 - Objects
JavaScript - Chapter 8 - Objects
 
JavaScript Fundamentals & JQuery
JavaScript Fundamentals & JQueryJavaScript Fundamentals & JQuery
JavaScript Fundamentals & JQuery
 
Elm dev front-end
Elm   dev front-endElm   dev front-end
Elm dev front-end
 
Advanced Reflection in Pharo
Advanced Reflection in PharoAdvanced Reflection in Pharo
Advanced Reflection in Pharo
 
Object Oriented JavaScript - II
Object Oriented JavaScript - IIObject Oriented JavaScript - II
Object Oriented JavaScript - II
 
OOP in JavaScript
OOP in JavaScriptOOP in JavaScript
OOP in JavaScript
 
Class 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented ProgrammingClass 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented Programming
 
Javascript Basics by Bonny
Javascript Basics by BonnyJavascript Basics by Bonny
Javascript Basics by Bonny
 
04 inheritance
04 inheritance04 inheritance
04 inheritance
 
Php Oop
Php OopPhp Oop
Php Oop
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
Xml dom & sax by bhavsingh maloth
Xml dom & sax by bhavsingh malothXml dom & sax by bhavsingh maloth
Xml dom & sax by bhavsingh maloth
 

Viewers also liked

Group7
Group7Group7
Ects 637, session seven, integration of global perspectives for marketing and...
Ects 637, session seven, integration of global perspectives for marketing and...Ects 637, session seven, integration of global perspectives for marketing and...
Ects 637, session seven, integration of global perspectives for marketing and...
Resort Opportunities(tm)
 
Digital Natives
Digital NativesDigital Natives
Digital Natives
guestbde5c5
 
Katanas Japonesas[1]
Katanas Japonesas[1]Katanas Japonesas[1]
Katanas Japonesas[1]
edgar
 
WCOM School Board Presentastion
WCOM School Board PresentastionWCOM School Board Presentastion
WCOM School Board Presentastion
Menache
 
Supremo Autobiography
Supremo AutobiographySupremo Autobiography
Supremo Autobiographyerikasupremo
 
FT Investing In Turkey | Alan Greenhalgh
FT Investing In Turkey | Alan GreenhalghFT Investing In Turkey | Alan Greenhalgh
FT Investing In Turkey | Alan Greenhalgh
Shinesquad
 
Skil storm testing at the speed of business 2
Skil storm testing at the speed of business 2Skil storm testing at the speed of business 2
Skil storm testing at the speed of business 2
Glen Noesen
 
Thousand Islands
Thousand IslandsThousand Islands
Thousand Islands
guest6067361
 
Effective Pair Programming
Effective Pair ProgrammingEffective Pair Programming
Effective Pair Programming
James Thompson
 
Programme guide en
Programme guide enProgramme guide en
Programme guide en
Hélder Silva
 
501, LP2 demonstration discovery - serps breakdown, saxe
501, LP2 demonstration   discovery - serps breakdown, saxe501, LP2 demonstration   discovery - serps breakdown, saxe
501, LP2 demonstration discovery - serps breakdown, saxe
Resort Opportunities(tm)
 
Learn Ruby 2011 - Session 1
Learn Ruby 2011 - Session 1Learn Ruby 2011 - Session 1
Learn Ruby 2011 - Session 1
James Thompson
 
Richard Sykula
Richard SykulaRichard Sykula
Richard Sykula
Richard Sykula
 
A New Paradigm In Linux Debug From Viosoft Corporation
A New Paradigm In Linux Debug From Viosoft CorporationA New Paradigm In Linux Debug From Viosoft Corporation
A New Paradigm In Linux Debug From Viosoft Corporation
art_lee
 
Representing Uncertainty in Situation Maps for Disaster Management
Representing Uncertainty in Situation Maps for Disaster ManagementRepresenting Uncertainty in Situation Maps for Disaster Management
Representing Uncertainty in Situation Maps for Disaster Management
hje
 
Prc Compassion La Intro
Prc Compassion La IntroPrc Compassion La Intro
Prc Compassion La Intro
Tim Grant
 
Relationship Building
Relationship BuildingRelationship Building
Relationship Building
Edventures1 Learning Solutions
 
The Dancer voor een Gezonde Organisatie
The Dancer voor een Gezonde OrganisatieThe Dancer voor een Gezonde Organisatie
The Dancer voor een Gezonde Organisatiethedancercoaching
 

Viewers also liked (20)

Group7
Group7Group7
Group7
 
Ects 637, session seven, integration of global perspectives for marketing and...
Ects 637, session seven, integration of global perspectives for marketing and...Ects 637, session seven, integration of global perspectives for marketing and...
Ects 637, session seven, integration of global perspectives for marketing and...
 
Digital Natives
Digital NativesDigital Natives
Digital Natives
 
Katanas Japonesas[1]
Katanas Japonesas[1]Katanas Japonesas[1]
Katanas Japonesas[1]
 
WCOM School Board Presentastion
WCOM School Board PresentastionWCOM School Board Presentastion
WCOM School Board Presentastion
 
Supremo Autobiography
Supremo AutobiographySupremo Autobiography
Supremo Autobiography
 
FT Investing In Turkey | Alan Greenhalgh
FT Investing In Turkey | Alan GreenhalghFT Investing In Turkey | Alan Greenhalgh
FT Investing In Turkey | Alan Greenhalgh
 
Skil storm testing at the speed of business 2
Skil storm testing at the speed of business 2Skil storm testing at the speed of business 2
Skil storm testing at the speed of business 2
 
Thousand Islands
Thousand IslandsThousand Islands
Thousand Islands
 
Effective Pair Programming
Effective Pair ProgrammingEffective Pair Programming
Effective Pair Programming
 
Octubre
OctubreOctubre
Octubre
 
Programme guide en
Programme guide enProgramme guide en
Programme guide en
 
501, LP2 demonstration discovery - serps breakdown, saxe
501, LP2 demonstration   discovery - serps breakdown, saxe501, LP2 demonstration   discovery - serps breakdown, saxe
501, LP2 demonstration discovery - serps breakdown, saxe
 
Learn Ruby 2011 - Session 1
Learn Ruby 2011 - Session 1Learn Ruby 2011 - Session 1
Learn Ruby 2011 - Session 1
 
Richard Sykula
Richard SykulaRichard Sykula
Richard Sykula
 
A New Paradigm In Linux Debug From Viosoft Corporation
A New Paradigm In Linux Debug From Viosoft CorporationA New Paradigm In Linux Debug From Viosoft Corporation
A New Paradigm In Linux Debug From Viosoft Corporation
 
Representing Uncertainty in Situation Maps for Disaster Management
Representing Uncertainty in Situation Maps for Disaster ManagementRepresenting Uncertainty in Situation Maps for Disaster Management
Representing Uncertainty in Situation Maps for Disaster Management
 
Prc Compassion La Intro
Prc Compassion La IntroPrc Compassion La Intro
Prc Compassion La Intro
 
Relationship Building
Relationship BuildingRelationship Building
Relationship Building
 
The Dancer voor een Gezonde Organisatie
The Dancer voor een Gezonde OrganisatieThe Dancer voor een Gezonde Organisatie
The Dancer voor een Gezonde Organisatie
 

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

Ruby — An introduction
Ruby — An introductionRuby — An introduction
Ruby — An introduction
Gonçalo Silva
 
Object-Oriented Programming with PHP (part 1)
Object-Oriented Programming with PHP (part 1)Object-Oriented Programming with PHP (part 1)
Object-Oriented Programming with PHP (part 1)
Bozhidar Boshnakov
 
Object Oriented Programming.pptx
Object Oriented Programming.pptxObject Oriented Programming.pptx
Object Oriented Programming.pptx
SAICHARANREDDYN
 
Ruby object model - Understanding of object play role for ruby
Ruby object model - Understanding of object play role for rubyRuby object model - Understanding of object play role for ruby
Ruby object model - Understanding of object play role for ruby
Tushar Pal
 
Introduction to Ruby Programming Language
Introduction to Ruby Programming LanguageIntroduction to Ruby Programming Language
Introduction to Ruby Programming Language
Nicolò Calcavecchia
 
About Python
About PythonAbout Python
About Python
Shao-Chuan Wang
 
A peek into Python's Metaclass and Bytecode from a Smalltalk User
A peek into Python's Metaclass and Bytecode from a Smalltalk UserA peek into Python's Metaclass and Bytecode from a Smalltalk User
A peek into Python's Metaclass and Bytecode from a Smalltalk User
Koan-Sin Tan
 
Metaprogramming Primer (Part 1)
Metaprogramming Primer (Part 1)Metaprogramming Primer (Part 1)
Metaprogramming Primer (Part 1)
Christopher Haupt
 
Java
JavaJava
Lecture 1 - Objects and classes
Lecture 1 - Objects and classesLecture 1 - Objects and classes
Lecture 1 - Objects and classes
Syed Afaq Shah MACS CP
 
From Ruby to Scala
From Ruby to ScalaFrom Ruby to Scala
From Ruby to Scala
tod esking
 
Is2215 lecture2 student(2)
Is2215 lecture2 student(2)Is2215 lecture2 student(2)
Is2215 lecture2 student(2)
dannygriff1
 
Model Manipulation Using Embedded DSLs in Scala
Model Manipulation Using Embedded DSLs in ScalaModel Manipulation Using Embedded DSLs in Scala
Model Manipulation Using Embedded DSLs in Scala
Filip Krikava
 
Advanced Reflection in Pharo
Advanced Reflection in PharoAdvanced Reflection in Pharo
Advanced Reflection in Pharo
Pharo
 
6 Object Oriented Programming
6 Object Oriented Programming6 Object Oriented Programming
6 Object Oriented Programming
Deepak Hagadur Bheemaraju
 
The Dark Art of Rails Plugins (2008)
The Dark Art of Rails Plugins (2008)The Dark Art of Rails Plugins (2008)
The Dark Art of Rails Plugins (2008)
lazyatom
 
Ruby objects
Ruby objectsRuby objects
Ruby objects
Reuven Lerner
 
ITFT-Classes and object in java
ITFT-Classes and object in javaITFT-Classes and object in java
ITFT-Classes and object in java
Atul Sehdev
 
Introduction to Swift 2
Introduction to Swift 2Introduction to Swift 2
Introduction to Swift 2
Joris Timmerman
 
Rapid Application Development using Ruby on Rails
Rapid Application Development using Ruby on RailsRapid Application Development using Ruby on Rails
Rapid Application Development using Ruby on Rails
Simobo
 

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

Ruby — An introduction
Ruby — An introductionRuby — An introduction
Ruby — An introduction
 
Object-Oriented Programming with PHP (part 1)
Object-Oriented Programming with PHP (part 1)Object-Oriented Programming with PHP (part 1)
Object-Oriented Programming with PHP (part 1)
 
Object Oriented Programming.pptx
Object Oriented Programming.pptxObject Oriented Programming.pptx
Object Oriented Programming.pptx
 
Ruby object model - Understanding of object play role for ruby
Ruby object model - Understanding of object play role for rubyRuby object model - Understanding of object play role for ruby
Ruby object model - Understanding of object play role for ruby
 
Introduction to Ruby Programming Language
Introduction to Ruby Programming LanguageIntroduction to Ruby Programming Language
Introduction to Ruby Programming Language
 
About Python
About PythonAbout Python
About Python
 
A peek into Python's Metaclass and Bytecode from a Smalltalk User
A peek into Python's Metaclass and Bytecode from a Smalltalk UserA peek into Python's Metaclass and Bytecode from a Smalltalk User
A peek into Python's Metaclass and Bytecode from a Smalltalk User
 
Metaprogramming Primer (Part 1)
Metaprogramming Primer (Part 1)Metaprogramming Primer (Part 1)
Metaprogramming Primer (Part 1)
 
Java
JavaJava
Java
 
Lecture 1 - Objects and classes
Lecture 1 - Objects and classesLecture 1 - Objects and classes
Lecture 1 - Objects and classes
 
From Ruby to Scala
From Ruby to ScalaFrom Ruby to Scala
From Ruby to Scala
 
Is2215 lecture2 student(2)
Is2215 lecture2 student(2)Is2215 lecture2 student(2)
Is2215 lecture2 student(2)
 
Model Manipulation Using Embedded DSLs in Scala
Model Manipulation Using Embedded DSLs in ScalaModel Manipulation Using Embedded DSLs in Scala
Model Manipulation Using Embedded DSLs in Scala
 
Advanced Reflection in Pharo
Advanced Reflection in PharoAdvanced Reflection in Pharo
Advanced Reflection in Pharo
 
6 Object Oriented Programming
6 Object Oriented Programming6 Object Oriented Programming
6 Object Oriented Programming
 
The Dark Art of Rails Plugins (2008)
The Dark Art of Rails Plugins (2008)The Dark Art of Rails Plugins (2008)
The Dark Art of Rails Plugins (2008)
 
Ruby objects
Ruby objectsRuby objects
Ruby objects
 
ITFT-Classes and object in java
ITFT-Classes and object in javaITFT-Classes and object in java
ITFT-Classes and object in java
 
Introduction to Swift 2
Introduction to Swift 2Introduction to Swift 2
Introduction to Swift 2
 
Rapid Application Development using Ruby on Rails
Rapid Application Development using Ruby on RailsRapid Application Development using Ruby on Rails
Rapid Application Development using Ruby on Rails
 

More from James Thompson

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

More from James Thompson (13)

Interfaces Not Required — RubyHACK 2018
Interfaces Not Required — RubyHACK 2018Interfaces Not Required — RubyHACK 2018
Interfaces Not Required — RubyHACK 2018
 
Bounded Contexts for Legacy Code
Bounded Contexts for Legacy CodeBounded Contexts for Legacy Code
Bounded Contexts for Legacy Code
 
Beyond Accidental Arcitecture
Beyond Accidental ArcitectureBeyond Accidental Arcitecture
Beyond Accidental Arcitecture
 
Wrapping an api with a ruby gem
Wrapping an api with a ruby gemWrapping an api with a ruby gem
Wrapping an api with a ruby gem
 
Microservices for the Monolith
Microservices for the MonolithMicroservices for the Monolith
Microservices for the Monolith
 
Mocking & Stubbing
Mocking & StubbingMocking & Stubbing
Mocking & Stubbing
 
Learn Ruby 2011 - Session 5 - Looking for a Rescue
Learn Ruby 2011 - Session 5 - Looking for a RescueLearn Ruby 2011 - Session 5 - Looking for a Rescue
Learn Ruby 2011 - Session 5 - Looking for a Rescue
 
Learn Ruby 2011 - Session 3
Learn Ruby 2011 - Session 3Learn Ruby 2011 - Session 3
Learn Ruby 2011 - Session 3
 
Learn Ruby 2011 - Session 2
Learn Ruby 2011 - Session 2Learn Ruby 2011 - Session 2
Learn Ruby 2011 - Session 2
 
Rails: Scaling Edition - Getting on Rails 3
Rails: Scaling Edition - Getting on Rails 3Rails: Scaling Edition - Getting on Rails 3
Rails: Scaling Edition - Getting on Rails 3
 
Ruby For Web Development
Ruby For Web DevelopmentRuby For Web Development
Ruby For Web Development
 
Ruby Testing: Cucumber and RSpec
Ruby Testing: Cucumber and RSpecRuby Testing: Cucumber and RSpec
Ruby Testing: Cucumber and RSpec
 
Introducing Ruby
Introducing RubyIntroducing Ruby
Introducing Ruby
 

Recently uploaded

Full-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalizationFull-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalization
Zilliz
 
UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5
DianaGray10
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
Uni Systems S.M.S.A.
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
名前 です男
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
Neo4j
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
Adtran
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
Octavian Nadolu
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
mikeeftimakis1
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
panagenda
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
Aftab Hussain
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
Matthew Sinclair
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
Quotidiano Piemontese
 
GraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracyGraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracy
Tomaz Bratanic
 
GenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizationsGenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizations
kumardaparthi1024
 
Mariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceXMariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceX
Mariano Tinti
 
How to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For FlutterHow to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For Flutter
Daiki Mogmet Ito
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
Kari Kakkonen
 
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
Neo4j
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
DianaGray10
 

Recently uploaded (20)

Full-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalizationFull-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalization
 
UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
 
GraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracyGraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracy
 
GenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizationsGenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizations
 
Mariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceXMariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceX
 
How to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For FlutterHow to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For Flutter
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
 
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
 

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

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n
  22. \n
  23. \n
  24. \n
  25. \n
  26. \n
  27. \n
  28. \n
  29. \n
  30. \n