Module 27 Β· Metaprogramming & Rails Magic β Lesson 2 of 6 Β· ~8 min
send & public_send
Call a method by its name-as-data
send calls a method whose name you have as a symbol or string: booking.send(:confirm!) is the same as booking.confirm!. The power is that the name can be computed β from user input, a config value, a database column. This is how a generic controller can call record.public_send(params[:action]) or how a serializer reads every attribute by name.
In an interview, say: "
sendinvokes a method from its name as data β essential when the method to call is determined at runtime. I default topublic_sendso I don't accidentally reach past an object's public interface."