More about Ruby

gorgeousvassalSoftware and s/w Development

Nov 7, 2013 (3 years and 9 months ago)

112 views

Chapter 2.2


More about Ruby

Maciej Mensfeld

Presented
b
y
:


Maciej Mensfeld

More about Ruby

maciej@mensfeld.pl

dev.mensfeld.pl

github.com/mensfeld

senior ruby developer@wordwatch.com

senior ruby developer@furioustribe.com

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

E
nsure

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. 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.

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:
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

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

W
hat 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

maciej@mensfeld.pl

dev.mensfeld.pl

github.com/mensfeld