5 Ruby Ducks

download 5 Ruby Ducks

of 16

Transcript of 5 Ruby Ducks

  • 8/10/2019 5 Ruby Ducks

    1/16

    CSE 413 Autumn 2008

    Ruby Duck Typing,Classes & Inheritance

  • 8/10/2019 5 Ruby Ducks

    2/16

    verv ew

    Next big topic is typing, classes, andinheritance

    But first, a couple of useful things

    An example of an each iterator

    .

  • 8/10/2019 5 Ruby Ducks

    3/16

    e ers e ers

    Recall that allinstance variables are

    class PosRatdef initialize(num, denom=1)@num = num

    really private need

    to define methods to

    enom = enomend

    @num

    enddef num=(value)

    @num = valueend

  • 8/10/2019 5 Ruby Ducks

    4/16

    n erna ve

    Was:def num

    Instead, can use

    @num

    end

    attr_reader :num, :denom

    def denom

    @denom There is a similar

    _

  • 8/10/2019 5 Ruby Ducks

    5/16

    era or xamp e

    Suppose we want to define a class ofSe uence ob ects that have a from toand step, and contain numbers x such that

    from

  • 8/10/2019 5 Ruby Ducks

    6/16

    equence ass ons ruc or

    class Sequence# mixin all of the methods in Enumerable

    include Enumerable

    def initialize(from, to, step)

    @from, @to, @step = from, to, step

    en

  • 8/10/2019 5 Ruby Ducks

    7/16

    equence eac me o

    To add an iterator to Sequence and make it alsowork with Enumerable, all we need is this:e eac

    x = @from

    while x

  • 8/10/2019 5 Ruby Ducks

    8/16

    oc s rocs ev s e

    Blocks are only usable in the immediatecontext of their caller. _ _

    Procs are real first-class objects

    Proc instances all have a call methodCan be stored in fields, passed as arguments,

    .This is exactly a closure

  • 8/10/2019 5 Ruby Ducks

    9/16

    ypes n u y

    Ruby is dynamically typed everything isan ob ect

    Only notion of an objects type is what

    i.e., whether it has methods for a particular

    This can change dynamically for either allob ects of a class or for individual ob ects

  • 8/10/2019 5 Ruby Ducks

    10/16

    uc yp ng

    If it walks like a duck and talks like aduck it must be a duck

    Even if it isnt

    (i.e, what messages it understands)

  • 8/10/2019 5 Ruby Ducks

    11/16

    oug xper men

    What must be true about x for this methodto work?

    x.m + x.n

  • 8/10/2019 5 Ruby Ducks

    12/16

    oug xper men

    What is true about x?x.m + x.n

    Less than you might thinkx must have 0-ar ument methods m and nThe object returned by x.m must have a +

    method that takes one argument

    The object returned by x.n must havewhatever methods are needed by x.m.+ (!)

  • 8/10/2019 5 Ruby Ducks

    13/16

    uc yp ng ra eo s

    PlusConvenient, romotes code reuse

    All that matters is what messages an object can

    receive Minus

    Obvious equivalences dont hold: x+x, 2*x, x*2

    May expose more about an object than might bedesirable (more coupling in code)

  • 8/10/2019 5 Ruby Ducks

    14/16

    asses n er ance

    Ruby vs Java:Subclassing in Ruby is not about type checking

    ecause o ynam c yp ng

    Subclassing in Ruby is about inheriting methods

    See examples in points.rb

    ColorPoint inherits distance methods

  • 8/10/2019 5 Ruby Ducks

    15/16

    verr ng

    With dynamic typing, inheritance alone isust avoidin cut/ aste

    Overriding is the key difference

    call, it resolves to a method defined in thesubclass if there is one

    Example: distFromOrigin2 in PolarPoint

  • 8/10/2019 5 Ruby Ducks

    16/16

    u y gress on

    Since we can add/change methods on the fly,why use a subclass? ns ea o c ass o or o n , w y no us a

    a color field to Point?Cant do this in JavaCan do it in Ruby, but it changes all Point

    instances (including subclasses), even existing

    Pro: now all Point classes have a colorCon: Maybe that breaks something else