https://github.com/jgraichen/proc-value
Take Smalltalks `#value` to Ruby. Dead easy transparent block usage.
https://github.com/jgraichen/proc-value
Last synced: about 1 year ago
JSON representation
Take Smalltalks `#value` to Ruby. Dead easy transparent block usage.
- Host: GitHub
- URL: https://github.com/jgraichen/proc-value
- Owner: jgraichen
- License: mit
- Created: 2013-08-03T17:38:43.000Z (almost 13 years ago)
- Default Branch: master
- Last Pushed: 2025-02-17T06:52:35.000Z (over 1 year ago)
- Last Synced: 2025-04-06T16:46:18.034Z (about 1 year ago)
- Language: Ruby
- Size: 21.5 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Proc::Value
[](https://rubygems.org/gems/proc-value)
[](https://github.com/jgraichen/proc-value/actions/workflows/test.yml)
[](https://codecov.io/gh/jgraichen/proc-value)
**Proc::Value** extends Ruby Objects with a Smalltalk like `#value` method that always returns the object except when called on a block. In this case the block will be evaluated and the result returned. This allows the transparent exchange of static object with lazy evaluated blocks e.g. for configuration classes.
```ruby
class Configuration
def initialize(options)
@options = options
end
def path
@options[:path].value.to_s
end
end
config1 = Configuration.new path: proc { "config/#{RAILS_ENV}/config.yml" }
config2 = Configuration.new path: 'config/config.yml'
config1.path # `path` block will be evaluated here!
# => "config/development/config.yml"
config2.path # Same code. No extra if statement needed.
# => "config/config.yml"
```
## Installation
Add this line to your application's Gemfile:
```ruby
gem 'proc-value'
```
And then execute:
```console
bundle
```
Or install it yourself as:
```console
gem install proc-value
```
## Usage
See example above.
You can also use `Proc.val(proc_or_object)` that does not depend on any injected method and only looks if given object implements a `#call` method or not.
## Contributing
1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Add specs for your feature
4. Add and commit your changes (`git commit -am 'Add some feature'`)
5. Push to the branch (`git push origin my-new-feature`)
6. Create new Pull Request