Programming Paradigms

Similar documents
Overview of the Ruby Language. By Ron Haley

Programming Paradigms

CSE 341, Autumn 2015, Ruby Introduction Summary

CS 5142 Scripting Languages

Ruby: Introduction, Basics

Ruby: Introduction, Basics

15.1 Origins and Uses of Ruby

CS 231 Data Structures and Algorithms, Fall 2016

Simple Java Reference

Preview from Notesale.co.uk Page 3 of 36

Ruby: Object-Oriented Concepts

Java Object Oriented Design. CSC207 Fall 2014

Programming Paradigms

Programming Paradigms

Enums. In this article from my free Java 8 course, I will talk about the enum. Enums are constant values that can never be changed.

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

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

SOAPScript For squeaky-clean code, use SOAPScript Language Summary Bob Nisco Version 0.5 β

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

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

Lecture 2. Object Orientation 1 / 51

Project 6 Due 11:59:59pm Thu, Dec 10, 2015

Programming Paradigms

Lecture 2. Object Orientation

Strings and Variables

Notes from a Short Introductory Lecture on Scala (Based on Programming in Scala, 2nd Ed.)

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Classes and Objects in Ruby

Type Checking and Type Equality

A First Look At Java. Didactic Module 13 Programming Languages - EEL670 1

Ruby: Introduction, Basics

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

Multiple Inheritance using modules in Ruby

Objective-C. Stanford CS193p Fall 2013

Algorithms and Programming I. Lecture#12 Spring 2015

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

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

OCaml Data CMSC 330: Organization of Programming Languages. User Defined Types. Variation: Shapes in Java

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

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

CS260 Intro to Java & Android 03.Java Language Basics

Computer Science II Lecture 1 Introduction and Background

Introduction to Ruby. Part I

Inheritance and Interfaces

Scheme Quick Reference

CS112 Lecture: Working with Numbers

Object Oriented Programming in C#

Program Fundamentals

Fall 2017 CISC124 9/16/2017

Lecture 2. Object Orientation 1 / 50

Ruby on Rails for PHP and Java Developers

What is Ruby? CSc 372. Comparative Programming Languages. 27 : Ruby Introduction. Department of Computer Science University of Arizona

1 Epic Test Review 2 Epic Test Review 3 Epic Test Review 4. Epic Test Review 5 Epic Test Review 6 Epic Test Review 7 Epic Test Review 8

Rules and syntax for inheritance. The boring stuff

Unit E Step-by-Step: Programming with Python

CEU s (Continuing Education Units) 12 Hours (i.e. Mon Thurs 5 9PM or Sat Sun 8AM 5PM)

egrapher Language Reference Manual

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

Java Review. Fundamentals of Computer Science

Your First Ruby Script

Inheritance and Polymorphism in Java

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

The software crisis. code reuse: The practice of writing program code once and using it in many contexts.

Java Bytecode (binary file)

CIS133J. Working with Numbers in Java

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

Pull Lecture Materials and Open PollEv. Poll Everywhere: pollev.com/comp110. Lecture 12. else-if and while loops. Once in a while

Big software. code reuse: The practice of writing program code once and using it in many contexts.

Inheritance. Notes Chapter 6 and AJ Chapters 7 and 8

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

Computer Components. Software{ User Programs. Operating System. Hardware

Array. Array Declaration:

Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable?

DO NOT. UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N.

STATS 507 Data Analysis in Python. Lecture 2: Functions, Conditionals, Recursion and Iteration

G52CPP C++ Programming Lecture 9

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

Programming overview

CSc 372 Comparative Programming Languages

AP Computer Science Unit 1. Writing Programs Using BlueJ

Chapter 1: A First Program Using C#

CSE 142 Su 04 Computer Programming 1 - Java. Objects

CPL 2016, week 10. Clojure functional core. Oleg Batrashev. April 11, Institute of Computer Science, Tartu, Estonia

Scheme Quick Reference

Pace University. Fundamental Concepts of CS121 1

Object-Oriented Design Lecture 3 CSU 370 Fall 2007 (Pucella) Friday, Sep 14, 2007

Project 5 Due 11:59:59pm Tuesday, April 25, 2017

do fifty two: Language Reference Manual

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

CMSC 330: Organization of Programming Languages

Topic 7: Algebraic Data Types

CS211 Computers and Programming Matthew Harris and Alexa Sharp July 9, Boggle

Notes on Chapter Three

09/02/2013 TYPE CHECKING AND CASTING. Lecture 5 CS2110 Spring 2013

Course Content. Objectives of Lecture 24 Inheritance. Outline of Lecture 24. CMPUT 102: Inheritance Dr. Osmar R. Zaïane. University of Alberta 4

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Software and Programming 1

Decaf Language Reference Manual

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

Java Classes. Produced by. Introduction to the Java Programming Language. Eamonn de Leastar

Transcription:

PP 2016/17 Unit 4 Ruby Advanced 1/42 Programming Paradigms Unit 4 Ruby Advanced J. Gamper Free University of Bozen-Bolzano Faculty of Computer Science IDSE

PP 2016/17 Unit 4 Ruby Advanced 2/42 Outline 1 Functions, Code Blocks and Procs 2 Classes 3 Modules and Mixins

Functions, Code Blocks and Procs PP 2016/17 Unit 4 Ruby Advanced 3/42 Outline 1 Functions, Code Blocks and Procs 2 Classes 3 Modules and Mixins

Functions, Code Blocks and Procs PP 2016/17 Unit 4 Ruby Advanced 4/42 Defining Functions Functions are used to bundle one or more statements into a single unit They can be defined in the console, and without defining a class first Functions are also referred to as methods >> def tell the truth >> true => nil >> tell the truth => true Every function returns something: if no explicit return expr1, expr2,... statement is used, the value of the last processed expression is returned More than one values are returned in an array Like everything else, functions in Ruby are considered objects You can call tell the truth.class or tell the truth.methods

Functions, Code Blocks and Procs PP 2016/17 Unit 4 Ruby Advanced 5/42 Positional Parameters to Functions Functions accept parameters The default are positional parameters, i.e., the order of the parameters matters >> def method name (var1, var2) >> expr.. You can set default values for the parameters, which will be used if method is called without passing parameters >> def method name (var1=value1, var2=value2) >> expr... Function call >> method name param1, param2

Functions, Code Blocks and Procs PP 2016/17 Unit 4 Ruby Advanced 6/42 Positional Parameters to Functions: Example Function with two parameters that are initialized >> def test(a1= Ruby, a2= Perl ) >> puts "The programming language is #{a1}" >> puts "The programming language is #{a2}" Call function with two parameters >> test C, C++ >> The programming language is C >> The programming language is C++ => nil Call function with one parameter (which is the first one) >> test C >> The programming language is C >> The programming language is Perl => nil

Functions, Code Blocks and Procs PP 2016/17 Unit 4 Ruby Advanced 7/42 Named Parameters to Functions Before Ruby 2.0, there was no support for named parameters, which are specified/referred to by a name in the function call For example, in Objective-C a function call can look like this: [window addnew:@"title" xposition:20 yposition:50 width:100 height:50 drawingnow:yes]; Named parameters simplify, e.g., the handling of optional parameters the parameters can be passed in any order

Functions, Code Blocks and Procs PP 2016/17 Unit 4 Ruby Advanced 8/42 Named Parameters Using Hashes A single hash can be used to emulate named parameters (only way before Ruby 2.0) >> def tell the truth(options = {}) >> case options[:profession] >> when :lawyer >> almost certainly not false >> when :doctor >> true >> else >> yep >> tell the truth( :profession => :lawyer ) => "almost certainly not false" >> tell the truth => "yep"

Functions, Code Blocks and Procs PP 2016/17 Unit 4 Ruby Advanced 9/42 Named Parameters as Keyword Arguments Ruby 2.0 introduced keyword arguments, which are named parameters They are followed by a colon (:) and an optional default value When calling a method, the order of the arguments can be in any order without affecting the behavior (not the case for positional arguments) Parameters that are not initialized have to be specified >> def total(subtotal:, tax:10, discount:5) >> subtotal + tax - discount >> total(subtotal:100) => 105 >> total(subtotal:100, discount:20) => 190 >> total(subtotal:100, discount:20) => 190 >> total() ArgumentError: missing keyword: subtotal

Functions, Code Blocks and Procs PP 2016/17 Unit 4 Ruby Advanced 10/42 Code Blocks/1 A code block is basically a function without a name It can be passed as a parameter to a function or method It is delimited by curly braces {...} (inline or single-line block) or do...end (multi-line block) >> 3.times { puts hello } hello hello hello => 3 times is an iterator (method) for the class Fixnum that does something a certain number of times

Functions, Code Blocks and Procs PP 2016/17 Unit 4 Ruby Advanced 11/42 Code Blocks/2 Let s write our own version of times called log2times x.log2times does something log 2 (x) times class Fixnum def log2times i = self while i > 1 i = i / 2 yield end end end >> 2.log2times { puts Hello world! } Hello world! => nil >> 5.log2times { puts Hello world! } Hello world! Hello world! => nil self gives you access to the current object the object that is receiving the current message yield in the method log2times calls the passed code block Ruby has open classes: write an existing class definition, specify/define something and it will be added to the class The code extends the class Fixnum by adding a method log2times

Functions, Code Blocks and Procs PP 2016/17 Unit 4 Ruby Advanced 12/42 Parameters to Code Blocks It is also possible to pass parameters to the code block In the block, a variable is placed in vertical lines to accept parameters >> (0..3).each { x puts x } 0 1 2 3 => 0..3 x assumes the values 0...3 What is the result of the following expression? Equivalent expression: (0..3).each do x puts x end >> [ 2, plus, 3, is, "#{2+3}"].each { x puts x }

Functions, Code Blocks and Procs PP 2016/17 Unit 4 Ruby Advanced 13/42 Procs/1 Code blocks are not first-class citizens of Ruby For example, you cannot assign them to a variable >> y = { x puts x } syntax error,... If you want to do something other than yielding them, you have to convert them to a Proc, which is a class in Ruby, and you can create objects of this type >> y = Proc.new { x puts x } => #<Proc:0xb7367ac4> >> y.call(3) 3 => nil >> y.class => Proc

Functions, Code Blocks and Procs PP 2016/17 Unit 4 Ruby Advanced 14/42 Procs/2 Any code block can be turned into a Proc object if it is passed as a parameter and the parameter is preceded by an ampersand The code block (object) is then executed with the call method (similar to yield) This allows to pass around executible code >> def call block(&block) >> block.call => nil >> def pass block(&block) >> call block(&block) => nil >> pass block { puts Hello block! } Hello block! => nil

Classes PP 2016/17 Unit 4 Ruby Advanced 15/42 Outline 1 Functions, Code Blocks and Procs 2 Classes 3 Modules and Mixins

Classes PP 2016/17 Unit 4 Ruby Advanced 16/42 Class Hierarchy Ruby supports single inheritance, creating a hierarchy of classes The methods class and superclass can be used to obtain, respectively, the class of an object and the parent of a class >> 4.class => Fixnum >> Fixnum.superclass => Integer >> Integer.superclass => Numeric >> Numeric.superclass => Object >> Object.superclass => nil

Classes PP 2016/17 Unit 4 Ruby Advanced 17/42 Ruby Metamodel As in Ruby everything is an object, classes are themselves instances of the class Class >> 4.class.class => Class >> 4.class.superclass => Integer >> Numeric.superclass => Object >> 4.class.superclass.superclass.class => Class

Classes PP 2016/17 Unit 4 Ruby Advanced 18/42 Defining a Class/1 A class is made up of a collection of variables representing the internal state and methods providing behaviours that operate on that state Class names must begin with a capital letter By convention, names that contain more than one word are run together with each word capitalized, e.g., CamelCase Let s start building a class Customer >> class Customer => nil This is the simplest possible class: an empty class (doing nothing)

Classes PP 2016/17 Unit 4 Ruby Advanced 19/42 Defining a Class/2 Initializing the class and creating variables >> class Customer >> @@no of customers=0 >> def initialize(name, addr) >> @name=name >> @addr=addr >> @@no of customers = @@no of customers + 1 Class variables are prepended with @@ Belong to the class and have one value per class Instance variables are prepended with @ Belong to the instances/objects and have different values for each instance Need not to be declared, but are dynamically appended to an object when they are first assigned The initialize method is executed when a new object is created

Classes PP 2016/17 Unit 4 Ruby Advanced 20/42 Accessor Methods for a Class/1 By default, variables are private and can only be directly accessed within an instance method To provide access from outside, accessor methods are needed Accessor methods can have the same name as variables... and addr= is a valid method name When you use them, it looks like you are accessing directly the variables >> class Customer >> def set name( name ) >> @name = name >> def get name >> return @name >> def addr=( addr ) >> @addr = addr >> def addr >> return @addr >> def get no of customers >> return @@no of customers

Classes PP 2016/17 Unit 4 Ruby Advanced 21/42 Accessor Methods for a Class/2 Now we can use our class >> c1 = Customer.new( max, meran ) => #<Customer:0x0000000084fd00 @name="max", @addr="meran"> >> c2 = Customer.new( moritz, meran ) => #<Customer:0x000000008cc4b8 @name="moritz", @addr="meran"> >> c1.addr => "meran" >> c1.addr= bozen => "bozen" >> c1.addr => "bozen" >> c1.get no of customers => 2

Classes PP 2016/17 Unit 4 Ruby Advanced 22/42 Accessor Methods for a Class/3 Since getters and setters are so common, they can be autogenerated >> class Customer >> @@no of customers=0 >> attr accessor :name, :addr attr accessor is a method, which is run when Ruby constructs the class object, and it generates the setter and getter methods for you addr=(addr), addr, name=(name), and name If instead attr :name, :addr is used, only the getter methods are created

Classes PP 2016/17 Unit 4 Ruby Advanced 23/42 Example Tree Class Let s build a class Tree, which allows to create a tree and to traverse it class Tree attr accessor :children, :node name def initialize(name, children=[]) @node name = name @children = children end def traverse(&block) process &block children.each { c c.traverse &block} end def process(&block) block.call self end end

Classes PP 2016/17 Unit 4 Ruby Advanced 24/42 Using the Tree Class Let s create a tree with root node Ruby and children Reia and MacRuby rubytree = Tree.new( Ruby, [Tree.new( Reia ),Tree.new( MacRuby )]) Processing the root node by outputting its name rubytree.process { node puts node.node name} Ruby Traversing the whole tree (printing each node name) rubytree.traverse { node puts node.node name} Ruby Reia MacRuby

Classes PP 2016/17 Unit 4 Ruby Advanced 25/42 Inheritance If we want to create a red-black tree based on our Tree class, we need to add a color and a method for balancing the tree class RedBlackTree < Tree attr accessor :color def initialize(name, color, children=[]) super(name, children) @color = color end def balance()... end end < creates a subclass of an existing class super calls the initialize method of the superclass Tree In Ruby, a class can only inherit from a single other class

Modules and Mixins PP 2016/17 Unit 4 Ruby Advanced 26/42 Outline 1 Functions, Code Blocks and Procs 2 Classes 3 Modules and Mixins

Modules and Mixins PP 2016/17 Unit 4 Ruby Advanced 27/42 Modules/1 A module is a collection of (related) classes, methods/functions and constants module Identifier statement1 statement2... end A module comes together with its own namespace (to avoid name clashes) Similar to classes, but there are no instances (objects) of modules there is no inheritance

Modules and Mixins PP 2016/17 Unit 4 Ruby Advanced 28/42 Modules/2 An example is the Math module >> module Math >> PI = 3.141592654 >> def sqrt( v ) >> #... >> #... It defines various constants, e.g., PI, and functions, e.g., sqrt Constants begin with an uppercase letter

Modules and Mixins PP 2016/17 Unit 4 Ruby Advanced 29/42 Using Modules To use modules, you can use either qualified names >> Math.sqrt(2) => 1.4142135623731 >> Math::PI => 3.14159265358979 or include the module >> include Math => Object >> sqrt(2) => 1.4142135623731 >> PI => 3.14159265358979

Modules and Mixins PP 2016/17 Unit 4 Ruby Advanced 30/42 Modules and Classes Modules become really interesting when used in combination with classes Ruby does not support multiple inheritance However, it does support a mechanism called a mixin If we include a module in a class definition, the module s methods are appended to the class Effectively, the module is mixed in with the class

Modules and Mixins PP 2016/17 Unit 4 Ruby Advanced 31/42 Mixins/1 Unfortunately, it is not that easy to create a mixin In order to use the full power of a module in a class, your class may need to implement certain methods Let s try to get the tree node names sorted in alphabetical order We ll try to do this mixing the module Enumerable into our class Enumerable provides a method called sort

Modules and Mixins PP 2016/17 Unit 4 Ruby Advanced 32/42 Mixins/2 Just including the module Enumerable in our Tree class won t give us the full functionality: >> class Tree >> include Enumerable >> attr accessor :children, :node name >>... => nil >> ruby tree = Tree.new(...) =>... >> ruby tree.sort NoMethodError: undefined method each for... A class wanting to be enumerable must implement the method each to go through all elements In our Tree class, the method needs to go through all nodes

Modules and Mixins PP 2016/17 Unit 4 Ruby Advanced 33/42 Mixins/3 Let s add the method each, which needs an object and a code block as parameter >> class Tree >> include Enumerable >> attr accessor :children, :node name >>... >> def each(&block) >> block.call self >> children.each { c c.each &block} >> ruby tree.sort NoMethodError: undefined method <=> for... We are missing yet another operator

Modules and Mixins PP 2016/17 Unit 4 Ruby Advanced 34/42 Mixins/4 A class wanting to be comparable must implement the spaceship operator <=> This operator is used for comparing two objects: 1 if a < b a <=> b = 1 if a > b 0 if a = b Let s add this operator to our Tree class

Modules and Mixins PP 2016/17 Unit 4 Ruby Advanced 35/42 Mixins/5 class Tree... def <=>(t) return -1 if self.node name < t.node name return 1 if self.node name > t.node name return 0 if self.node name == t.node name return nil end end >> ruby tree.sort => [#<Tree:0xb73bfa6c @node name="mcruby",... The output doesn t look very nice, sort returns an array of trees

Modules and Mixins PP 2016/17 Unit 4 Ruby Advanced 36/42 Success! However, an array supports the method each as well So we can pass a code block to the array, printing the names of the nodes: >> (ruby tree.sort).each { n puts n.node name} McRuby Reia Ruby This small example already hints at the flexibility provided by modules, mixins, and code blocks

Modules and Mixins PP 2016/17 Unit 4 Ruby Advanced 37/42 Success? Well, looking at the code for our Tree class you ll notice that some of the code is redundant Now that we ve implemented the each method, we don t need the traverse method anymore (which does essentially the same thing) So we can refactor the code to make it slimmer and better

Modules and Mixins PP 2016/17 Unit 4 Ruby Advanced 38/42 Final Tree Class class Tree include Enumerable attr accessor :children, :node name def initialize(name, children=[]) @children = children @node name = name end def each(&block) block.call self children.each { c c.each &block} end def <=>(tree)... end end

Modules and Mixins PP 2016/17 Unit 4 Ruby Advanced 39/42 Example Insertion Sort/1 Insertion sort iterates through a list of data, consuming one input element each repetition, and growing a sorted output list In each iteration, it removes one element from the input data, finds the correct position in the sorted output list, and inserts it there Sorting is typically done in-place, by iterating through the input array from the beginning, and growing the sorted list behind it In the array after k iterations the first k +1 entries are sorted If the current element is x it becomes after this iteration

Modules and Mixins PP 2016/17 Unit 4 Ruby Advanced 40/42 Example Insertion Sort/2 Insertion sort in Java class InsertionSort{ static void sort(int[] a){ for (int i = 1; i < a.length; i++){ int val = a[i]; int j = i-1; } } while (j >= 0 && a[j] > val){ a[j+1] = a[j]; j--; } a[j+1] = val; } public static void main(string[] args){ int[] a = {2,43,24,100,3}; sort(a); for (int e : a) System.out.println(e); }

Modules and Mixins PP 2016/17 Unit 4 Ruby Advanced 41/42 Example Insertion Sort/3 Insertion sort in Ruby def insertionsort( a ) a.each index do i val = a[i] j = i - 1 while j >= 0 and a[j] > val a[j+1], a[j] = a[j], a[j+1] j -= 1 end end end a = [2,43,24,100,3] insertionsort(a) puts a

PP 2016/17 Unit 4 Ruby Advanced 42/42 Summary Strengths of Ruby Ruby is a pure object-oriented language, treating objects in a consistent way Ruby is a strongly typed language, but applies dynamic type checking Supports Duck typing, and is therefore very flexible when it comes to substitutability Some nice features not present in other languages: rich methods on arrays, code blocks, modules and mixins Programmers can be very productive using Ruby, can be used like a scripting language Comes with a very successful web development framework: Ruby on Rails The original Twitter implementation was done in Ruby Weaknesses of Ruby Performance: Ruby is not the most efficient language All the flexibility makes it difficult to compile programs Concurrent programming is difficult to do with a state-based language Type Safety: duck typing makes it harder to debug code that has type errors in it