RSS Amplifier

dhilst · Jan 30, 2019

Method caller in ruby

0
Sign in to vote or save

This page cannot be shown here. You can still read it on the original site — the toolbar below keeps your place in the directory.

Python has this by default, a function that receives a method and arguments and return a function that when called with an object call the method with the provided args. Perfect for usign with map and reduce family of fuctions. Here is how to achieve the same with ruby def methodcaller ( method , * args ) -> ( object ) { object . send ( method , * args ) } end [ 'foo' , 'boo' , 'zoo' ]. map ( &…

Python has this by default, a function that receives a method and arguments and return a function that when called with an object call the method with the provided args. Perfect for usign with map and reduce family of fuctions.

Here is how to achieve the same with ruby

def methodcaller(method, *args)
  ->(object) { object.send(method, *args) }
end
['foo', 'boo', 'zoo'].map(&methodcaller(:gsub, 'oo', 'aa'))

Cheers

Read on dhilst.github.com

Comments

Nothing yet. Say the first thing.

    Sign in to join the conversation.