Learning Ruby Classes. Variables Variables in Ruby can contain data of any type. You can use variables in your Ruby programs without any declarations.

Slides:



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

C Language.
Python Objects and Classes
Object-Oriented Programming
Object-Oriented Programming Python. OO Paradigm - Review Three Characteristics of OO Languages –Inheritance It isn’t necessary to build every class from.
Programming Languages and Paradigms
Chapter 3 DATA: TYPES, CLASSES, AND OBJECTS. Chapter 3 Data Abstraction Abstract data types allow you to work with data without concern for how the data.
Coding Standard: General Rules 1.Always be consistent with existing code. 2.Adopt naming conventions consistent with selected framework. 3.Use the same.
Let's Learn Ruby AQAP As Quickly As Possible!.  Run the interactive ruby shell – irb  Suggest using Netbeans – download ALL Running Ruby Code 2.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Language of the Month If it’s December, it must be Ruby! Adam Coffman and Brent Beer.
Learning Ruby Methods. We’ve seen several so far: puts, gets, chomp, to_s, and even +, -, * and / Usually use dot notation is really just a shortcut.
Learning Ruby - 9 Classes. class Person def initialize( name, dob, nationality = = = nationality end # of initialize.
Object-Oriented PHP (1)
Learning Ruby - 12 Grabbag II - The Grabbag Strikes Back!
Data Types.
C++ fundamentals.
Introduction to Methods
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
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.
The Ruby Programming Language
Intermediate PHP (2) File Input/Output & User Defined Functions.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Writing Classes (Chapter 4)
Python: Classes By Matt Wufsus. Scopes and Namespaces A namespace is a mapping from names to objects. ◦Examples: the set of built-in names, such as the.
CCSA 221 Programming in C CHAPTER 14 MORE ON DATA TYPES 1 ALHANOUF ALAMR.
Tutorial 2 Variables and Objects. Working with Variables and Objects Variables (or identifiers) –Values stored in computer memory locations –Value can.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Ruby and the tools 740Tools05ClassesObjectsVars Topics Ruby Classes Objects Variables Containers Blocks Iterators Spring 2014 CSCE 740 Software Engineering.
The Java Programming Language
JAVA Tokens. Introduction A token is an individual element in a program. More than one token can appear in a single line separated by white spaces.
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
What is PHP? PHP stands for PHP: Hypertext Preprocessor PHP is a server-side scripting language, like ASP PHP scripts are executed on the server PHP supports.
10-Nov-15 Java Object Oriented Programming What is it?
Java The Java programming language was created by Sun Microsystems, Inc. It was introduced in 1995 and it's popularity has grown quickly since A programming.
Introduction to Computer Programming
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
+ Ruby and other programming Languages Ronald L. Ramos.
Chapter 10, Slide 1 ABSTRACT DATA TYPES Based on the fundamental concept of ABSTRACTION:  process abstraction  data abstraction Both provide:  information.
ENUMERATED DATATYPES. USER DEFINED DATA TYPES  Data Type Defined By Programmer  Allows Use Of More Complex Data  Typically Defined Globally So Variables.
Ruby Objects, Classes and Variables CS 480/680 – Comparative Languages.
Scripting Languages Diana Trandab ă ț Master in Computational Linguistics - 1 st year
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
Guide to Programming with Python Chapter Eight (Part I) Object Oriented Programming; Classes, constructors, attributes, and methods.
CSC 143A 1 CSC 143 Introduction to C++ [Appendix A]
Chapter2 Constants, Variables, and Data Types. 2.1 Introduction In this chapter, we will discuss –constants (integer, real, character, string, enum),symbolic.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
 History  Ease of use  Portability  Standard  Security & Privacy  User support  Application &Popularity Today  Ten Most Popular Programming Languages.
Structure A collection of values (members) struct date{ int day; char month[10]; int year; }; Declare a structure variable struct date today; struct struct_name.
Introduction to information systems RUBY dr inż. Tomasz Pieciukiewicz.
Chapter 2 - OOP Maciej Mensfeld Presented by: Maciej Mensfeld More about OOP dev.mensfeld.pl github.com/mensfeld.
OOP Basics Classes & Methods (c) IDMS/SQL News
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
structured statically typed imperative wide-spectrum object-oriented high-level computer programming language extended from Pascal and other languages.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 8: Namespaces, the class string, and User-Defined Simple Data Types.
Introduction to Programming
COLORS.
Ruby: An Introduction Created by Yukihiro Matsumoto in 1993 (named after his birthstone) Pure OO language (even the number 1 is an instance of a class)
Introduction to Python
Variables All variables hold object references
Yanal Alahmad Java Workshop Yanal Alahmad
CS-104 Final Exam Review Victor Norman.
CIT 383: Administrative Scripting
Data Types Chapter 8.
Functions BIS1523 – Lecture 17.
Teach A-Level Computer Science: Object-Oriented Programming in Python
Ruby Classes.
What Color is it?.
Classes and Objects Imran Rashid CTO at ManiWeber Technologies.
Presentation transcript:

Learning Ruby Classes

Variables Variables in Ruby can contain data of any type. You can use variables in your Ruby programs without any declarations. Variable name itself denotes its scope (local, global, instance, etc.). A local variable (declared within an object) name consists of a lowercase letter (or an underscore) followed by name characters An instance variable (declared within an object always "belongs to" whatever object self refers to) name starts with an ''at'' sign followed by A class variable (declared within a class) name starts with two ''at'' signs followed by a name A class variable is shared among all objects of a class. Only one copy of a particular class variable exists for a given class. Global variables start with a dollar sign (''$'') followed by name characters. A global variable name can be formed using '‘$'' followed by any single character ($counter, $COUNTER, $-x). Ruby defines a number of global variables that include other punctuation characters, such as $_ and $-K

class Person def initialize( name, dob, nationality = = = nationality end # of initialize. end # of Person. fred = Person.new( 'Fred Jones', '24/05/1950', :Irish ) puts fred puts fred.methods.sort puts fred.instance_variables Creating Classes indicates class variables

fred.to_s class Person def to_s born on end # of to_s. end # of Person. fred.to_s Classes to Strings - Dynamically! We have added to_s AFTER already finishing the definition for class

Open Classes Methods can be added to classes at any point … even built in classes class Integer def even? (self % 2) == 0 end p (1..10).select { |n| n.even? }  [2, 4, 6, 8, 10]

inspect: Returns a string containing a human- readable representation of obj. If not overridden, uses the to_s method to generate the string (nice as prints array as [2, 4, 6, 8, 10] ) For each object, p directly writes anObject.inspect followed by the current output record separator to the program's standard output.

class Person def end # of name. def end # of dob. def end # of nationality. end # of Person. fred.name fred.to_s fred.dob Class Get Methods

class Person def set_name( name = name end # of set_name. def set_dob( dob = dob end # of set_dob. def set_nationality( nationality = nationality end # of set_nationality. end # of Person. fred.name fred.set_name( "Fred Reginald Jones" ) fred.to_s Class Set Methods

class Person attr_reader :name, :dob, :nationality attr_writer :name, :dob, :nationality def initialize( name, dob, nationality = = = nationality end # of initialize. def to_s born on end # of to_s. end # of Person. tom = Person.new( "Thomas", "26/05/1945", :Irish ) dick = Person.new( "Richard", "15/02/1980", :English ) harry = Person.new( "Harold", "02/11/1975", :American ) people = [ tom, dick, harry ] people.each { |person| puts person.to_s } Working Even More Less

class Person attr_accessor :name, :dob, :nationality  reading and writing of attribute def initialize( name, dob, nationality = = = nationality end # of initialize. def to_s born on end # of to_s. end # of Person. tom = Person.new( "Thomas", "26/05/1945", :Irish ) dick = Person.new( "Richard", "15/02/1980", :English ) harry = Person.new( "Harold", "02/11/1975", :American ) people = [ tom, dick, harry ] people.each { |person| puts person.to_s } Working Even Less

More... Ruby So Far Creating classes in Ruby is almost too easy The attr_reader and attr_writer and attr_accessor shortcuts are especially handy Of course, as Ruby supports OO, classes can inherit from other classes (and from more than one when you use mixins) Public, Protected and Private access controls are also available Chapter 3 of The PickAxe has all the details

Singleton methods Singleton methods are defined on individual objects, not classes. class Dog end rover = Dog.new fido = Dog.new def rover.speak puts "Red Rover" end rover.speak  "Red Rover" fido.speak  NoMethodError

Hooks allow the user to gain control at interesting moments during the execution of a program. method_added is predefined hook class MyClass def MyClass.method_added(name) puts "Adding Method #{name}" end def joy puts "HAPPY DAY" end t = MyClass.new puts “howdy ho” class MyClass def another end Adding Method joy howdy ho Adding Method another

Code Eval? class Module def trace_attr(sym) self.module_eval %{ def #{sym} printf "Accessing %s with value end } end class Dog trace_attr :name def = string end Dog.new("Fido").name # => Accessing name with value "Fido" d = Dog.new("Toby") puts "how is #{d} "

Enumerated Type – Ruby doesn’t have them class Color BLUE=1 RED=2 GREEN=3 YELLOW=4 ORANGE=5 PURPLE=6 end Fast way of associating name with value #paint_the_car(Color::YELLOW) puts Color::YELLOW 15 The :: is a unary operator that allows: constants, instance methods and class methods defined within a class or module, to be accessed from anywhere outside the class or module.

class Color def ||= {}  assign if not end def end def {|key,value| yield(key,value)} end Color.add_item :BLUE, 1 Color.add_item :RED, 2 Color.add_item :YELLOW, 3 Color.add_item :GRAY, 4 end #That’s it! We can now use our enum : #my_color = Color::RED if some_condition #And we can loop Color.each do |key,value| puts "my colors are ", key, value end 16