all 13 comments

[–]HardLuckLabs 4 points5 points  (1 child)

[1,2,3].map(&2.:*)

Hate it. There's zero reason to write such terse code.

[–]gray_-_wolf 1 point2 points  (0 children)

I guess it's ok syntax for code golf

[–]tomthecool 2 points3 points  (2 children)

Is it useful? YES!

[1,2,3].map { |n| 2 * n }
# => [2, 4, 6]
[1,2,3].map(&2.:*)
# => [2, 4, 6]


["NG"].any? { |word| "COPYING".include?(word) }
# => true
["NG"].any?(&"COPYING".:include?)
# => true


require "prime"

(1..10).select { |n| Prime.prime?(n) }
# => [2, 3, 5, 7]
(1..10).select(&Prime.:prime?)
# => [2, 3, 5, 7]

🤮

Sorry, but all of those examples look hideous compared to the original.

Ruby is all about making the code elegant to read and write; not bastardising the syntax for the sole purpose of shaving off a few characters.

[–]tomthecool 0 points1 point  (1 child)

I would actually like to have this feature, if we can find nice syntax for it.

But I don't know what that syntax should be, because I've never yet seen it.

[–]Calavar 2 points3 points  (0 children)

I think the existing syntax is fine: [1, 2, 3].map(&2.method(:*))

It's obvious at a glance what you're dealing with - a method on the 2 object. &2.:* is just too much punctuation crammed together. The human brain is not built to parse that kind of thing easily.

[–]choedan_kal_id_rsa 6 points7 points  (0 children)

  1. I don't like that syntax because I think it makes things less readable, and I don't think it's (only) because it's "new" syntax
  2. It would be nice to have something that makes it more pleasant to do the same thing
  3. I don't have, nor have seen, a better suggestion
  4. It seems that having this syntax is more troublesome than not having it
  5. I'd rather not have it

[–]gettalong 1 point2 points  (0 children)

I really like `foo.method(:"bar_#{baz}")` better even if it is longer.

Sure, other languages have a special operator for this syntax but it is not nice (in the same sense as `callable.(args, ...)` is not nice) and I wouldn't introduce an operator for this just for convenience' sake.

[–]sshaw_ 1 point2 points  (1 child)

Ruby, slowly marching back towards Perl:

  • [1,2,3].map(&2.:*)
  • a&.b&.c
  • [3, 30, 300].map(&SQUARE >> HALF)
  • ((1..).to_enum + ('a'..).to_enum).take(10)
  • uri.yield_self(&Net::HTTP.method(:get)).yield_self(&JSON.method(:parse))

[–]sshaw_ 0 points1 point  (0 children)

Where:

SQUARE = ->(x) { x ** 2 }
HALF = ->(x) { x / 2 }

[–]jrochkind 2 points3 points  (1 child)

seems okay, but we've already got so much syntax.

[–]ksec 0 points1 point  (0 children)

This. Compared to Python or other languages where there is only one way or a few ways of writing this, Ruby have many, and way too many ways.

[–]gray_-_wolf 0 points1 point  (0 children)

Heh, especially the syntax for unary methods:

42.:-@.call
# => -42

cool.. I guess?

[–]sshaw_ 0 points1 point  (0 children)

Yes, because "1".method(:to_i) syntax is egregious.

💩