From: "Azuma-01 (Azuma Eleth)" Date: 2021-11-08T18:13:26+00:00 Subject: [ruby-core:105971] [Ruby master Feature#12125] Proposal: Shorthand operator for Object#method Issue #12125 has been updated by Azuma-01 (Azuma Eleth). Hello, I cannot help myself to see than all proposal are of the form: ` (expression) (shorthand_operator) (method_name)` while I had seen some interesting option, to me, that format seem to add an obsure behaviour rater than a shorthand. Personaly, when i mix the words 'Ruby' and 'shorthand', i see %string %i, %q, %r, %s, %w, %x Why not adding %m to the party? something like ` %m{(expression)(space)(method_name)}` That should not be to hard to idantify and translate to ` (expression).method(:method_name)` If there is no expression, we assume the value self ``` ruby %m #=> # %m(foo.bar.baz something) #=> # %m:to_s: #=> # [1, 4, 9, 16, 25].map(& %m"Math sqrt") #=> [1.0, 2.0, 3.0, 4.0, 5.0] ``` To enable meta-programming, it can be done the same way than all others %string does it... with an upper case version. ``` ruby x = ::Kernel.gets.chomp %M[MyModule #{x}] ``` ---------------------------------------- Feature #12125: Proposal: Shorthand operator for Object#method https://bugs.ruby-lang.org/issues/12125#change-94518 * Author: Papierkorb (Stefan Merettig) * Status: Open * Priority: Normal ---------------------------------------- Hello, The `&` operator lets one pass a `#call`-able object as block. Really useful feature, but at the moment, if you want to pass a `Method` this way the syntax is not really concise: ```ruby Dir["*/*.c"].map(&File.method(:basename)) ``` More often than not, at least I end up writing this instead `.map{|a| File.basename a}` which isn't that great either. Thus, I want to propose adding a short-hand operator to the ruby language, which simply calls `#method` on an `Object`. It could look like this: `an_object->the_method` which is 100% equivalent to doing `an_object.method(:the_method)` I'm reusing the `->` operator which is already used for the stabby lambda. But I think it makes sense: You have an object, and from that object you point at a method to get it as `Method`. With this, the example from above becomes: `Dir["*/*.c"].map(&File->basename)` I attached a proof of concept patch. When you apply this to trunk, you can try the example above yourself. Do note however that this PoC also breaks stabby lambda for the moment. I'll work on fixing that the following days. Thank you for reading, Stefan. ---Files-------------------------------- method_shorthand.diff (740 Bytes) dot-symbol.patch (554 Bytes) -- https://bugs.ruby-lang.org/ Unsubscribe: