From: duerst@... Date: 2017-03-15T02:57:22+00:00 Subject: [ruby-core:80168] [Ruby trunk Misc#13283][Closed] Disable `&' interpreted as argument prefix warning when passing symbol to Enumerable#map Issue #13283 has been updated by duerst (Martin D��rst). Status changed from Open to Closed First, for `&` immediately before a `:symbol` (without any space inbetween), Nobu already has removed the warning as approved by Matz, so I'm closing this issue. urbanautomaton (Simon Coffey) wrote: > 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. Yes. But `&:symbol` these days is a Ruby idiom, whereas other usages of `&` directly before symbols are few and far between. Also please note that the chance is high that passing the block (produced by &:symbol) to a method in a situation where the result of that method is intended to be `&`ed with a symbol will produce a run-time error is quite high, so even if something is unclear, it should be detected quite easily. > ~~~ > $ irb -w > > 1&2 > => 0 > > > 1 &2 > (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) > ~~~ All these warnings are still active. Only in the case that a symbol literal follows `&` immediately is the warning no longer produced. > 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 free to use as many parens as you want. But please don't force others; not having to use parens in many places is one of the important advantages of Ruby for many. ---------------------------------------- Misc #13283: Disable `&' interpreted as argument prefix warning when passing symbol to Enumerable#map https://bugs.ruby-lang.org/issues/13283#change-63607 * Author: mojavelinux (Dan Allen) * Status: Closed * 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: