General

Profile

Nondv (Dmitry Non)

  • Login: Nondv
  • Registered on: 07/24/2016
  • Last sign in: 11/11/2019

Issues

open closed Total
Assigned issues 0 0 0
Reported issues 1 3 4

Activity

11/12/2019

08:12 PM Ruby Feature #16341: Proposal: Set#to_proc and Hash#to_proc
shan (Shannon Skipper) wrote:
> This already works!
I can't believe I'm so oblivious :D
Nondv (Dmitry Non)
07:09 PM Ruby Feature #16341: Proposal: Set#to_proc and Hash#to_proc
The main problem is that implicit conversion can be confusing, especially, if it's not obvious what the resulting proc is going to do.
However, I think that hashes are being used *mainly* for making key-value pairs and accessing them ...
Nondv (Dmitry Non)
07:05 PM Ruby Feature #16341: Proposal: Set#to_proc and Hash#to_proc
Speaking of hashes, they could implement implicit proc conversion as well:
```ruby
class Hash
def to_proc
->(key) { self[key] }
end
end
dogs = ['Lucky', 'Tramp', 'Lady']
favourite_food = { 'Lucky' => 'salmon', 'Tramp'...
Nondv (Dmitry Non)
06:58 PM Ruby Feature #16341: Proposal: Set#to_proc and Hash#to_proc
This *is* a syntactic sugar. Using `&` + `to_proc` in this case is the same (not technically, but algorithmically, I guess) as providing an explicit block `something.some_method { |x| some_set.include?(x) }`
I don't find it crucial in...
Nondv (Dmitry Non)

11/11/2019

03:37 PM Ruby Feature #16341: Proposal: Set#to_proc and Hash#to_proc
Well, to be fair, this change is just nice-to-have sugar. I don't expect it to become a thing.
I guess for now the best way to do that is:
```ruby
pets.count { |x| dogs.include?(x) }
# or
pets.count(&dogs.method(:include?))
```...
Nondv (Dmitry Non)
03:18 PM Ruby Feature #16341: Proposal: Set#to_proc and Hash#to_proc
Well, `to_proc` allows to send objects as blocks which can be quite useful not just in case of `select`/`reject`. Also, probably, those two are used more often than `grep`/`grep_v`.
Another example from the top of my head is `count`:
...
Nondv (Dmitry Non)
03:02 PM Ruby Feature #16341 (Open): Proposal: Set#to_proc and Hash#to_proc
``` ruby
class Set
def to_proc
-> (x) { include?(x) } # or method(:include?).to_proc
end
end
```
Usage:
```ruby
require 'set'
banned_numbers = Set[0, 5, 7, 9]
(1..10).reject(&banned_numbers) # ===> [1, 2, 3, 4, 6...
Nondv (Dmitry Non)

02/20/2018

03:24 PM Ruby Feature #14498: Class#to_proc
Hanmac (Hans Mackowiak) wrote:
> Duplicate of #4910
> ...
Hi!
It's not really a duplicate since there is a different suggestion.
I have written PR in C based on `Hash#to_proc` definition
Nondv (Dmitry Non)
03:01 PM Ruby Feature #14498 (Rejected): Class#to_proc
Hello!
I was creating a list of class instances via `some_list.map { |e| SomeClass.new(e) }` and wondered if there's a `Class#to_proc` to make possible doing `some_list.map(&SomeClass)`.
Well, there is not.
Basically, this is what...
Nondv (Dmitry Non)

02/17/2017

11:56 AM Ruby Feature #13172: Method that yields object to block and returns result
.patch file Nondv (Dmitry Non)

Also available in: Atom