[ruby-core:96324] [Ruby master Misc#16260] Symbol#to_proc behaves like lambda, but doesn't aknowledge it

[email protected]
Newsgroups gmane.comp.lang.ruby.core
Message-ID <redmine.journal-83236.20191219032652.eac0468d32b8742c@ruby-lang.org>
Issue #16260 has been updated by mame (Yusuke Endoh).


Just curious: How do you want to use the result of `lambda?`?  Even if it returns true, we may pass an arbitrary number of arguments: `lambda {|*a| ... }`.  I think that `lambda?` is useless except debugging.

----------------------------------------
Misc #16260: Symbol#to_proc behaves like lambda, but doesn't aknowledge it
https://bugs.ruby-lang.org/issues/16260#change-83236

* Author: zverok (Victor Shepelev)
* Status: Rejected
* Priority: Normal
* Assignee: 
----------------------------------------
Seems that `Symbol#to_proc` returns `Proc` that has lambda semantics:
```ruby
proc = :+.to_proc
proc.call(1, 2)   # => 3
proc.call([1, 2]) # ArgumentError (wrong number of arguments (given 0, expected 1))
```
But if you ask...
```ruby
proc.lambda? # => false
```

That seems to be an inconsistency, which I'd like to clarify. There are obviously two ways to fix it:
1. Make it respond `true` to `lambda?` (and mention the semantics in docs)
2. Make it behave like non-lambda.

The second one seems to produce some useful behavior:
```ruby
# Currently:
[1, 2].zip([3, 4]).map(&:+) # ArgumentError (wrong number of arguments (given 0, expected 1))

# With non-lambda:
class Symbol
  def to_proc
    proc { |o, *a| o.send(self, *a) }
  end
end

[1, 2].zip([3, 4]).map(&:+) # => [4, 6] 
```

Probably all of it was discussed when `Symbol#to_proc` was introduced, but as old NEWS-files doesn't link to tickets/discussions, I can't find the reasoning for current behavior.



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:[email protected]?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.