General Purpose Language Ruby Kit Chan COMP3351 Programming Languages November 9, 2007.

Slides:



Advertisements
Similar presentations
Microsoft Research March 20, 2000 A Programming Language for Developing Interactive Web Services Claus Brabrand BRICS, University of Aarhus, Denmark.
Advertisements

Types in Ruby and other languages….  Classes and objects (vs prototypes)  Instance variables/encapsulation  Object creation  Object equality/comparison.
Adapted from Scott, Chapter 6:: Control Flow Programming Language Pragmatics Michael L. Scott.
Language of the Month If it’s December, it must be Ruby! Adam Coffman and Brent Beer.
Ruby: An introduction - Who am I? Maciej Mensfeld Presented by: Maciej Mensfeld Ruby: An introduction dev.mensfeld.pl github.com/mensfeld.
CSE 341, Winter Type Systems Terms to learn about types: –Type –Type system –Statically typed language –Dynamically typed language –Type error –Strongly.
Ruby on Rails Tutorial Peter Mosca April, Ruby on Rails Tutorial Ruby History Invented 12 years ago in Japan by Yukihiro Matsumoto Spent first 5.
Reasons to study concepts of PL
Ruby The Gem of new programming languages. An interpreted scripting language.
ISBN Lecture 01 Preliminaries. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Lecture 01 Topics Motivation Programming.
CSE S. Tanimoto Syntax and Types 1 Representation, Syntax, Paradigms, Types Representation Formal Syntax Paradigms Data Types Type Inference.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
By Logan Phipps Hal student.  This power point explains some common programming languages enjoy  When done click on the home button to return to home.
Introduction to JavaScript for Python Programmers
Lecture 1: Overview of Java. What is java? Developed by Sun Microsystems (James Gosling) A general-purpose object-oriented language Based on C/C++ Designed.
CSE 1301 J Lecture 2 Intro to Java Programming Richard Gesick.
CMSC 202 Computer Science II for Majors Fall 2009 Introduction.
Introduction to Ruby CSE 413 Autumn 2008 Credit: Dan Grossman, CSE341.
First appearedFeaturesMain paradigmsPopular uses COMPUTING Basic FOR A=1 TO 100 IF A MOD 15 = 0 THEN PRINT “FizzBuzz” ELSE IF A MOD 3 = 0 THEN PRINT “Fizz”
Language Evaluation Criteria
CSE 341, S. Tanimoto Concepts 1- 1 Programming Language Concepts Formal Syntax Paradigms Data Types Polymorphism.
CMSC 330: Organization of Programming Languages 1 Overview of Ruby.
A TOUR OF RUBY 2011 ACM Class, Dong Xie. What is Ruby?  Dynamic programming language  Complex but expressive grammar  A core class library with rich.
1 Perl Perl basics Perl Elements Arrays and Hashes Control statements Operators OOP in Perl.
BIT 1003 – Presentation 7. Contents GENERATIONS OF LANGUAGES COMPILERS AND INTERPRETERS VIRTUAL MACHINES OBJECT-ORIENTED PROGRAMMING SCRIPTING LANGUAGES.
Static and Dynamic Behavior CMPS Power of OOP Derives from the ability of objects to change their behavior dynamically at run time. Static – refers.
CSC3315 (Spring 2009)1 CSC 3315 Programming Languages Hamid Harroud School of Science and Engineering, Akhawayn University
Chapter 1 - Introduction
Lecture # 5 Methods and Classes. What is a Method 2 A method is a set of code which is referred to by name and can be called (invoked) at any point in.
Sather vs Java §Brian Oh §Ann Win. l Introduction (of Sather) l History (of Sather) l Comparison between Java and Sather l Sample - “Hello World” l Future.
Ruby! Ronald L. Ramos. What is Ruby? Ruby is a scripting language designed by Yukihiro Matsumoto, also known as Matz. It runs on a variety of platforms,
JAVA SERVER PAGES. 2 SERVLETS The purpose of a servlet is to create a Web page in response to a client request Servlets are written in Java, with a little.
Ruby. Who am I? 1- Bunlong Van 2- Sreyleap Lay, 3- Chhon Meily Blog:
1 Programming Language History and Evolution In Text: Chapter 2.
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Ruby. Quick History Name chosen because it was the birthstone of the colleage of the creator Yukihiro Matsumoto First public release Dec Created.
Ruby on Rails. What is Ruby? Programming Language Object-oriented Interpreted.
Trends in Scripting Languages History For me the purpose of life is partly to have joy. Programmers often feel joy when they can concentrate on the creative.
CSSE501 Object-Oriented Development. Chapter 11: Static and Dynamic Behavior  In this chapter we will examine the differences between static and dynamic.
Applied Computing Technology Laboratory QuickStart C# Learning to Program in C# Amy Roberge & John Linehan November 7, 2005.
An Introduction to Java – Part 1 Dylan Boltz. What is Java?  An object-oriented programming language  Developed and released by Sun in 1995  Designed.
National Taiwan University Department of Computer Science and Information Engineering National Taiwan University Department of Computer Science and Information.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 6: Object-Oriented Programming.
CIT 383: Administrative ScriptingSlide #1 CIT 383: Administrative Scripting Introduction.
We will talking about story of JAVA language. By Kristsada Songpartom.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
1. An Introduction A Programming Language A Technology Java Development Kit Java API One Language: Three Editions Standard Edition Enterprise Edition.
C++ The reason why it is still in use. Table of Contents Disadvantages Disadvantages Advantages Advantages Compare with object-oriented programming language.
RUBY by Ryan Chase.
1-1 An Introduction to Functional Programming Sept
Introduction to Computational Modeling of Social Systems Prof. Lars-Erik Cederman Center for Comparative and International Studies (CIS) Seilergraben 49,
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
Functional Programming. Some Functional Languages Lisp Scheme - a dialect of Lisp Haskell Miranda.
Overview: Lecture 6: Dolores Zage. What is a program? n Operations that are to be applied to certain data in a certain sequence (definition holds for.
Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 1 Concordia University Department of Computer Science and Software Engineering SOEN6441 –
Introduction to programming in java Lecture 22 Arrays – Part 2 and Assignment No. 3.
Ruby Tien Ho and Brandon Hostetter. Overview ▸ Object-oriented, functional, imperative, and reflective ▸ Interpreted language ▸ Influenced by Perl, Smalltalk,
Presented By P.SRIVIDYA 085D1A0552 Programming Language.
CMSC 202 Computer Science II for Majors Fall 2010 Introduction Version 9/101.
Introduction CMSC 202 Fall Instructors Mr. Ryan Bergeron – Lecture Section 01 Tues/Thu 1:00 – 2:15 am, Sondheim 111 – Lecture Section 04 Tues/Thu.
Heath Carroll Bill Hanczaryk Rich Porter.  A Theory of Type Polymorphism in Programming ◦ Robin Milner (1977)  Milner credited with introducing the.
Basic 1964 PC general purpose Imperative Small Easy to use.
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)
Scope History of Ruby. Where can you use Ruby? General Features.
Chapter 6 More Conditionals and Loops
Outline Altering flow of control Boolean expressions
CSE 341 Lecture 11 b closures; scoping rules
Presentation transcript:

General Purpose Language Ruby Kit Chan COMP3351 Programming Languages November 9, 2007

Outline Reference Reference Introduction Introduction Ruby is Ruby is Features Features Array Array Loop Loop Ruby v.s. Java Ruby v.s. Java Duck Typing Duck Typing Dynamic Typing Dynamic Typing Type Tag Checking Type Tag Checking Functional Programming Functional Programming

Reference Wikipedia Wikipedia Ruby Official Website Ruby Official Website Learning Ruby Learning Ruby Duck Typing Duck Typing

Introduction Developed by Yukihiro “Matz” Matsumoto, in Feb, 1993 Developed by Yukihiro “Matz” Matsumoto, in Feb, 1993 First released in 1995 First released in Ruby 1.9 developed March, Ruby 1.9 developed March, 2007 Named Named As a gemstone because of a joke with in Matsumoto’s friends alluding to Perl’s name As a gemstone because of a joke with in Matsumoto’s friends alluding to Perl’s name

Ruby is Cross-platform Operating System Cross-platform Operating System Combines syntax by Combines syntax by Perl Perl Smalltalk-like OO features Smalltalk-like OO features Shares features with Shares features with Lisp Lisp Python Python Dylan Dylan CLU CLU

Ruby is The interpreted scripting language for The interpreted scripting language for Quick Quick Easy OO programming Easy OO programming designed for programmer productivity designed for programmer productivity Straight-forward Straight-forward principle of least surprise (POLS) principle of least surprise (POLS) Ruby behaves in a way that minimizing confusion for experienced users Ruby behaves in a way that minimizing confusion for experienced users After you learn Ruby very well After you learn Ruby very well

Ruby is Obedient Obedient>> 'Hello World' => "Hello World” >> 'blink ' * 4 >> 'blink ' * 4 => "blink blink blink blink " => "blink blink blink blink " Your Calculator Your Calculator>> 1+1 => 2 Or>> 2**2**2 => 16

Features Object-oriented Object-oriented Four levels of variables: Four levels of variables: Global$var Global$var Local[a-z] or _; var Local[a-z] or _; var Constant[A-Z] Constant[A-Z] Exception handling Exception handling Iterators & closures Iterators & closures Automatic garbage collecting Automatic garbage collecting Highly portable Highly portable

Array >> numbers = ["zero", "one", "two", "three", "four"] >> numbers = ["zero", "one", "two", "three", "four"] => ["zero", "one", "two", "three", "four"] => ["zero", "one", "two", "three", "four"] => Array => Array >> numbers[0] >> numbers[0] => "zero" => "zero" What arrays do? >> numbers[0].class >> numbers[0].class => String => String >> numbers[0].upcase >> numbers[0].upcase => "ZERO" => "ZERO" >> numbers[0].reverse >> numbers[0].reverse => "orez" => "orez"

Loop If I knew Ruby when I was in grade school……. If I knew Ruby when I was in grade school……. >> 100.times do >> 100.times do.. puts "I won't do that again".. puts "I won't do that again".. end.. end I won't do that again I won't do that again => 100 => 100 My life was going to be much easier My life was going to be much easier

Ruby v.s. Java- Syntax begin_time = Time.now.to_i begin_time = Time.now.to_i i = 0 i = times do 100.times do i += 1 i += 1 j = 0 j = 0 10.times do 10.times do j += 1 j += 1 k = 0 k = times do 100.times do k += 1 k += 1 puts i.to_s + " + " + j.to_s + " + " + k.to_s puts i.to_s + " + " + j.to_s + " + " + k.to_s end end end_time = Time.now.to_i end_time = Time.now.to_i difference = end_time - begin_time difference = end_time - begin_time puts "It took " + difference.to_s + " seconds" puts "It took " + difference.to_s + " seconds" ho = gets ho = gets class test { public static void main(String[] args) { long startTime = System.currentTimeMillis(); for (int i=0; i<=100 ; i++ ) { for (int j=0; j<=10 ; j++) { for (int k=0; k<=100 ; k++ ) { System.out.println( i + " + " + j + " + " + k);}}} long endTime = System.currentTimeMillis(); long difference = (endTime - startTime)/1000; System.out.println("It took " + difference + " seconds"); }

Ruby v.s. Java Performance Performance Ruby: 24 – 26 seconds Ruby: 24 – 26 seconds Java: 1 – 2 seconds Java: 1 – 2 seconds LanguageParadigmTypeChecking Java Imperative Imperative Object-oriented Object-oriented Generic GenericStatic Ruby Imperative Imperative Object-oriented Object-oriented Functional Functional Aspect-oriented Aspect-orientedDynamic(duck)

Duck Typing Ruby interpreter is happy to treat it as it were a duck Ruby interpreter is happy to treat it as it were a duck If an object walks and talks like a duck If an object walks and talks like a duck Duck typing means Duck typing means An object type is defined by what it can do An object type is defined by what it can do Not by what it is Not by what it is Duck typing refers to Duck typing refers to less concerned with the class of an object less concerned with the class of an object more concerned with what methods can be called on it more concerned with what methods can be called on it what operations can be performed what operations can be performed

Duck Typing we use respond_to? we use respond_to? Example Example >> puts ('A string'.respond_to? :to_str) >> puts ('A string'.respond_to? :to_str) true true => nil => nil >> puts (Exception.new.respond_to? :to_str) >> puts (Exception.new.respond_to? :to_str) true true => nil => nil If an object quacks like a duck, just treat it as a duck…. If an object quacks like a duck, just treat it as a duck…. We should treat objects to the methods they define We should treat objects to the methods they define

Duck Typing >> class Duck >> class Duck.. def quack.. def quack.. 'Quack!'.. 'Quack!'.. end.. end.. def swim.. def swim.. 'Paddle paddle paddle...'.. 'Paddle paddle paddle...'.. end.. end => nil => nil >> class Goose >> class Goose.. def honk.. def honk.. 'Honk!'.. 'Honk!'.. end.. end.. def swim.. def swim.. 'Splash splash splash...'.. 'Splash splash splash...'.. end.. end => nil => nil >> >>

Duck Typing >> class DuckRecording >> class DuckRecording.. def quack.. def quack.. play.. play.. end.. end.. def play.. def play.. 'Quack'.. 'Quack'.. end.. end => nil => nil >> def make_it_quack(duck).. duck.quack.. end => nil >> puts make_it_quack(Duck.new) Quack! >> puts make_it_quack(DuckRecording.new) Quack => nil >> def make_it_swim(duck).. duck.swim.. end => nil >> puts make_it_swim(Duck.new) Paddle paddle paddle... => nil >> puts make_it_swim(Goose.new) Splash splash splash... => nil

Add Method to Class Instances Add methods to individual class instances Add methods to individual class instances class Duck class Duck def quack def quack puts 'Quack!' puts 'Quack!' end end def swim def swim puts 'Paddle paddle paddle...' puts 'Paddle paddle paddle...' end end d = Duck.new #create new instance of the class d = Duck.new #create new instance of the class d.quack#call method d.quack#call method d.swim#call method d.swim#call method def d.walk#override existing method with def d.walk#override existing method with #new functionality puts 'I am walking... walking' puts 'I am walking... walking' end end d.walk d.walk => nil => nil => # => # Quack! Quack! => nil => nil Paddle paddle paddle... Paddle paddle paddle... => nil => nil I am walking... walking I am walking... walking => nil => nil irb(main):022:0> irb(main):022:0>

Dynamic Typing Ruby, the data types are not wholly declared on variable Ruby, the data types are not wholly declared on variable Data associated with variables are not known until the time of execution Data associated with variables are not known until the time of execution Advantage: Advantage: flexibility flexibility Less work for the programmer Less work for the programmer

Type Tag Checking Ruby is dynamically typed Ruby is dynamically typed it supports run-time dispatch on tagged data it supports run-time dispatch on tagged data Takes place at run-time Takes place at run-time values bound in variables can acquire different tags depending on the execution path values bound in variables can acquire different tags depending on the execution path

Example var x #declares the name x var x #declares the name x x :=1#associates int val 1 to name x x :=1#associates int val 1 to name x x :=“hi”#associates the string val “hi” to name x x :=“hi”#associates the string val “hi” to name x ……………illegal ……………illegal binding x to values of inconsistent type binding x to values of inconsistent type Pure Dynamically typed system allows the execution Pure Dynamically typed system allows the execution Type tags are attached to values Type tags are attached to values Dynamic typing catches errors during program execution Dynamic typing catches errors during program execution

Example......cont Dynamic typing keeps all program values “tagged” Dynamic typing keeps all program values “tagged” Checks the tag before using any value in an operation Checks the tag before using any value in an operation var x :=1 #binds val 1 to x var x :=1 #binds val 1 to x var y := “hi”#binds val “hi” to y var y := “hi”#binds val “hi” to y var z := x + y#add x to y var z := x + y#add x to y

Example......cont The value bound to x be a pair (integer, 1) The value bound to x be a pair (integer, 1) The value bound to y be a pair (string, “hi”) The value bound to y be a pair (string, “hi”) Attempts to execute the 3 rd line, Attempts to execute the 3 rd line, Checks the type tags integer and string Checks the type tags integer and string If the operation + (addition) is not defined If the operation + (addition) is not defined An error is signaled An error is signaled

Why Ruby? High productivity for programmers High productivity for programmers Execution time is not the main concern Execution time is not the main concern Web Developments Web Developments Projects like Ruby on Rails Projects like Ruby on Rails Functional Programming Functional Programming Paradigm treats computation as the evaluation of mathematical functions Paradigm treats computation as the evaluation of mathematical functions Emphasize on application of functions Emphasize on application of functions Largely being used in academia Largely being used in academia Lambda calculus Lambda calculus Forms the foundation for most models of functional programming Forms the foundation for most models of functional programming

Thank you !!