0

I am trying to use "reduce" to select the largest number in an array, and the specific logical judgment is written in a block. If I wrap the block in curly brackets, I get the expected result. If I wrap the block with "do..end", I get a TypeError (0 is not a symbol nor a string).

Here is the code:

p [5, 2, 9, 10, 7, 89, 70].reduce(0) { |acc, cv|
  if cv > acc   
    acc = cv    
  else
    acc                
  end
}
TPR
  • 101
  • 6
  • 1
    This is a duplicate of [Ruby Block Syntax Error](http://StackOverflow.Com/q/6854283/2988), [Code block passed to `each` works with brackets but not with `do`-`end` (ruby)](http://StackOverflow.Com/q/6718340/2988), [Block definition - difference between braces and `do`-`end` ?](http://StackOverflow.Com/q/6179442/2988), [Ruby multiline block without `do` `end`](http://StackOverflow.Com/q/3680097/2988), [Using `do` block vs brackets `{}`](http://StackOverflow.Com/q/2122380/2988), [What is the difference or value of these block coding styles in Ruby?](http://StackOverflow.Com/q/533008/2988), … – Jörg W Mittag Feb 06 '22 at 14:32
  • 1
    … [Ruby block and unparenthesized arguments](http://StackOverflow.Com/q/420147/2988), [Why aren't `do`/`end` and `{}` always equivalent?](http://StackOverflow.Com/q/7487664/2988), [Wierd imperfection in Ruby blocks](http://StackOverflow.Com/q/7620804/2988), [Passing block into a method - Ruby](http://StackOverflow.Com/q/10909496/2988), [`instance_eval` block not supplied?](http://StackOverflow.Com/q/12175788/2988), [block syntax difference causes “`LocalJumpError: no block given (yield)`”](http://StackOverflow.Com/q/18623447/2988), … – Jörg W Mittag Feb 06 '22 at 14:32
  • 1
    … [`instance_eval` does not work with `do`/`end` block, only with `{}`-blocks](http://StackOverflow.Com/q/21042867/2988), [`Proc` throws error when used with `do` `end`](http://StackOverflow.Com/q/25217274/2988), [Block not called in Ruby](http://StackOverflow.Com/q/29454056/2988), [Different behaviour of “`do … end`” and “`{ … }`” block in ruby](http://StackOverflow.Com/q/37638152/2988), [Ruby syntax for passing block](https://stackoverflow.com/q/61749537/2988), and [Difference between do..end and {} while using block](https://stackoverflow.com/q/68367908/2988). – Jörg W Mittag Feb 06 '22 at 14:33
  • Note that this can be expressed much simpler: `p [5, 2, 9, 10, 7, 89, 70].max` – Jörg W Mittag Feb 06 '22 at 14:36

1 Answers1

1

The Ruby parser does not understand your code if you don't supply the parenthesis for the p method.

This version works:

p([5, 2, 9, 10, 7, 89, 70].reduce(0) do |acc, cv|
  if cv > acc   
    acc = cv    
  else
    acc                
  end
end)
bjelli
  • 9,752
  • 4
  • 35
  • 50
  • Also worth noting that `acc = cv` is completely pointless as the return value of the block is passed along as `acc` on the next iteration. That whole condition can be expressed as `[acc, cv].max`. – Chris Feb 06 '22 at 20:09
  • Is there a theoretical reason why only parentheses can be used here? – TPR Apr 13 '22 at 09:41