Object-Oriented Concepts
Computer Science and Engineering College of Engineering The Ohio State University
Classes have methods and variables
Copyright By PowCoder代写 加微信 powcoder
class LightBulb # name with CamelCase def initialize # special method name
@state = false # @ means “instance variable” end
def flip_switch!
# implicit return
# name with snake_case
Instantiation calls initialize method
f = LightBulb.new #=>
f.on? #=> false
Computer Science and Engineering The Ohio State University
Visibility
Computer Science and Engineering The Ohio State University
Instance variables are never visible outside of the object
Default visibility for methods is public
Can be made private (to object, not class)
class LightBulb
private def inside
def access_internals(other_bulb)
inside # ok
other_bulb.inside # no! inside is private self.inside # no explicit recv’r allowed
Getters/Setters
class LightBulb
def initialize(color, state = false)
@color = color # not visible outside object
@state = state # not visible outside object end
@state end
def state=(value)
@state = value
Computer Science and Engineering The Ohio State University
Attributes
class LightBulb
def initialize(color, state = false)
@color = color
@state = state
attr_accessor :state # name is a symbol
Computer Science and Engineering The Ohio State University
Attributes
class LightBulb
def initialize(color, state = false)
@color = color
@state = state
attr_reader :color
attr_accessor :state
Computer Science and Engineering The Ohio State University
Attributes
class LightBulb
attr_reader :color
attr_accessor :state
attr_writer :size
Computer Science and Engineering The Ohio State University
def initialize(color, state = false)
@color = color
@state = state
Classes Are Always Open
Computer Science and Engineering The Ohio State University
A class can always be extended
class Street
def construction … end
class Street
def repave … end # Street now has 2 methods end
Applies to core classes too
class Integer
def log2_of_cube # lg(self^3)
(self**3).to_s(2).length – 1
500.log2_of_cube #=> 26
Classes are Always Open (!)
Computer Science and Engineering The Ohio State University
Existing methods can be redefined!
When done with system code (libraries, core …) called “monkey patching”
Tempting, but… Just Don’t Do It
No Overloading
Method identified by (symbol) name No distinction based on number of arguments
Approximation: default arguments
def initialize(width, height = 10)
@width = width
@height = height
Better alternative: trailing options hash
def initialize(width, options)
Modern alternative: default keyword args
def initialize(height: 10, width:)
Computer Science and Engineering The Ohio State University
A Class is an Object Instance too
Computer Science and Engineering The Ohio State University
Even classes are objects, created by :new
LightBulb = Class.new do #class LightBulb def initialize
@state = false
def flip_switch!
Instance, Class, Class Instance
Computer Science and Engineering The Ohio State University
class LightBulb
@state1 # class instance var def initialize
@state2 = … # instance variable
= … # class variable end
def bar …
def self.foo
# instance method
# sees @state2,
# class method
# sees @state1,
Inheritance
Single inheritance between classes
class LightBulb < Device
Default superclass is Object (which inherits
from BasicObject)
Super calls parent's method
No args means forward all args
class LightBulb < Device
def electrify(current, voltage)
super # with current and voltage end
Computer Science and Engineering The Ohio State University
Another container for definitions
module Stockable
MAX = 1000
class Item ... end
def self.inventory ... end
def order ... end
Cannot, themselves, be instantiated
Stockable.inventory # ok
s = Stockable.new # NoMethodError Stockable.order # NoMethodError
Computer Science and Engineering The Ohio State University
Modules as Namespaces
Computer Science and Engineering The Ohio State University
Modules create independent namespaces cf. packages in Java
Access contents via scoping (::)
Math::PI #=> 3.141592653589793 Math::cos 0 #=> 1.0
widget = Stockable::Item.new
x = Stockable::inventory
Post < ActiveRecord::Base
BookController < ActionController::Base
Style: use dot to invoke utility functions (ie module methods)
Math.cos 0 #=> 1.0 Stockable.inventory
Modules are Always Open
Computer Science and Engineering The Ohio State University
Module contains several related classes
Module definition can be split into several files: Each file contains 1 class definition
module Game
# game/card.rb
module Game
class Card … end
# game/player.rb
module Game
class Player … end
Require used to find these files require ‘./game/card.rb’
Modules as “Mixins”
Computer Science and Engineering The Ohio State University
Another container for definitions
module Stockable
def order … end
A module can be included in a class class LightBulb < Device
include Stockable, Comparable ...
Module's (instance) methods/vars are
now (instance) methods/vars for class
bulb = LightBulb.new
bulb.order # from Stockable if bulb <= old_bulb # from Comparable
Requirements for Mixins
Computer Science and Engineering The Ohio State University
Mixins often rely on certain aspects of
classes into which they are included
Example: Comparable methods call <=>
module Comparable
def <(other) ... end
def <=(other) ... end
Enumerable methods call #each
Recall layering in SW I/II
Class implements kernel methods Module implements secondary methods
Software Engineering
Computer Science and Engineering The Ohio State University
All the good principles of SW I/II apply Single point of control over change
Avoid magic numbers
Client view: abstract state, contracts,
invariants
Implementers view: concrete rep,
correspondence, invariants
Checkstyle tool: e.g., rubocop
Documentation (YARD or RDoc)
Notation for types: yardoc.org/types.html @param [String, #read] # either is ok
Computer Science and Engineering The Ohio State University
Classes as blueprints for objects
Contain methods and variables
Public vs private visibility of methods Attributes for automatic getters/setters
Metaprogramming
Classes are objects too "Class instance" variables
Single inheritance
Modules are namespaces and mixins
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com