[ruby-core:120015] [Ruby master Bug#20911] Array#max doesn't take block if using &:
From:
"mame (Yusuke Endoh) via ruby-core" <ruby-core@...>
Date:
2024-11-26 18:28:07 UTC
List:
ruby-core #120015
Issue #20911 has been updated by mame (Yusuke Endoh).
Status changed from Open to Rejected
`Array#max` accepts a block that compares two values.
```
p ["1", "2", "3"].max {|i, j| i.to_i <=> j.to_i } #=> "3"
```
You may want to use `Array#max_by`.
```
p ["1", "2", "3"].max_by(&:to_i) #=> "3"
```
----------------------------------------
Bug #20911: Array#max doesn't take block if using &:
https://bugs.ruby-lang.org/issues/20911#change-110755
* Author: [email protected] (Andrew Vitovitch)
* Status: Rejected
* ruby -v: ruby 3.3.5
* Backport: 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN
----------------------------------------
["1","2","3"].max { |i| i.to_i }
=> "3"
["1","2","3"].max(&:to_i)
(irb):6:in `to_i': no implicit conversion of String into Integer (TypeError)
["1","2","3"].max(&:to_i)
^^^^^^
from (irb):6:in `max'
from (irb):6:in `<main>'
from <internal:kernel>:187:in `loop'
from /Users/alvitovitch/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/irb-1.13.1/exe/irb:9:in `<top (required)>'
from /Users/alvitovitch/.asdf/installs/ruby/3.3.5/bin/irb:25:in `load'
from /Users/alvitovitch/.asdf/installs/ruby/3.3.5/bin/irb:25:in `<main>'
--
https://bugs.ruby-lang.org/
______________________________________________
ruby-core mailing list -- [email protected]
To unsubscribe send an email to [email protected]
ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/