From: simon@... Date: 2017-03-14T23:55:09+00:00 Subject: [ruby-core:80166] [Ruby trunk Misc#13283] Disable `&' interpreted as argument prefix warning when passing symbol to Enumerable#map Issue #13283 has been updated by urbanautomaton (Simon Coffey). mojavelinux (Dan Allen) wrote: > The alternative, a bitwise operation on a symbol, makes little sense. That's especially when the bitwise operator is directly adjacent to the symbol. Bitwise AND isn't the only interpretation of infix binary `&`, though. Just looking in stdlib, `Set#&` means set intersection, and anyone can define `#&` on any class they like to mean whatever makes sense to them - that's the joy of ruby. What appears to be common sense in the example you gave may be completely unclear in another context. Adjacency means different things in other circumstances, too, and infix operation is totally possible with this specific whitespace configuration - the interpretation depends on the operands, and a warning is emitted whether it's interpreted as unary or binary `&`: ~~~ $ irb -w > 1&2 => 0 > 1 &2 # infix binary, warning (irb): warning: `&' after local variable or literal is interpreted as binary operator (irb): warning: even though it seems like argument prefix => 0 > x, y = 1, 2 > x &y (irb): warning: `&' after local variable or literal is interpreted as binary operator (irb): warning: even though it seems like argument prefix => 0 > def a; 1; end > b = 2 > a &b (irb): warning: `&' interpreted as argument prefix TypeError: wrong argument type Fixnum (expected Proc) ~~~ Essentially the warning is to let you know that you're depending on interpreter behaviour that can be very hard to infer by visual inspection (indeed you frequently can't know whether the LHS is a local variable or method until runtime, ironically because of optional parens). It may be just me (and I confess I'm a "use parens all the time" type), but it seems so much simpler to retain the warning and accept that there are some occasions when parens are either necessary or advisable to disambiguate the syntax. You're using them anyway in method chains, you're just putting them around (some) expressions instead of (some) argument lists. ---------------------------------------- Misc #13283: Disable `&' interpreted as argument prefix warning when passing symbol to Enumerable#map https://bugs.ruby-lang.org/issues/13283#change-63605 * Author: mojavelinux (Dan Allen) * Status: Open * Priority: Normal * Assignee: ---------------------------------------- A common idiom in Ruby is to pass a symbol reference to `Enumerable#map`, which in turn invokes the corresponding method on each entry. Case in point: ~~~ %(a b c).map &:upcase ~~~ Yet, when warnings are enabled, this line produces the following warning: ~~~ warning: `&' interpreted as argument prefix ~~~ Perhaps we can all agree that's what this statement *should* do, and in fact Ruby does it. So there's really no reason for this warning. The alternative, a bitwise operation on a symbol, makes little sense. That's especially when the bitwise operator is directly adjacent to the symbol. The workaround to squelch the warning is to add parentheses: ~~~ %(a b c).map(&:upcase) ~~~ However, it's one of the few cases in Ruby where parentheses are mandatory (for an isolated statement). For those of us who prefer to drop parentheses for code style, this is an irritation. It's also one of the most common warnings I've seen Ruby report when warnings are enabled, so it's also just noisy. Can this warning be removed? -- https://bugs.ruby-lang.org/ Unsubscribe: