Presentation on theme: "More about Ruby Maciej Mensfeld Presented by: Maciej Mensfeld More about Ruby dev.mensfeld.pl github.com/mensfeld."— Presentation transcript:
More about Ruby Maciej Mensfeld Presented by: Maciej Mensfeld More about Ruby firstname.lastname@example.org dev.mensfeld.pl github.com/mensfeld
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.
More about Ruby Maciej Mensfeld Exceptions Using retry statement
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.
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.
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.
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.
More about Ruby Maciej Mensfeld Class Exception To be even more specific about an error, you can define your own Exception subclass
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.
More about Ruby Maciej Mensfeld Modules – Class methods
More about Ruby Maciej Mensfeld Instance extending
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. 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.
More about Ruby Maciej Mensfeld Ruby Gems Listing all installed gems Installing remote gem
More about Ruby Maciej Mensfeld Ruby Gems Using Ruby Gems
More about Ruby Maciej Mensfeld Blocks, Procs and Lambdas Taken from: http://www.robertsosinski.com/2008/12/21/understanding-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
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.
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.
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.
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:
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.
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!
More about Ruby Maciej Mensfeld Blocks, Procs and Lambdas
MongoDB Maciej Mensfeld MongoDB introduction MongoDB is a document database that provides high performance, high availability, and easy scalability.
MongoDB Maciej Mensfeld MongoDB introduction - Document Database Documents (objects) map nicely to programming language data types. Embedded documents and arrays reduce need for joins. Dynamic schema makes polymorphism easier. - High Performance Embedding makes reads and writes fast. Indexes can include keys from embedded documents and arrays. Optional streaming writes (no acknowledgments). - High Availability Replicated servers with automatic master failover. - Easy Scalability Automatic sharding distributes collection data across machines. Eventually-consistent reads can be distributed over replicated servers.