CSC302 2006S, Class 38: Presentations (2): Objects in Ruby Admin: * Presentation by Michael Claveria, Bradley Miller, David Ventresca * Reading for Friday? * SamR takes notes in EBoard again. * Really Late: Alex * Moral: "Ruby is Awesome" Overview: * Classes, How to Use Them * Inheritance * Modules and Mixins * Polymorphism, Overloading, and Other Failures * Mini Lab /Class Basics/ * Start with "ruby eval.rb", which starts the Ruby shell * Not quite the Ruby shell described in /Beyond Java/, but close enough * Define classes with the "class" keyword and the name of the class. * Class names start with capital letters. * Constructors are called "initialize" * Fields begin with an at sign * Class variables begin with two at signs * Example ruby> class Book | @@numberOfBooks = 0 | def initialize(title, author, contents) | @title = title | @author = author | @contents = contents | @@numberOfBooks = @@numberOfBooks + 1 | end | end nil ruby> abook = Book.new("Beyond Java", "O'Reilly", "words") * You load a file with "require" * You can only load once. /Inheritance/ * Use "<" for subclassing * The constructor can call the constructor for the superclass with "super" * Example require "book.rb" class TextBook < Book def initialize(title, author, contents, course) super(title, author, contents) @course = course end end [Good, simple, example] [I like to see polymorphism here, but later is reasonable.] * Can override methods * Method call: Check class, then parent, then grandparent, and so on and so forth * Ruby shell allows live redefinition of classes /Modules and Mixins/ * Modules permit you to group related things together. * Use keyword "module" * Module names start with capital letters" (it seems) module Characteristics def Characteristics.sex chromosomes if chromosomes == 'XY' puts 'male' elsif chromosomes == 'XX' puts 'female' else puts 'ambiguous' end end end module Drunk_Student def Drunk_Student.sex beers if beers >= 5 puts "Too drunk to make a rational decision. No sex." else puts "Went to Harris and hooked up." end end end Although both have a sex method, the methods are independent (and, in fact, have a different name.) [Difference between modules and classes? Modules group static methods] /Mixins/ Classes can "include" other classes and, by default, get the methods of those classes module Debug def identify "#{self.class} (\##{self.object_id})" end end class Pikachu include Debug end class Jigglypuff include Debug end [Good question from Davis: Why do this instead of subclassing? Answer: Gets around problems of multiple inheritance.] [Revised question from Davis: If they inherit a method and include a method, which wins?] [Not a lot of discussion] /Polymorphism/ class PowerDrill def drill # drill like a power drill end end class HandDrill def drill # drill like a hand drill end end def goDrill(tool) tool.drill end We can call goDrill(PowerDrill.new()) We can also call goDrill(HandDrill.new()) No overloading permitted (no two methods with the same name) /Exercise/ Lab: Shuffle a deck of cards in Ruby [Sam calls this "permuting" because shuffling works differently] Part 0: Overview. A standard deck of cards has 52! possible permutations. In order to shuffle a deck of cards it is necessary to supply an algorithm that presents an equal chance for any of the permutations to occur. Part 1: Precoding. Discuss possible solutions to the problem. There is a linear time solution! Part 2: Deck. Create an array of length 52, you can use any naming convention for the cards, or it can be simply the numbers from 1 to 52, or 0 to 51. Part 3: Shuffle. Write a function that provides the shell of your algorithm. This 'shell' should simply step through the array and then call a function to perform the required swapping your algorithm requires. Write a function swap which performs the linear time algorithm given a position in the array. Part 4: Test. Execute the deck.Shuffle function 10-15 times, or until you are satisfied with your results and print the results of 5 consecutive shuffles. [Solution given is not guaranteed to be uniform, but is acceptable.]