0.0
Repository is archived
No commit activity in last 3 years
No release in over 3 years
Makes all objects polymorphic with respect to the :call message, allowing to use any objects where a block is expected.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.3
>= 0
>= 0
 Project Readme

ValueProtocol

Allows passing any object where a block is expected.
It is inspired by the way Smalltalk deals with the same scenario, making use of the #value message.

Motivation

I wrote this gem to avoid having to wrap objects inside lambdas (and thus, avoid repeating code) when working with messages like :detect.

I wanted to turn this:

numbers.detect(lambda{ 2 }) { |number| number.even? }

Into something like this, reducing the noise introduced by the lambda part:

numbers.detect(2) { |number| number.even? }

Also, I wanted to be able to pass any object when an implicit block is expected:

# create an instance of a complex condition
even_numbers = EvenNumberConition.new
numbers.select &even_numbers

How does it works?

Basically, it works by implementing :call (returning self), and implementing :to_proc (returning a proc that evaluates self.call) in Object.

It sound more complicated than it really is. Take a look at the implementation and specs!

Usage

Just pass any object where a block is expected (respecting the & operator when needed). That's all.
If you want to implement custom behavior, override the :call method in your class (remember to respect the signature!).

For most scenarios, you won't need to override :to_proc.

Feel like reading a blog post?

Check this out! Rock Your Ruby: The Value of Value

Installation

Add this line to your application's Gemfile:

gem 'value_protocol'

And then execute:

$ bundle

Or install it yourself as:

$ gem install value_protocol

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request