Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #7392

reduce, map, :+ and :upcase as blocks and symbols as arguments

From Fernando Basso <fernanodobasso.br@gmail.com>
Newsgroups comp.lang.ruby
Subject reduce, map, :+ and :upcase as blocks and symbols as arguments
Date 2018-06-28 06:25 -0300
Organization Netfront http://www.netfront.net/
Message-ID <ph29i8$1alc$1@adenine.netfront.net> (permalink)

Show all headers | View raw


Why does `reduce' work passing both `&:+' _and_ `:+', but `map' seems to
accept only `&:upcase' (but not `:upcase`)?

>> (1..5).reduce(:+)
=> 15
>> (1..5).reduce(&:+)
=> 15
>> ['x', 'y', 'z'].map(:upcase)
ArgumentError: wrong number of arguments (given 1, expected 0)
from (pry):3:in `map'
>> ['x', 'y', 'z'].map(&:upcase)
=> ["X", "Y", "Z"]

Docs for `Enumerable#reduce' say:

""
Combines all elements of enum by applying a binary
operation, specified by a block or a symbol that names a
method or operator.
""

Docs `Enumerable#map' say:

""
Returns a new array with the results of running block once
for every element in enum.
""

So, is that `reduce' takes both a block and a symbol, while map takes
only a block? And if so, is that some Ruby inconsistency?

Back to comp.lang.ruby | Previous | NextNext in thread | Find similar


Thread

reduce, map, :+ and :upcase as blocks and symbols as arguments Fernando Basso <fernanodobasso.br@gmail.com> - 2018-06-28 06:25 -0300
  Re: reduce, map, :+ and :upcase as blocks and symbols as arguments Robert Klemme <shortcutter@googlemail.com> - 2018-07-05 08:23 +0200
    Re: reduce, map, :+ and :upcase as blocks and symbols as arguments Fernando Basso <fernanodobasso.br@gmail.com> - 2018-07-05 07:01 -0300

csiph-web