Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Allows you to bind procs to instances of classes

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 lib
Octocat-spinner-32 spec
Octocat-spinner-32 .gitignore
Octocat-spinner-32 Gemfile
Octocat-spinner-32 LICENSE
Octocat-spinner-32 README.md
Octocat-spinner-32 README.mountain_berry_fields.md
Octocat-spinner-32 Rakefile
Octocat-spinner-32 bindable_block.gemspec
README.md

BindableBlock

instance_exec can't take block arguments. Get around that with BindableProc

require 'bindable_block'

User = Struct.new :name
greeter = BindableBlock.new(User) { "Welcome, #{name}" }
greeter.bind(User.new "Josh").call # => "Welcome, Josh"

Here is an example.

Where the abstraction leaks

Bindable block does something that isn't possible in Ruby. It does this with black magick. Unfortunately, that abstraction will leak in the case of return statements. Return statements in blocks will return you from the containing method.

def meth
  Proc.new { return 1 }.call
  2
end

meth # => 1

Return statements in lambdas will return you from the lambda.

def meth
  lambda { return 1 }.call
  2
end

meth # => 2

You would expect a bindable block to continue to behave like the former example, but it will actually behave like the latter example.

At present, I can only think of two ways to fix this:

1) It might be possible to rewrite the AST with ripper. 2) It is probably possible to write this in C.

Neither of these are high on my priority list.

Installation

Add this line to your application's Gemfile:

gem 'bindable_block'

And then execute:

$ bundle

Or install it yourself as:

$ gem install bindable_block
Something went wrong with that request. Please try again.