0

Being able to call a Proc/method on multiple elements using the &: shorthand is great:

[0,4,7].map(&:to_s)            # => ["0","4", "7"]
[0,4,7].select(&:zero?)        # => [0]
[0,4,7].reduce(&:+)            # => 11
[0,4,7].count(&:nil?)          # => 0

Simply passing parameters or logical operators to the Proc doesn't work:

[0,4,7].map(&:to_s(2))         # Syntax error
[0,4,7].select(&:!zero?)       # Syntax error
[0,4,7].select(&:<5)           # Syntax error
[0,4,7].count(!&:nil?)         # Syntax error

Instead, I need to use the long-form:

[0,4,7].map { |e| e.to_s(2)}   # => ["0", "100", "111"]
[0,4,7].select { |e| !e.zero?} # => [4,7]
[0,4,7].select { |e| e < 5 }   # => [0,4]
[0,4,7].count { |e| !e.nil? }  # => 3

What other options do I have? Which ways can I modify the Proc or method I'm calling? Is there any way to pass parameters, or to use logical operators like negation (!)?

Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
Nick Beaujean
  • 58
  • 2
  • 5
  • For the record, I understand why my code produces syntax errors. &:to_s is shorthand for &:to_s.to_proc and the .to_proc method works on a Symbol representing the method, not on a method call. – Nick Beaujean Jul 05 '17 at 06:26
  • 4
    The `:` belongs to the symbol, not to the operator, i.e. it's `&` and `:to_s`, not `&:` and `to_s`. – Stefan Jul 05 '17 at 06:28
  • I think this is more a duplicate of [can-you-supply-arguments-to-the-mapmethod-syntax-in-ruby](https://stackoverflow.com/questions/23695653/can-you-supply-arguments-to-the-mapmethod-syntax-in-ruby). It's possible to patch `Symbol#call` to allow such a thing, which you can see was rejected from ruby core [here](https://bugs.ruby-lang.org/issues/12115) – max pleaner Jul 05 '17 at 06:54
  • Thanks Max, I added a note to that effect at the bottom. I have no way to contact the guy who marked the question as a dupe. Just hope he comes back and sees this, I guess – Nick Beaujean Jul 05 '17 at 06:55
  • @NickBeaujean: done! Thanks, Max. – Sergio Tulentsev Jul 05 '17 at 06:58
  • @CarySwoveland: two of them, even. :) But now that the answer is deleted, maybe no more action will be taken. I don't know if the mods issue formal warnings or just delete offending posts. – Sergio Tulentsev Jul 05 '17 at 07:09
  • @Sergio, I rarely downvote, but I'm very much old-school: cheaters should be kicked out of SO, period. – Cary Swoveland Jul 05 '17 at 07:15
  • @CarySwoveland: btw, I was looking for a cool project idea to do on weekends (which is also outside my normal expertise), and this situation gave me a hint: data project for ranking posts on their similarity (plagiarism detection, yes). Would you be interested in joining me in this? :) – Sergio Tulentsev Jul 05 '17 at 07:19
  • 1
    @Sergio, my apologies for not replying earlier--it slipped my mind. When I was younger I would have immediately said "Sure!", and then find myself burning the candles at both ends, sometimes wishing I hadn't been so hasty. With age comes wisdom. Knowing what's on my plate, I'll have to thank you for the offer, but respectfully decline (but keep the invites coming). – Cary Swoveland Jul 06 '17 at 05:45
  • @CarySwoveland: sure! :) – Sergio Tulentsev Jul 06 '17 at 05:46

0 Answers0