From: "nobu (Nobuyoshi Nakada)" Date: 2013-08-28T13:30:46+09:00 Subject: [ruby-core:56841] [ruby-trunk - Feature #8827] A method that flips the receiver and the first argument Issue #8827 has been updated by nobu (Nobuyoshi Nakada). You can write as: some_chain_of_operations_that_ends_up_with_an_array.tap {|ary| Hash[ary]} some_chain_of_operations_that_ends_up_with_a_string.tap {|path| File.read(path)} some_chain_of_operations_that_ends_up_with_an_object.tap {|obj| YAML.dump(obj)} ---------------------------------------- Feature #8827: A method that flips the receiver and the first argument https://bugs.ruby-lang.org/issues/8827#change-41383 Author: sawa (Tsuyoshi Sawada) Status: Open Priority: Normal Assignee: Category: Target version: =begin If it often happens that we need to pass to a method an argument that is the result of a long chain of operations: Hash[some_chain_of_operations_that_ends_up_with_an_array] File.read(some_chain_of_operations_that_ends_up_with_a_string) YAML.dump(some_chain_of_operations_that_ends_up_with_an_object) ... I believe one basic tenet of Ruby is to encourage method chaining, but that practice is discouraged in the examples above. It would be convenient if there is a method (let us call this `Object#flip`) that flips the receiver and the first argument and sends the method so that the examples above can be written as follows: some_chain_of_operations_that_ends_up_with_an_array.flip(Hash, :[]) some_chain_of_operations_that_ends_up_with_a_string.flip(File, :read) some_chain_of_operations_that_ends_up_with_an_object.flip(YAML, :dump) ... The implementation in Ruby may be as follows: class Object def flip receiver, method, *rest, &pr receiver.send(method, self, *rest, &pr) end end It would be good if we can have that as a built-in Ruby method. =end -- http://bugs.ruby-lang.org/