Begin, Rescue and Ensure in Ruby

Small tutorial - overview of Begin, Rescue and Ensure in Ruby with code examples and video
26 September 2017   1186

ensure in Ruby ensures that the code is always evaluated. That's why it's called ensure. So, it is equivalent to Java's and C#'s finally.

The general flow of begin/rescue/else/ensure/end looks like this:

begin
  # something which might raise an exception
rescue SomeExceptionClass => some_variable
  # code that deals with some exception
rescue SomeOtherException => some_other_variable
  # code that deals with some other exception
else
  # code that runs only if *no* exception was raised
ensure
  # ensure that this code always runs, no matter what
end

Some blocks form implicit exception blocks. For example, method definitions are implicitly also exception blocks, so instead of writing

def foo
  begin
    # ...
  rescue
    # ...
  end
end

you can write

def foo
  # ...
rescue
  # ...
end

or

def foo
  # ...
ensure
  # ...
end

The same applies to class definitions and module definitions.

Also, this short video tutorial can be userful too.

NGINX to Release Unit 1.3 Beta

Developers expanded the ability to run web applications in Python, PHP, Perl, Ruby and Go
16 July 2018   110

In open access, a beta version of the NGINX Unit 1.3 application server was released. Developers continued to expand the ability to run web applications in Python, PHP, Perl, Ruby and Go. The project code is written in C and is distributed under the Apache 2.0 license.

Features

Version 1.3 eliminates the problems with handling errors when installing HTTP connections.

Among other changes:

  • parameter max_body_size to limit the size of the body of the request;
  • new parameters for setting timeouts when setting up an HTTP connection:
         "settings": {
              "http": {
                  "header_read_timeout": 30,
                  "body_read_timeout": 30,
                  "send_timeout": 30,
                  "idle_timeout": 180,
                  "max_body_size": 8388608
              }
          },
  • automatic use of the Bundler where possible in the Ruby module;
  • http.Flusher interface in the module for the Go language;
  • The possibility of using characters in the UTF-8 encoding in the request headers.

The first version of the NGINX 1.1 application server was released in mid-April 2018. Under the control of NGINX Unit, several applications can be executed simultaneously in different programming languages, the startup parameters of which can be changed dynamically without the need to edit the configuration files and restart.