[#89806] [Ruby trunk Bug#15306] Generate prelude.c using miniruby — v.ondruch@...
Issue #15306 has been reported by vo.x (Vit Ondruch).
3 messages
2018/11/15
[ruby-core:89784] [Ruby trunk Feature#15301] Symbol#call, returning method bound with arguments
From:
hanmac@...
Date:
2018-11-13 14:10:53 UTC
List:
ruby-core #89784
Issue #15301 has been updated by Hanmac (Hans Mackowiak).
I once had a similar script (> 3 years old), but i extened it to be chainable
~~~ ruby
class Symbol
class SymbolHelper
def initialize(obj,methId,*args)
@obj= obj
@args=args
@methId=methId
end
def method_missing(methId,*args)
return SymbolHelper.new(self,methId,*args)
end
def to_proc
proc {|obj| (@obj.nil? ? obj : @obj.to_proc.(obj)).public_send(@methId,*@args) }
end
end
def call(*args)
return SymbolHelper.new(nil,self,*args)
end
end
~~~
you can chain it like that
~~~ ruby
[1,2,3,4].map(&:to_s.(2)) #=> ["1", "10", "11", "100"]
[1,2,3,4].map(&:to_s.(2).length) #=> [1, 2, 2, 3]
~~~
i think that was before the use of symbol kargs
----------------------------------------
Feature #15301: Symbol#call, returning method bound with arguments
https://bugs.ruby-lang.org/issues/15301#change-74845
* Author: zverok (Victor Shepelev)
* Status: Open
* Priority: Normal
* Assignee:
* Target version:
----------------------------------------
In one Reddit discussion I've got stuck with this simple, yet seemingly powerful idea, not sure if it was discussed anytime previously (can't find on the bug tracker, but maybe I am just bad at searching):
```ruby
class Symbol
def call(*args, &block)
proc { |x| x.send(self, *args, &block) }
end
end
[10, 20, 30].map &:modulo.(3) # => [1, 2, 0]
[[1, -2], [-3, -4]].map(&:map.(&:abs)) # => [[1, 2], [3, 4]]
[1, 2, 3, 4].map &:**.(2) # => [1, 4, 9, 16]
```
I understand and respect core team's reluctance for adding new methods to core classes, but from the top of my head I can't invent _incredibly_ bad consequences (there, of course, could be some codebases that defined their own `Symbol#call` in a different way, but I don't estimate the probability as super-high; and the same could be said almost for any new method).
On the other hand, resulting code seems pretty nice, "Rubyish", explainable and mostly unambiguous.
Would like to hear other's opinions.
PS: One obvious objection could be that it is almost a de-facto standard to have any object's `#to_proc` to return proc doing exactly the same what the `#call` does (if the object happen to have both). It is unfortunate, but I believe the people will use to it, considering the possible gains. And, anyway, that's only "de-facto" rule, not the language standard :)
--
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>