Chapter 2.2 – More about Ruby Maciej Mensfeld Presented by: Maciej Mensfeld More about Ruby dev.mensfeld.pl github.com/mensfeld senior.

Slides:



Advertisements
Similar presentations
More about Ruby Maciej Mensfeld Presented by: Maciej Mensfeld More about Ruby dev.mensfeld.pl github.com/mensfeld.
Advertisements

Exception Handling Chapter 15 2 What You Will Learn Use try, throw, catch to watch for indicate exceptions handle How to process exceptions and failures.
© The McGraw-Hill Companies, 2006 Chapter 15. © The McGraw-Hill Companies, 2006 Exceptions an exception is an event that occurs during the life of a program.
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
1 Lecture 11 Interfaces and Exception Handling from Chapters 9 and 10.
Exception Handling Introduction Exception handling is a mechanism to handle exceptions. Exceptions are error like situations. It is difficult to decide.
Computer Science 1620 Loops.
Introduction to Python
Guide To UNIX Using Linux Third Edition
Chapter 3 Planning Your Solution
Introduction to Methods
Structure of program You must start with a module import# You must then encapsulate any while loop in a main function at the start of the program Then.
Fundamentals of Python: From First Programs Through Data Structures
Exceptions COMPSCI 105 S Principles of Computer Science.
1 Chapter One A First Program Using C#. 2 Objectives Learn about programming tasks Learn object-oriented programming concepts Learn about the C# programming.
JS Arrays, Functions, Events Week 5 INFM 603. Agenda Arrays Functions Event-Driven Programming.
REPETITION STRUCTURES. Topics Introduction to Repetition Structures The while Loop: a Condition- Controlled Loop The for Loop: a Count-Controlled Loop.
Fundamentals of Python: First Programs
Lecture Note 3: ASP Syntax.  ASP Syntax  ASP Syntax ASP Code is Browser-Independent. You cannot view the ASP source code by selecting "View source"
Object Oriented Programming
And other languages…. must remember to check return value OR, must pass label/exception handler to every function Caller Function return status Caller.
Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Invitation to Computer Science, Java Version, Second Edition.
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
Introduction to Python 2 Dr. Bernard Chen University of Central Arkansas PyArkansas 2011.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Errors And How to Handle Them. GIGO There is a saying in computer science: “Garbage in, garbage out.” Is this true, or is it just an excuse for bad programming?
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Python – Part 1 Python Programming Language 1. What is Python? High-level language Interpreted – easy to test and use interactively Object-oriented Open-source.
Chapter 24 Exception CSC1310 Fall Exceptions Exceptions Exceptions are events that can modify the flow or control through a program. They are automatically.
Controlling Execution Programming Right from the Start with Visual Basic.NET 1/e 8.
Chapter Function Basics CSC1310 Fall Function function (subroutine, procedure)a set of statements different inputs (parameters) outputs In.
Controlling Execution Dong Shao, Nanjing Unviersity.
Objects & Dynamic Dispatch CSE 413 Autumn Plan We’ve learned a great deal about functional and object-oriented programming Now,  Look at semantics.
Exceptions Chapter 16 This chapter explains: What as exception is Why they are useful Java exception facilities.
Exceptions and Assertions Chapter 15 – CSCI 1302.
CSC 1010 Programming for All Lecture 4 Loops Some material based on material from Marty Stepp, Instructor, University of Washington.
More on Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
12. MODULES Rocky K. C. Chang November 6, 2015 (Based on from Charles Dierbach. Introduction to Computer Science Using Python and William F. Punch and.
JavaScript and Ajax (Control Structures) Week 4 Web site:
Chapter 2 - OOP Maciej Mensfeld Presented by: Maciej Mensfeld More about OOP dev.mensfeld.pl github.com/mensfeld.
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
Copyright © Curt Hill Flow of Control A Quick Overview.
More on Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
similar concepts, different syntax
Data Virtualization Tutorial: Introduction to SQL Script
Topics Introduction to Repetition Structures
Introduction to PL/SQL
Topics Introduction to Repetition Structures
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
CHAPTER FOUR Functions.
Python Primer 2: Functions and Control Flow
Topics Introduction to File Input and Output
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Packages and Interfaces
Throwing and catching exceptions
Exceptions.
Problems Debugging is fine and dandy, but remember we divided problems into compile-time problems and runtime problems? Debugging only copes with the former.
Python Syntax Errors and Exceptions
CISC101 Reminders All assignments are now posted.
CISC101 Reminders Assignment 3 due next Friday. Winter 2019
COMPUTER PROGRAMMING SKILLS
Introduction to Computer Science
Python While Loops.
Topics Introduction to File Input and Output
CMSC 202 Exceptions.
Exception Handling.
Presentation transcript:

Chapter 2.2 – More about Ruby Maciej Mensfeld Presented by: Maciej Mensfeld More about Ruby dev.mensfeld.pl github.com/mensfeld senior ruby senior ruby

Chapter 2.2 – More about Ruby Maciej Mensfeld Exceptions Ruby provide a nice mechanism to handle exceptions. We enclose the code that could raise an exception in a begin/end block and use rescue clauses to tell Ruby the types of exceptions we want to handle.

Chapter 2.2 – More about Ruby Maciej Mensfeld Exceptions Using retry statement

Chapter 2.2 – More about Ruby Maciej Mensfeld Exceptions The following is the flow of the process: 1.an exception occurred at open 2.went to rescue 3.fname was re-assigned 4.by retry went to the beginning of the begin 5.this time file opens successfully 6.continued the essential process Notice that if the file of re-substituted name does not exist this example code retries infinitely. Be careful if you use retry for an exception process.

Chapter 2.2 – More about Ruby Maciej Mensfeld Exceptions - raise You can use raise statement to raise an exception. The following method raises an exception whenever it's called. It's second message will be printed.

Chapter 2.2 – More about Ruby Maciej Mensfeld Exceptions - ensure Ensure goes after the last rescue clause and contains a chunk of code that will always be executed as the block terminates. It doesn't matter if the block exits normally, if it raises and rescues an exception, or if it is terminated by an uncaught exception. the ensure block will get run.

Chapter 2.2 – More about Ruby Maciej Mensfeld Exceptions - else If the else clause is present, it goes after the rescue clauses and before any ensure. The body of an else clause is executed only if no exceptions are raised by the main body of code.

Chapter 2.2 – More about Ruby Maciej Mensfeld Class Exception To be even more specific about an error, you can define your own Exception subclass

Chapter 2.2 – More about Ruby Maciej Mensfeld Class Exception

Chapter 2.2 – More about Ruby Maciej Mensfeld Modules Modules are a way of grouping together methods, classes, and constants. Modules give you two major benefits: 1.Modules provide a namespace and prevent name clashes 2.Modules implement the mixin facility Modules define a namespace, a sandbox in which your methods and constants can play without having to worry about being stepped on by other methods and constants.

Chapter 2.2 – More about Ruby Maciej Mensfeld Modules

Chapter 2.2 – More about Ruby Maciej Mensfeld Modules

Chapter 2.2 – More about Ruby Maciej Mensfeld Modules

Chapter 2.2 – More about Ruby Maciej Mensfeld Modules – Class methods

Chapter 2.2 – More about Ruby Maciej Mensfeld Instance extending

Chapter 2.2 – More about Ruby Maciej Mensfeld Ruby Gems A gem is a packaged Ruby application or library. It has a name (e.g. rake) and a version (e.g ). RubyGems is a package manager for the Ruby programming language that provides a standard format for distributing Ruby programs and libraries (in a self-contained format called a "gem"), a tool designed to easily manage the installation of gems, and a server for distributing them. It is analogous to EasyInstall for the Python programming language. RubyGems is now part of the standard library from Ruby version 1.9.

Chapter 2.2 – More about Ruby Maciej Mensfeld Ruby Gems Listing all installed gems Installing remote gem

Chapter 2.2 – More about Ruby Maciej Mensfeld Ruby Gems Using Ruby Gems

Chapter 2.2 – More about Ruby Maciej Mensfeld Blocks, Procs and Lambdas Taken from: Blocks, Procs and lambdas (referred to as closures in Computer Science) are one of the most powerful aspects of Ruby, and also one of the most misunderstood. This is probably because Ruby handles closures in a rather unique way. Making things more complicated is that Ruby has four different ways of using closures

Chapter 2.2 – More about Ruby Maciej Mensfeld Blocks, Procs and Lambdas - Yielding So to recap what is happening: 1. Send iterate! to the Array of numbers. 2. When yield is called with the number n (first time is 1, second time is 2, etc…), pass the number to the block of code given. 3. The block has the number available (also called n) and squares it. As it is the last value handled by the block, it is returned automatically. 4. Yield outputs the value returned by the block, and rewrites the value in the array. 5. This continues for each element in the array. What we now have is a flexible way to interact with our method. Think of blocks as giving your method an API, where you can determine to square each value of the array, cube them or convert each number to a string and print them to the screen. The options are infinite, making your method very flexible, and as such, very powerful.

Chapter 2.2 – More about Ruby Maciej Mensfeld Blocks, Procs and Lambdas - Proc Blocks are very handy and syntactically simple, however we may want to have many different blocks at our disposal and use them multiple times. As such, passing the same block again and again would require us to repeat ourself. However, as Ruby is fully object- oriented, this can be handled quite cleanly by saving reusable code as an object itself. This reusable code is called a Proc (short for procedure). The only difference between blocks and Procs is that a block is a Proc that cannot be saved, and as such, is a one time use solution.

Chapter 2.2 – More about Ruby Maciej Mensfeld Blocks, Procs and Lambdas - Proc The above is how most languages handle closures and is exactly the same as sending a block. However, this does not look „Ruby like”. The above reason is exactly why Ruby has blocks to begin with, and that is to stay within its familiar end concluding syntax.

Chapter 2.2 – More about Ruby Maciej Mensfeld Blocks, Procs and Lambdas - Proc What if we want to pass two or more closures to a method? If this is the case, blocks quickly become too limiting. By having Procs however, we can do something like this:

Chapter 2.2 – More about Ruby Maciej Mensfeld Blocks, Procs and Lambdas - Lambdas On first look, lambdas seem to be exactly the same as Procs. However, there are two subtle differences. The first difference is that, unlike Procs, lambdas check the number of arguments passed.

Chapter 2.2 – More about Ruby Maciej Mensfeld Blocks, Procs and Lambdas - Lambdas The second difference is that lambdas have diminutive returns. What this means is that while a Proc return will stop a method and return the value provided, lambdas will return their value to the method and let the method continue on. Part of Ruby’s syntax is that arguments (a Proc in this example) cannot have a return keyword in it. However, a lambda acts just like a method, which can have a literal return, and thus sneaks by this requirement unscathed!

Chapter 2.2 – More about Ruby Maciej Mensfeld Blocks, Procs and Lambdas

Chapter 2 - OOP Maciej Mensfeld THX Maciej Mensfeld dev.mensfeld.pl github.com/mensfeld