• Stars
    star
    175
  • Rank 216,623 (Top 5 %)
  • Language
    Ruby
  • License
    MIT License
  • Created over 6 years ago
  • Updated over 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Golang-style defer in Ruby

Deferral - introduce golang style "defer" in Ruby

# gem install deferral

require "deferral/toplevel"
using Deferral::TopLevel
# it makes "defer" without module name available everywhere in this file

def my_method_name
  # ...
  file = File.open("my_file", "r")
  defer { file.close }

  file.write "data..."
  # ...
end # `file.close` is called at the end of method (even when exception thrown)

# or end of blocks
def my_method_name(list)
  # ...
  list.each do |item|
    file = File.open(item, "r")
    defer { file.close }

    file.write "data..."
    # ...
  end # `file.close` is called at the end of block
end

# "deferral" imports `Deferral.defer`, not to inject top-level methods widely.
require "deferral"

# or enable everywhere! (DANGER!)
require "deferral/kernel_ext"

This gem provides a feature to release resources safely, using golang style defer method.

Deferral.defer method does:

  • accept a block argument to execute at the end of caller scope
  • execute specified blocks in reverse order when getting out from specified scope

Resource release blocks are called even when any exception occurs.

See also with_resources gem for try-with-resources style resource allocator.

Disclosure

This library is a kind of PoC to introduce safe resource allocation in Ruby world. Take care about using this library in your production environment.

This library uses/enables TracePoint, and it may collapse optimizations of your Ruby runtime.

API

  • Deferral.defer(&block)

This method registers a block to be called at the end of caller scope (end of method or block). Registered blocks will be called in reverse order (LIFO: last-in, first-out) when this method is called twice or more in a scope.

Introduce defer to top-level namespace

Top-level defer is available via 2 different ways. One is using Refinements, another is modifying Kernel in open-class way.

require "deferral/toplevel"
using Deferral::TopLevel

def my_method
  f = AnyResource.new
  defer { f.close }
  # ...
end

Refinements is a feature of Ruby to apply Module modification in just a file (by using statement). using Deferral::TopLevel introduces top level defer in safer way than modifying Kernel.

require "deferral/kernel_ext"

# now, "defer" is available everywhere...

Requiring deferral/kernel_ext modifies Kernel module globally to add deferral. It's not recommended in most cases.


Authors

License

MIT (See License.txt)

More Repositories

1

shib

WebUI for query engines: Hive and Presto
JavaScript
200
star
2

xbuild

Language runtimes installer for production environment
Shell
197
star
3

fluent-plugin-forest

Ruby
149
star
4

fluent-plugin-secure-forward

Ruby
140
star
5

presto-client-node

Distributed query engine Presto client library for node.js
JavaScript
126
star
6

fluent-plugin-mysql

Ruby
91
star
7

fluent-agent-lite

Lightweight log delivery agent works w/ fluentd
Perl
79
star
8

fluent-plugin-parser

Ruby
75
star
9

fluent-plugin-flowcounter

TODO: one-line summary of your gem
Ruby
54
star
10

fluent-plugin-datacounter

Ruby
47
star
11

mysql2-cs-bind

'mysql2' extension to add pseudo prepared statement
Ruby
38
star
12

fluent-plugin-growthforecast

TODO: one-line summary of your gem
Ruby
37
star
13

maccro

Macro in Ruby
Ruby
30
star
14

fluent-plugin-file-alternative

Ruby
25
star
15

fluent-plugin-ping-message

Ruby
24
star
16

yabitz

Yet Another Business Information Tracker Z: host management application
Ruby
22
star
17

fluent-plugin-sampling-filter

TODO: one-line summary of your gem
Ruby
22
star
18

msgpack-inspect

A tool to inspect and dump the MessagePack binary data: msgpack.org[msgpack-inspect]
Ruby
20
star
19

fluent-plugin-route

This is copy of frsyuki's out_route
Ruby
20
star
20

fluent-plugin-numeric-monitor

Ruby
19
star
21

fluent-plugin-notifier

Ruby
18
star
22

rb-growthforecast

Ruby
17
star
23

astarisk

AST visualizer, named from "AST a risk"
Ruby
16
star
24

right_speed

Ruby
15
star
25

fluent-plugin-config-expander

Ruby
14
star
26

fluent-plugin-numeric-counter

Ruby
12
star
27

with_resources

Add "with" method in Ruby to allocate/release resources in safe way
Ruby
11
star
28

Apache-Log-Parser

Log Parser for Apache common, combined and other custom styles
Perl
11
star
29

fluent-mixin-config-placeholders

Ruby
10
star
30

fluent-plugin-pull_forward

Ruby
10
star
31

LFA

Web application framework to perform as Lambda Function Adapter
Ruby
8
star
32

scribe_line

Python script collection for log transfer with scribe
Python
7
star
33

binding-slicer

Let you write binding[:a, :b, :c] => Hash {a: a, b: b, c: c}
Ruby
7
star
34

fluent-plugin-hoop

TODO: one-line summary of your gem
Ruby
6
star
35

mysqldef_lambda_package

Docker container and scripts to run k0kubun/mysqldef
JavaScript
6
star
36

shibui

Perl
6
star
37

p5-Net-GrowthForecast

Client library for GrowthForecast
Perl
6
star
38

fluent-plugin-buffer-lightening

Ruby
6
star
39

stratum

O/R mapper library for ruby and MySQL on additional architecture
Ruby
6
star
40

fluent-mixin-plaintextformatter

Ruby
5
star
41

fluent-agent

Perl
4
star
42

fluent-plugin-ikachan

Ruby
4
star
43

fluentd-leak-test

Ruby
4
star
44

logstash-output-fluentd

Logstash plugin to forward data to Fluentd
Ruby
4
star
45

fluent-helper-plugin-spec

RSpec helper for Fluentd plugin development
Ruby
4
star
46

Net-Hadoop-DFSAdmin-ReportParser

Perl
4
star
47

fluent-plugin-dummydata-producer

Ruby
3
star
48

itunesconnect-reviews-bookmarklet

bookmarklet to load all reviews from all of countries in iTunes Connect
3
star
49

passenger-monitor

HTTP interface same as mod_status for passenger-status
Ruby
3
star
50

remote_driver

Python
3
star
51

fluentd-tester

Ruby
3
star
52

fluent-plugin-encrypt

Ruby
3
star
53

fluent-plugin-ruby-memory-usage-profiler

Fluentd plugin to output memory profiler information for debugging of ruby/fluentd itself
Ruby
3
star
54

fluentd-book-samples

Ruby
3
star
55

fluentd-v1-checker

Ruby
2
star
56

Net-Hadoop-WebHDFS

Perl
2
star
57

Net-Hadoop-Hive-QueryBuilder

Perl
2
star
58

ruby-memory-usage-profiler

Ruby
2
star
59

fluent-plugin-amplifier-filter

Ruby
2
star
60

scribed_launcher

start-stop script for facebook scribed
Python
2
star
61

isucon3-final-code

Perl
2
star
62

simpleoauth-gae

OAuth 1.0 library for Python on Google App Engine
Python
2
star
63

Net-Hadoop-Hoop

Hoop client library perl module
Perl
2
star
64

MessagePack-RPC-HTTP-Client

Perl
2
star
65

whada

Web Authentication Data Aggregator
Perl
2
star
66

fluent-plugin-reducer

TODO: one-line summary of your gem
Ruby
1
star
67

fluent-plugin-buffered-stdout

Ruby
1
star
68

fluent-plugin-http_file_upload

Ruby
1
star
69

ruby-cli

Ruby
1
star
70

node-scribed

facebook scribe server implementation on node.js (highly experimental)
JavaScript
1
star
71

fluent-plugin-deparser

Ruby
1
star
72

fluent-plugin-delay-inspector

Ruby
1
star
73

isucon7-elimination

Ruby
1
star
74

demo-webapps

Very simple Ruby webapps to verify app servers
Ruby
1
star
75

dyna_mo

Dynamic scope implementation for method overriding
Ruby
1
star
76

fluent-plugin-flatten-filter

Ruby
1
star
77

woothee-sqale

Ruby
1
star
78

logstash-output-treasure_data

Logstash output plugin to store data on Treasure Data service https://www.treasuredata.com/
Ruby
1
star
79

Net-Hadoop-HuahinManager

client library for Huahin Manager
Perl
1
star
80

fluent-plugin-test-counter

TODO: one-line summary of your gem
Ruby
1
star