https://github.com/dblock/enumerable-detect-value
Unlike Enumerable#detect, #detect_value returns the result of the block being evaluated.
https://github.com/dblock/enumerable-detect-value
Last synced: about 1 year ago
JSON representation
Unlike Enumerable#detect, #detect_value returns the result of the block being evaluated.
- Host: GitHub
- URL: https://github.com/dblock/enumerable-detect-value
- Owner: dblock
- License: mit
- Created: 2013-12-20T22:48:19.000Z (over 12 years ago)
- Default Branch: master
- Last Pushed: 2013-12-21T18:26:17.000Z (over 12 years ago)
- Last Synced: 2025-03-11T18:59:52.912Z (about 1 year ago)
- Language: Ruby
- Size: 121 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE.md
Awesome Lists containing this project
README
Enumerable Detect w/ Value
==========================
[](https://travis-ci.org/dblock/enumerable-detect-value)
## Detect Value
Unlike `Enumerable#detect`, `#detect_value` returns the result of the block being evaluated.
Consider an example where you have an expensive `Geocoder.search` operation and a list of addresses, two of which are fake. The
function returns `nil` for a fake address. We would like to find the geo-location of the first real address.
```ruby
addresses = [
'221B Baker Street, London, UK', # Sherlock Holmes
'1428 Elm Street, Springwood, Ohio', # Nightmare on Elm Street
'350 5th Ave, New York, NY' # Empire State Building
]
first_real_address = addresses.detect do |address|
Geocoder.search(address)
end
first_real_address # 350 5th Ave, New York, NY
```
We do, however, want the geo-location of the first real address. We would have to call `Geocoder.search` on `first_real_address` twice.
Using `detect_value` you can return the geo-location of the first real address.
```ruby
first_geo_location = addresses.detect_value do |address|
Geocoder.search(address)
end
first_geo_location # lat: 40.74830, lng: -73.98554
```
## Contributing
See [CONTRIBUTING](CONTRIBUTING.md).
## Copyright and License
Copyright (c) 2013, Daniel Doubrovkine.
This project is licensed under the [MIT License](LICENSE.md).