Ruby Classes.

Slides:



Advertisements
Similar presentations
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Introduction to Ruby.
Advertisements

Objects and Classes David Walker CS 320. Advanced Languages advanced programming features –ML data types, exceptions, modules, objects, concurrency,...
Types in Ruby and other languages….  Classes and objects (vs prototypes)  Instance variables/encapsulation  Object creation  Object equality/comparison.
C++ Classes & Data Abstraction
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Written by: Dr. JJ Shepherd
And other languages….  Get out a piece of paper  You’ll be tracing some code (quick exercises) as we go along.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Objects and Classes David Walker CS 320. Advanced Languages advanced programming features –ML data types, exceptions, modules, objects, concurrency,...
1 Objects and Classes Introduction to Classes Object Variables and Object References Instantiating Objects Using Methods in Objects Reading for this Lecture:
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
Like our natural language. Designed to facilitate the expression and communication ideas between people and computer Like our natural language. Designed.
Ruby (on Rails) CSE 190M, Spring 2009 Week 4. Constructors Writing a new class is simple! Example: class Point end But we may want to initialize state.
Ruby (on Rails) Slides modified by ements-2ed.shtml) 1.
Objects & Dynamic Dispatch CSE 413 Autumn Plan We’ve learned a great deal about functional and object-oriented programming Now,  Look at semantics.
+ Ruby and other programming Languages Ronald L. Ramos.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Ruby Objects, Classes and Variables CS 480/680 – Comparative Languages.
Written by: Dr. JJ Shepherd
Ruby Duck Typing, Classes & Inheritance CSE 413 Autumn 2008.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Ruby Inheritance and other languages….
Topic: Classes and Objects
Programming with ANSI C ++
CSE341: Programming Languages Lecture 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods James Wilcox Winter 2017.
Inheritance and Polymorphism
Java Primer 1: Types, Classes and Operators
PHP Classes and Objects
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Week 4 Object-Oriented Programming (1): Inheritance
An Introduction to Inheritance
Chapter 3 Inheritance © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Types of Programming Languages
Programming Language Concepts (CIS 635)
Identifying the Parts of an Expression
Operator Overloading BCA Sem III K.I.R.A.S.
CMPE212 – Stuff… Exercises 4, 5 and 6 are all fair game now.
Python Classes By Craig Pennell.
CSE341: Programming Languages Lecture 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods Zach Tatlock Winter 2018.
Chapter 10: Visibility Chapter 18 in Applying UML and Patterns Book.
Chapter 20 Applying UML and Patterns Craig Larman
MSIS 670 Object-Oriented Software Engineering
Lecture 22 Inheritance Richard Gesick.
CSE341: Programming Languages Lecture 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods Dan Grossman Autumn 2018.
L1-6 Notes: Algebra: Variables and Expressions
Advanced Programming Behnam Hatami Fall 2017.
CSE341: Programming Languages Lecture 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods Dan Grossman Spring 2016.
Passing Parameters by value
Advanced Java Programming
More Object-Oriented Programming
Object Oriented Programming
Testing with OO OO has several key concepts:
CSE341: Programming Languages Lecture 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods Dan Grossman Spring 2017.
Java – Inheritance.
Tonga Institute of Higher Education
Tonga Institute of Higher Education
Object Oriented Programming in java
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Java Programming Language
In this class, we will cover:
Review of Previous Lesson
CSE341: Programming Languages Lecture 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods Dan Grossman Autumn 2017.
Object Oriented Programming
ENERGY 211 / CME 211 Lecture 22 November 10, 2008.
Jim Fawcett CSE687 – Object Oriented Design Spring 2014
C++ Object Oriented 1.
Creating and Using Classes
CSE341: Programming Languages Lecture 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods Dan Grossman Spring 2019.
Brett Wortzman Summer 2019 Slides originally created by Dan Grossman
Presentation transcript:

Ruby Classes

Classes Classes in Ruby are similar to classes in other familiar languages with some interesting differences We will see how to write classes and different related features

Remember convention @ - instance variable @@ - class variable No symbol in front of it – local variable We will discuss this more soon

Initializing Initialize is a special method always private

Access Control private, public, protected like other languages Except private is really private only accessible on self, not even on other objects of same class You can’t call private explicitly using a receiver, it can only be called on an implicitly receiver There are some tricky ways to get around this… Default is public You can specify access control one of two ways

Especially useful when you Access Control… Especially useful when you want to make some attributes protected

Attributes – readers :name returns a Symbol object : represents name of a variable while variable name represents the value of the variable Creates year method and @year instance variable

Attributes - Writer OR

What’s wrong with this code? We’re missing an @ Coming from other languages we tend to do this for a while

A little gotcha The return value from assignment method is ignored

Inheritance You use < to express inheritance Super call’s respective base method

Inheritance… Ruby supports Single Inheritance like Java/C# But how about multiple inheritance No and Yes Not supported, but There is a very powerful Mixin – more about this later

Classes never closed You can add methods to any class you like

method_missing You can dynamically figure out what to do when methods are called Ruby calls method_missing when it can’t hande a method After adding method_missing