[#109095] [Ruby master Misc#18888] Migrate ruby-lang.org mail services to Google Domains and Google Workspace — "shugo (Shugo Maeda)" <noreply@...>
Issue #18888 has been reported by shugo (Shugo Maeda).
16 messages
2022/06/30
[ruby-core:108760] [Ruby master Bug#18815] instance_{eval, exec} vs Proc#>>
From:
"zverok (Victor Shepelev)" <noreply@...>
Date:
2022-06-02 16:56:55 UTC
List:
ruby-core #108760
Issue #18815 has been updated by zverok (Victor Shepelev).
@jeremyevans0 I don't think it is a "bug", but maybe a nice feature to have (composite procs being a bit smarter than just a really thin syntax sugar over what you could've done manually). The realistic case I have is a constant with definitions of how to handle some huge list of parameters, it might look like that:
```ruby
FLATTEN = -> { [*_1] }
IDENTITY = -> { _1 }
DATE = -> { Date.parse(_1) }
TRANSFORMATIONS = {
param1: FLATTEN,
param2: FLATTEN,
param3: IDENTITY,
# ....
}
```
...and then, some processing is more complicated and depends on current class' context:
```ruby
param4: -> { allowed?(:something) ? _1 : DEFAULT }
```
...now, if `param4` should also be flattened before handling, I was happy I can write
```ruby
param4: FLATTEN >> -> { allowed?(:something) ? _1 : DEFAULT }
```
...until I understood it wouldn't work :)
Obviously, I can (and did)
```ruby
param4: -> { allowed?(:something) ? FLATTEN.(_1) : DEFAULT }
```
...but the `>>` version looked much more clear, especially in big params list, so you can clearly read: this is flattened, this is flattened and then also post-processed.
----------------------------------------
Bug #18815: instance_{eval,exec} vs Proc#>>
https://bugs.ruby-lang.org/issues/18815#change-97819
* Author: zverok (Victor Shepelev)
* Status: Open
* Priority: Normal
* Backport: 2.7: UNKNOWN, 3.0: UNKNOWN, 3.1: UNKNOWN
----------------------------------------
```ruby
measure = proc { p "self=#{self}"; size }
multiply = proc { '*' * _1 }
'test'.instance_eval(&measure)
# "self=test"
# => 4
'test'.instance_eval(&measure >> multiply)
# "self=main"
# NameError (undefined local variable or method `size' for main:Object)
```
So, `Proc#>>` produces a proc which is not suitable for `instance_eval`/`instance_exec`. The same as the "naive" implementation:
```ruby
# Naive sequence
sequence = proc { measure.call.then(&multiply) }
```
...but is it possible to make the combination to behave like the first proc is not wrapped into an additional layer of indirection?.. Intuitively, it shouldn't be completely impossible.
```ruby
# What I meant actually:
intended = proc { size.then(&multilpy) }
"test".instance_eval(&intended)
# => "****"
```
The example is invented, the question is "whether this should work", not "how to solve this task with another approach".
--
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>