Ruby II. Classes. Classes are straight forward in Ruby.

Similar documents
CSE 341, Autumn 2015, Ruby Introduction Summary

CSE 341: Programming Languages

SYMBOLS. you would not assign a value to a symbol: :name = "Fred" The Book of Ruby 2011 by Huw Collingbourne

CSE341: Programming Languages Lecture 19 Introduction to Ruby and OOP. Dan Grossman Winter 2013

Ruby logistics. CSE341: Programming Languages Lecture 19 Introduction to Ruby and OOP. Ruby: Not our focus. Ruby: Our focus. A note on the homework

Software Design and Analysis for Engineers

Overview of the Ruby Language. By Ron Haley

Programming Paradigms

Inheritance. Benefits of Java s Inheritance. 1. Reusability of code 2. Code Sharing 3. Consistency in using an interface. Classes

A A B U n i v e r s i t y

Strings and Variables

Inheritance Ch

Ruby: Object-Oriented Concepts

Smalltalk: developed at Xerox Palo Alto Research Center by the Learning Research Group in the 1970 s (Smalltalk-72, Smalltalk-76, Smalltalk-80)

C++ Important Questions with Answers

Basics Inheritance and Instance Variables Inheritance and Methods Class Variables. Ruby Inheritance CSCI September 2017.

CSc 372 Comparative Programming Languages

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Chapter 10 Defining Classes

Multiple Inheritance using modules in Ruby

Lecture 2. Object Orientation 1 / 51

Lecture 2. Object Orientation

Classes and Modules. Properties. Chapter 11

Lecture 2. Object Orientation 1 / 50

Last lecture. Lecture 9. in a nutshell. in a nutshell 2. Example of encapsulation. Example of encapsulation. Class test. Procedural Programming

Announcements. Last modified: Fri Sep 8 00:59: CS61B: Lecture #7 1

CS61B Lecture #7. Announcements:

Unit3: Java in the large. Prepared by: Dr. Abdallah Mohamed, AOU-KW

Introduction to Ruby. Part I

Encapsulation. Mason Vail Boise State University Computer Science

COMP-202 Unit 8: Defining Your Own Classes. CONTENTS: Class Definitions Attributes Methods and Constructors Access Modifiers and Encapsulation

EEE-425 Programming Languages (2013) 1

Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat Chapter 6 Inheritance Extending a Class

1. BlueJ bank example with subclasses of BankAccount 2. Transparency of UML diagram for BankAccount class hierarchy

Objects and Iterators

Introduction to Inheritance

Inheritance. Transitivity

CS112 Lecture: Defining Classes. 1. To describe the process of defining an instantiable class

QUIZ. How could we disable the automatic creation of copyconstructors

STUDENT LESSON A5 Designing and Using Classes

Inheritance in Ruby. You are familiar with the idea of inheritance and how to use this in programming.

EEE-425 Programming Languages (2013) 1

CSE 413 Spring Introduction to Ruby. Credit: Dan Grossman, CSE341

Data Structures (list, dictionary, tuples, sets, strings)

file:///users/dave/tmp/rubyadv/html/metaprogramming.html

QUIZ. How could we disable the automatic creation of copyconstructors

Object Oriented Design

About Ruby Comments Constants and variables Scopes Methods Ranges Arrays Operators Flow Control Iterators Math Library Date and Time Hashes Object

Unit Title: Objects in Visual Basic.NET. Software Development Unit 4. Objects in Visual Basic.NET

Simplest version of DayOfYear

PART A : MULTIPLE CHOICE Circle the letter of the best answer (1 mark each)

15.1 Origins and Uses of Ruby

Handout 9 OO Inheritance.

Lesson 10B Class Design. By John B. Owen All rights reserved 2011, revised 2014

CPS122 Lecture: Defining a Class

In fact, as your program grows, you might imagine it organized by class and superclass, creating a kind of giant tree structure. At the base is the

Data Abstraction. Hwansoo Han

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

CS 5142 Scripting Languages

Ruby. Mooly Sagiv. Most slides taken from Dan Grossman

Inheritance (Outsource: )

Ruby: Introduction, Basics

Starting Savitch Chapter 10. A class is a data type whose variables are objects. Some pre-defined classes in C++ include int,

What is it? CMSC 433 Programming Language Technologies and Paradigms Spring Approach 1. Disadvantage of Approach 1

CS162 Week 1. Kyle Dewey. Friday, January 10, 14

Ruby, with foxes. Ruby, cont d. Warning. why's (poignant) Guide to Ruby

Sri Vidya College of Engineering & Technology

JAVA: A Primer. By: Amrita Rajagopal

Principles of Ruby Applica3on Design. Dean Wampler Senior Mentor and Consultant Object Mentor, Inc. Chicago, IL

Ruby on Rails for PHP and Java Developers

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Chapter 12. OOP: Creating Object-Oriented Programs The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill

Lecture Notes on Programming Languages

CLASSES AND OBJECTS IN JAVA

Intermediate Cucumber. CSCI 5828: Foundations of Software Engineering Lecture 17 03/13/2012

Lecture 21: The Many Hats of Scala: OOP 10:00 AM, Mar 14, 2018

Computational Concepts Toolbox. Object Oriented Programming. Today: class. Review: Objects

Inheritance: Definition

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE. What is Inheritance?

Storing Data in Objects

Overview of OOP. Dr. Zhang COSC 1436 Summer, /18/2017

Programming overview

CSC Web Programming. Introduction to JavaScript

Chapter Goals. Chapter 9 Inheritance. Inheritance Hierarchies. Inheritance Hierarchies. Set of classes can form an inheritance hierarchy

Object-Oriented Programming

Programming Languages

6.096 Introduction to C++

Project 5 Due 11:59:59pm Wed, Nov 25, 2015 (no late submissions)

Arrays Classes & Methods, Inheritance

Inheritance. CSc 372. Comparative Programming Languages. 28 : Ruby Classes. Department of Computer Science University of Arizona.

What is Inheritance?

1 State, objects, and abstraction

CSc 372 Comparative Programming Languages

CS313D: ADVANCED PROGRAMMING LANGUAGE

Java Puzzle Ball Nick Ristuccia

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

comparing groovy & jruby *

Name Return type Argument list. Then the new method is said to override the old one. So, what is the objective of subclass?

Symbols. accessor properties, attributes, creating, adding properties, 8 anonymous functions, 20, 80

Cpt S 122 Data Structures. Introduction to C++ Part II

Transcription:

Ruby II Classes Classes are straight forward in Ruby. expr Note that the class name begins with a capitol letter. Classes can contain instance variables, instance methods, class variables, class methods, and special accessor functions. Classes Instance Attributes/Methods Instance attributes and methods are variables and methods that belong to the object. Instance methods are simply methods defined inside of a class structure. Instance attributes are variables whose name begins with an @ that are used in an instance method. It is not possible to declare instance attributes outside of the instance methods. If an instance attribute is accessed before it is set, the value is nil. 1

Classes Instance Attributes/Methods def initialize() @balance = 0 def depositfunds(amount) @balance += amount myaccount = BankAccount.new() myaccount.depositfunds(100) In this example, the class is BankAccount, depositfunds is an instance method of BankAccount, and @balance is an instance attribute of BankAccount. We call the method after we create an object of type BankAccount. Classes Initialize Method You must create an object named initialize for each class you create. When you create a new object, you call the new method for the class. myaccount = BankAccount.new(500); The new method will create the object and then call the user defined initialize method in the class. Any parameters passed to new will be passed to the initialize method. Because you do not declare instance attributes ahead of time in Ruby, the initialize method is a good place to set up all instance attributes. Classes Initialize Cont. def initialize(startingbalance, accounttype= Checking, accountnumber=nil) @balance = startingbalance @accounttype = accounttype @accountnumber = accountnumber Initialize can only be declared once per class. If you wish to have optional parameters, you should set the default values in the parameter list. 2

Classes - Self An instance method is able to call another instance method. If you do not specify an object to be the receiver, it is assumed that the current object is the receiver. def transfermoney(receivingaccount, amount) withdrawfunds(amount) receivingaccount.depositfunds(amount) In this case, we did not specify a receiver for withdrawfunds, so the current object was passed. To make the code more clear, we could have used the self keyword. self simply refers to the current object. self.withdrawfunds(amount) Classes Access Control Instance variables are always private. You must use a method to allow get/set privileges from outside of the class. All instance methods besides initialize are public by default. initialize is always private. Though in some ways not really since new invokes it implicitly It is possible to change the access level of instance methods by specifying public, protected, or private. public: can be called by anyone private: can only be called by the current object. This means that you can not specify a receiver. protected: can by called in the class/subclass. Differs from private because you can specify a reciever. Classes Access Control cont. There are two ways to specify accessibility. #1) Write the accessibility level followed by a newline. Any subsequent methods will have that level. In the following definition, withdrawfunds is protected and the other methods are public. protected def withdrawfunds(amount) @balance -= amount public def depositfunds(amount) @balance += amount def transfermoney(receivingaccount, amount) receivingaccount.depositfunds(amount) withdrawfunds(amount) 3

Classes Access Control cont. #2) Declare all methods and then call the access levels with parameters passed to them. The parameter should be : + methodname. Any method name specified will have that access level. def withdrawfunds(amount) @balance -= amount def depositfunds(amount) @balance += amount def transfermoney(receivingaccount, amount) receivingaccount.depositfunds(amount) withdrawfunds(amount) protected :withdrawfunds, :depositfunds public :transfermoney Classes Attribute Accessors As mentioned before, instance attributes are always private. You can not specify public or protected for them. You must create get/set methods for the attributes if you want them to be accessed outside of the class. def balance return @balance def setbalance(amount) @balance = amount This could get tedious, so Ruby has made it much simpler to create these get/set methods with accessors. Classes Attribute Accessors Ruby offers three accessor methods to do the basic get/set for you. attr_reader: Implements the get behavior. attr_writer: Implements the set behavior. attr_accessor: Implements both get & set. All of these are used by specifying the proper method and then passing it a colon and the instance attribute s name. You can specify multiple attributes by separating the names with a comma. attr_reader :balance attr_writer :balance, :accounttype attr_accessor :balance Once you set these accessor methods, you can access the attribute just as you would if it were public. myaccount.balance = 100 puts myaccount.balance 4

Classes Virtual Attributes It is possible to allow the user to assign to an attribute that does not exist. myaccount.interestratepercent = 10 Then in the class, we can handle the incoming value and set interestrate appropriately. Note the equal sign at the of the method name. def interestratepercent= (percentage) @interestrate = (percentage.to_f/100.to_f).to_f Classes Class Variable A class variable is shared among all the instances of a class. It is useful if you want to keep a balance/count of all instances. A class variable MUST be initialized in the class outside of any methods. A class variable is specified by using @@name. The class variable is accessible in the instance methods and the class methods. @@allaccountbalance = 0 def withdrawfunds(amount) @balance -= amount @@allacountbalance -= amount Classes Class Methods A class method is a method for the class instead of the instance. A class method has no access to the instance attributes except through an object of the class. The class method is defined inside of the class. It apps the class name to it s definition def BankAccount.transferFunds(fromAccount, toaccount, amount) fromaccount.withdrawfunds(amount) toaccount.depositfunds(amount) Note that withdrawfunds and depositfunds must be public in order for this to work. 5

Classes Singleton Classes We can use class methods to create a singleton class in Ruby. In this example, we will limit the creation of Myself to a single object. We first disable the new method. We can do this by setting it to private. This way, we can control when new gets called and therefore how many objects are created of our class. It is possible to set an existing item to private by using the private_class_method method. We pass the method a colon followed by the name of the method we want to set to be private (in this case, new ) class Myself private_class_method :new Classes Singleton Classes cont. Next we create a new constructor (make) for the user to call. Our new constructor will decide to call new or not. We use a class method for our constructor and a class variable to store the actual object. If the object is not created yet, we can create it by calling new. Otherwise, we can just return the already existing object. class Myself private_class_method :new @@me = nil def Myself.make @@me = new unless @@me @@me thomas = Myself.make() print thomas.inspect() thomasclone = Myself.make() print thomasclone.inspect() Classes Inspect & to_s Two useful methods for testing are inspect and to_s. inspect lists the object s instance variables. myaccount.inspect() #<BankAccount:0x27b72f8 @accounttype="checking", @balance=500, @accountnumber=4654> to_s converts the object to a string. myaccount.to_s() #<BankAccount:0x27b72f8> We can overwrite to_s to present a more readable string. def to_s "Balance for #{@accounttype} Account ##{@accountnumber}: $#{@balance}" Balance for Checking Account #4654: $500 6

Classes - Inheritance When an object oriented language is implementing inheritance, it must decide if it will allow single inheritance, multiple inheritance, or some sort of hybrid. In life, multiple inheritance makes sense. Single inheritance limits object oriented programming. However, multiple inheritance can get very messy. There can be ambiguous attributes and it is difficult to read and maintain. Ruby implements single inheritance. An object can only have one parent. However, it creates a hybrid by allowing mixins to include additional functionality We will study mixins when we look at modules. Classes - Inheritance When declaring a class with a parent, we simply add < parentname to the of the class declaration class CheckingAccount < BankAccount expr A child class will inherit all of the parent s attributes and methods no matter what the access level. It is possible for a child class to overwrite any of the parent s methods. class CheckingAccount < BankAccount attr_accessor :pin def to_s PIN for Account ##{@accountnumber}: #{@pin}" Classes - Inheritance Notice that we did not create a new initialize method for CheckingAccount. When we call CheckingAccount.new(), Ruby will first see if CheckingAccount has an initialize function. When it does not find one, it will check the parent and see if the parent has one. This is how it behaves with all methods. Note there is obviously a top Object class just like in JavaScript that is the ultimate super class for everything If wanted to add additional fields to our initialization, we could create our own initialize. class CheckingAccount < BankAccount def initialize(startingbalance, pin, accountnumber=nil) @balance = startingbalance @pin = pin @accounttype = Checking @accountnumber = accountnumber 7

Classes - Super Instead of repeating the initialize code we use in BankAccount, we should just pass the appropriate parameters to the BankAccount initialize. We use the keyword super to access the parent methods. class CheckingAccount < BankAccount def initialize(startingbalance, pin, accountnumber=nil) super(startingbalance, Checking, accountnumber) @pin = pin Modules A module is a collection of classes, methods, and constants. Since modules can be stored in separate files with their own namespace and included (and mixed in) when needed, they are ideal for reusability. module Bank expr This module creates a namespace named Bank with our BankAccount class in it. Modules cont. There are various ways to access the objects in the module. You put constants and classes in the module the same way you would put it in a normal file. module Logs LOGPATH = /logs class LogFile In order to reference a constant or a class inside of a module, you must use the scope resolution operator (::) path = Logs::LOGPATH newfile = Logs::LogFile.new() 8

Modules cont. Methods in modules are private by default. In order to make them public, you must name the method like you would a class method: ModuleName.methodname module Logs def Logs.addLog(logmessage) expr Then you can call the method simply by calling the same name Logs.addLog( Success ) Modules - Mixins As mentioned in the classes section, we use modules to get around the need for multiple inheritance. As we saw in the last section, we had to declare methods with the module name in front of it in order to access it outside of the module. If we declare methods without the module name module Logs def addlog(logmessage) expr Logs.addLog( Success ) We get the following error: undefined method `addlog' for Logs:Module Modules - Mixins In this case, addlog is being implemented as an instance method and because a module is not a class, it can not use an instance method. However, we can mix-in the module with a class and then the class will have access to the instance method. include Logs Now, the BankAccount class can use addlog as if it were an instance method in BankAccount. myaccount = BankAccount.new myaccount.addlog( success ) If multiple mixins contain the same method name, the last one to be included is the method that will be used. 9

Modules Mixins cont. Mixin methods can call other methods in the class. Of course, the class must ensure that it defines any methods that the included mixin calls. module Logs def addlog(logmessage) puts self.getaccount + : + logmessage include Logs def getaccount return #{@accounttype} Account ##{@accountnumber} myaccount = BankAccount.new myaccount.addlog( success ) Modules Mixins cont. Mixins can access/set instance attributes in the same manner as methods. module Logs attr_reader :logsadded def addlog(logmessage) @logsadded = true include Logs myaccount = BankAccount.new myaccount.addlog( success ) puts myaccount.logsadded Note: The same possible conflict does exist if two included mixins contain the same attribute name. Hmmm you kind of wonder if this really doesn t have the same types of problems of multiple inheritance 10