Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.ruby > #7393
| Path | csiph.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail |
|---|---|
| From | Robert Klemme <shortcutter@googlemail.com> |
| Newsgroups | comp.lang.ruby |
| Subject | Re: reduce, map, :+ and :upcase as blocks and symbols as arguments |
| Date | Thu, 5 Jul 2018 08:23:33 +0200 |
| Lines | 50 |
| Message-ID | <fq5rr6Ff076U1@mid.individual.net> (permalink) |
| References | <ph29i8$1alc$1@adenine.netfront.net> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=utf-8; format=flowed |
| Content-Transfer-Encoding | 7bit |
| X-Trace | individual.net v4mxnuF6+ely/I39jpooiQzQzT1hVR0NsiZoz36B23Jlox77g= |
| Cancel-Lock | sha1:N7tjY0Ip8QmFaqiW6d5hLqRsAkg= |
| User-Agent | Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.8.0 |
| In-Reply-To | <ph29i8$1alc$1@adenine.netfront.net> |
| Content-Language | en-US |
| Xref | csiph.com comp.lang.ruby:7393 |
Show key headers only | View raw
On 28.06.2018 11:25, Fernando Basso wrote: > 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? Yes. > And if so, is that some Ruby inconsistency? Yes. Actually I am surprised that #reduce / #inject accepts a single Symbol. But these methods have some subtleties in argument handling anyway, so maybe I should not be surprised. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/
Back to comp.lang.ruby | Previous | Next — Previous in thread | Next in thread | Find similar
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