Ways to call a method in Ruby
#code #summary #method #calls #ruby
This is a code summary for https://www.notonlycode.org/12-ways-to-call-a-method-in-ruby/


![# Part 3️⃣/4: Other ways to call a method # with __send__ service.__send__(:post, "Extra #1!") # Using [] shorthand syntax for call service.method(:post)["Extra #2!"] # With lambda service.tap(&->(object) { object.post("Extra #3!")}) # or post_lambda=->(service) { service.post("Extra #4!")} service.tap(&post_lambda) # With proc service.tap(&(Proc.new { _1.post("Extra #5!")})) # or post_proc=Proc.new { _1.post("Extra #6!")} service.tap(&post_proc) # It works with `then` too if you want to get the result also # `tap` will discard the result result=service.then { _1.post("Extra #7!") }](https://archive-assets.shortruby.com/assets/issues/e3b7db6317f55bf740f1c30da1773b52d66464c684dad9cde5d38235f3c7806b.png)
![# Part 4️⃣/4: using bind and binding method=TweetService.instance_method(:post) method.bind(service).call("Extra #8!") # with bind, you can change the context where this executes class ClientOauthV2 def send(message) = puts "[OauthV2] #{message}" end class TweetServiceV2 < TweetService def initialize(client = ClientOauthV2.new) = super end new_service=TweetServiceV2.new method.bind(new_service).call("Extra #9!") # You can also get binding and then eval in that context class TweetServiceV2 < TweetService def get_binding=binding end context=new_service.get_binding context.eval("post('Extra #10!')") # or eval("post('Extra #11!')", context)](https://archive-assets.shortruby.com/assets/issues/859fdfeed2b5c282314bc4172dddf3d8a488d215efd9c543e4f311860889b2f2.png)
The source code if you want to play with this is here.
#code #summary #method #calls #ruby
This is a code summary for https://www.notonlycode.org/12-ways-to-call-a-method-in-ruby/


![# Part 3️⃣/4: Other ways to call a method # with __send__ service.__send__(:post, "Extra #1!") # Using [] shorthand syntax for call service.method(:post)["Extra #2!"] # With lambda service.tap(&->(object) { object.post("Extra #3!")}) # or post_lambda=->(service) { service.post("Extra #4!")} service.tap(&post_lambda) # With proc service.tap(&(Proc.new { _1.post("Extra #5!")})) # or post_proc=Proc.new { _1.post("Extra #6!")} service.tap(&post_proc) # It works with `then` too if you want to get the result also # `tap` will discard the result result=service.then { _1.post("Extra #7!") }](https://archive-assets.shortruby.com/assets/issues/e3b7db6317f55bf740f1c30da1773b52d66464c684dad9cde5d38235f3c7806b.png)
![# Part 4️⃣/4: using bind and binding method=TweetService.instance_method(:post) method.bind(service).call("Extra #8!") # with bind, you can change the context where this executes class ClientOauthV2 def send(message) = puts "[OauthV2] #{message}" end class TweetServiceV2 < TweetService def initialize(client = ClientOauthV2.new) = super end new_service=TweetServiceV2.new method.bind(new_service).call("Extra #9!") # You can also get binding and then eval in that context class TweetServiceV2 < TweetService def get_binding=binding end context=new_service.get_binding context.eval("post('Extra #10!')") # or eval("post('Extra #11!')", context)](https://archive-assets.shortruby.com/assets/issues/859fdfeed2b5c282314bc4172dddf3d8a488d215efd9c543e4f311860889b2f2.png)
The source code if you want to play with this is here.