1 / 27

Chapter 2.2 – More about Ruby

Chapter 2.2 – More about Ruby. More about Ruby. Presented b y :. Maciej Mensfeld. senior ruby developer@wordwatch.com senior ruby developer@furioustribe.com. maciej@mensfeld.pl dev.mensfeld.pl github.com/mensfeld. Maciej Mensfeld. Chapter 2.2 – More about Ruby. Exceptions.

mili
Download Presentation

Chapter 2.2 – More about Ruby

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 2.2 – More about Ruby More about Ruby Presented by: Maciej Mensfeld senior ruby developer@wordwatch.com senior ruby developer@furioustribe.com maciej@mensfeld.pl dev.mensfeld.pl github.com/mensfeld Maciej Mensfeld

  2. Chapter 2.2 – More about Ruby 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. Maciej Mensfeld

  3. Chapter 2.2 – More about Ruby Exceptions Usingretrystatement Maciej Mensfeld

  4. Chapter 2.2 – More about Ruby Exceptions The following is the flow of the process: an exception occurred at open went to rescue fname was re-assigned by retry went to the beginning of the begin this time file opens successfully 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. Maciej Mensfeld

  5. Chapter 2.2 – More about Ruby 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. Maciej Mensfeld

  6. Chapter 2.2 – More about Ruby 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. Maciej Mensfeld

  7. Chapter 2.2 – More about Ruby 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. Maciej Mensfeld

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

  9. Chapter 2.2 – More about Ruby Class Exception Maciej Mensfeld

  10. Chapter 2.2 – More about Ruby Modules Modules are a way of grouping together methods, classes, and constants. Modules give you two major benefits: Modules provide a namespace and prevent name clashes 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. Maciej Mensfeld

  11. Chapter 2.2 – More about Ruby Modules Maciej Mensfeld

  12. Chapter 2.2 – More about Ruby Modules Maciej Mensfeld

  13. Chapter 2.2 – More about Ruby Modules Maciej Mensfeld

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

  15. Chapter 2.2 – More about Ruby Instance extending Maciej Mensfeld

  16. Chapter 2.2 – More about Ruby Ruby Gems A gem is a packaged Ruby application or library. It has a name (e.g. rake) and a version (e.g. 0.4.16). 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. Maciej Mensfeld

  17. Chapter 2.2 – More about Ruby Ruby Gems Listing all installed gems Installingremote gem Maciej Mensfeld

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

  19. Chapter 2.2 – More about Ruby Blocks, Procs and Lambdas 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 Taken from: http://www.robertsosinski.com/2008/12/21/understanding-ruby-blocks-procs-and-lambdas/ Maciej Mensfeld

  20. Chapter 2.2 – More about Ruby Blocks, Procs and Lambdas - Yielding So to recap what is happening: Send iterate! to the Array of numbers. 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. 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. Yield outputs the value returned by the block, and rewrites the value in the array. 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. Maciej Mensfeld

  21. Chapter 2.2 – More about Ruby 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. Maciej Mensfeld

  22. Chapter 2.2 – More about Ruby 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. Maciej Mensfeld

  23. Chapter 2.2 – More about Ruby 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: Maciej Mensfeld

  24. Chapter 2.2 – More about Ruby 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. Maciej Mensfeld

  25. Chapter 2.2 – More about Ruby 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! Maciej Mensfeld

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

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

More Related