CSE 300 Software Engineering CSE 300 CSE 300

  • Slides: 10
Download presentation
CSE 300 - Software Engineering CSE 300

CSE 300 - Software Engineering CSE 300

CSE 300 - Software Engineering Discussion Items • • Ruby methods, blocks, procs, lambda

CSE 300 - Software Engineering Discussion Items • • Ruby methods, blocks, procs, lambda Exception handling Threads and Fibers File IO 2 by Manish Shrotriya

CSE 300 - Software Engineering Methods, Procs, Lambda • A method is a named

CSE 300 - Software Engineering Methods, Procs, Lambda • A method is a named block of parameterized code associated with one or more objects. • A method invocation specifies the method name, the object on which it is to be invoked (sometimes called the receiver) • Object representation of methods can obtained using class Method • Functions are named block of parameterized code that is not associated with any object. 3 by Manish Shrotriya

CSE 300 - Software Engineering Methods, Procs, Lambda • Blocks are block of parameterized

CSE 300 - Software Engineering Methods, Procs, Lambda • Blocks are block of parameterized executable code that can be invoked only though another method. • An object representing a block can be obtained using Proc class. Proc objects are of two types, procs and lambdas • procs and lambda are not methods instead they are type of functions called closures. They retain the access to all local variable that are available to them in the scope where they are defined. 4 by Manish Shrotriya

CSE 300 - Software Engineering Throw catch and raise rescue • Throw catch is

CSE 300 - Software Engineering Throw catch and raise rescue • Throw catch is more like cascaded exit from a method where throw is occurred to the point where catch is called. One can think of throw catch as java throw catch without any exception. It is more of jumping out of many nested loop to some top level loop. • raise is the method to throw an exception for predefined valid error condition. Rescue is a clause that is given to handle an exception. They work like java exception handling. 5 by Manish Shrotriya

CSE 300 - Software Engineering Throw catch and raise rescue • There are multiple

CSE 300 - Software Engineering Throw catch and raise rescue • There are multiple way to pass exception to raise. simple message, exception object, any object having exception method can be passed to raise clause • rescue is catch of java. It is there to handle the exception. Multiple rescue are allowed. • $1 is the built-in variable that contains exception in a rescue clause. • raise in rescue simply throws the rescued exception again 6 by Manish Shrotriya

CSE 300 - Software Engineering Exception vs. Standard Error • When we catch and

CSE 300 - Software Engineering Exception vs. Standard Error • When we catch and exception or error by class name we catch all exception inheriting it too. • Exception is the root class of all Errors and exception, That means we will catch all exceptions even for Syntax errors or interrupts too. • Never catch Exception • All user exceptions are subclass of Standard. Error either catch it or use specific custom class that is subclass of Standard. Error 7 by Manish Shrotriya

CSE 300 - Software Engineering Threads • Threads in ruby are straight forward. Using

CSE 300 - Software Engineering Threads • Threads in ruby are straight forward. Using thread class one can create new thread. • new method on thread would create and run it. • Block passed in new thread would be run when thread is executed. • Mutex is used to control the access to any share object. synchronize method prevents simultaneous entry of multiple threads to a given block • Thread. join is the method make caller thread to wait till callee is finished 8 by Manish Shrotriya

CSE 300 - Software Engineering Fiber • Fiber is relatively new concept. Fibers are

CSE 300 - Software Engineering Fiber • Fiber is relatively new concept. Fibers are coroutines. • They are way to toggle/switch between two methods without returning from either of them within the same thread. • Fiber can be created by new method with a block to be executed with the fiber. • Unlike threads, fiber declaration doesn’t automatically start them. • resume method is used to either start a fiber or continue it after suspension using Fiber. yield 9 by Manish Shrotriya

CSE 300 - Software Engineering File IO • File/Dir are classes to list, read

CSE 300 - Software Engineering File IO • File/Dir are classes to list, read and write files/dirs. • One can open a file in read, write or both mode. • File encoding can be set. File mode can be switched from text or binary mode. Once it is switched to binary it cannot be changed back to text. • ARGF is special stream that are designed to use in ruby scripts. It points to the files passed as argument to scripts or input stream to the script (pipe) • One can obtained different type of file locks to synchronize file access. 10 by Manish Shrotriya