[#86520] [Ruby trunk Bug#14681] `syswrite': stream closed in another thread (IOError) — samuel@...
Issue #14681 has been reported by ioquatix (Samuel Williams).
3 messages
2018/04/12
[#86755] [Ruby trunk Feature#14723] [WIP] sleepy GC — normalperson@...
Issue #14723 has been reported by normalperson (Eric Wong).
6 messages
2018/04/29
[ruby-core:86554] [Ruby trunk Feature#13683] Add strict Enumerable#single
From:
me@...
Date:
2018-04-16 22:45:46 UTC
List:
ruby-core #86554
Issue #13683 has been updated by IotaSpencer (Ken Spencer).
matz (Yukihiro Matsumoto) wrote:
> Hmm, I don't like the name `single`. Besides that, I think it may be useful for database access, but I don't see the use-case of this method for generic Enumerable.
>
> Matz.
I think of single as a method towards mutual exclusivity.
If an Array or Enumerable from another expression should only have a single element,
then this gives the process a much faster setup and possible rescue, as I currently have
one of my projects checking for the existence of 3 headers, `X-GitHub-Event`, `X-GitLab-Event`,
and `X-Gogs-Event`, and I found the easiest way was to use `one` from Enumerable, but I wanted it
to error out so that I could catch it with the rest of my raised exceptions from other errors that
arise in the handling of the request.
How about these for suggestions.
`one_or_raise`
`one_or_nothing`
Part of my code for context.
~~~ ruby
events = {'github' => github, 'gitlab' => gitlab, 'gogs' => gogs
}
events_m_e = events.values.one?
case events_m_e
when true
event = 'push'
service = events.select { |key, value| value }.keys.first
when false
halt 400, {'Content-Type' => 'application/json'}, {message: 'events are mutually exclusive', status: 'failure'
}.to_json
else halt 400, {'Content-Type' => 'application/json'}, {'status': 'failure', 'message': 'something weird happened'
}
end
~~~
----------------------------------------
Feature #13683: Add strict Enumerable#single
https://bugs.ruby-lang.org/issues/13683#change-71494
* Author: dnagir (Dmytrii Nagirniak)
* Status: Feedback
* Priority: Normal
* Assignee:
* Target version:
----------------------------------------
### Summary
This is inspired by other languages and frameworks, such as LINQ's [Single](https://msdn.microsoft.com/en-us/library/bb155325%28v=vs.110%29.aspx) (pardon MSDN reference), which has very big distinction between `first` and `single` element of a
collection.
- `first` normally returns the top element, and the developer assumes
there could be many;
- `single` returns one and only one element, and it is an error if there
are none or more than one.
We, in Ruby world, very often write `fetch_by('something').first`
assuming there's only one element that can be returned there.
But in majority of the cases, we really want a `single` element.
The problems with using `first` in this case:
- developer needs to explicitly double check the result isn't `nil`
- in case of corrupted data (more than one item returned), it will never
be noticed
`Enumerable#single` addresses those problems in a very strong and
specific way that may save the world by simply switching from `first` to
`single`.
### Other information
- we may come with a better internal implementation (than `self.map`)
- better name could be used, maybe `only` is better, or a bang version?
- re-consider the "block" implementation in favour of a separate method (`single!`, `single_or { 'default' }`)
The original implementation is on the ActiveSupport https://github.com/rails/rails/pull/26206
But it was suggested to discuss the possibility of adding it to Ruby which would be amazing.
--
https://bugs.ruby-lang.org/
Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>