`Send` and `Public Send` in Ruby

Patrick Wendo - Jul 18 '22 - - Dev Community

These are methods I learned of when I was learning Ruby and I never understood why they exist or where they'd be used. Regardless, they exist and I finally found out a use case this weekend.

First off, what is Send and Public Send? (According to the docs send and public send)

Send: Invokes the method identified by symbol, passing it any arguments specified. You can use send if the name send clashes with an existing method in obj. When the method is identified by a string, the string is converted to a symbol.

Public Send : Invokes the method identified by symbol, passing it any arguments specified. Unlike send, public_send calls public methods only. When the method is identified by a string, the string is converted to a symbol.

I was building a helper in rails for forms to auto-generate the input tags for specific model fields, so I'd define an array like this

cols = ["first_name", :user, :first_name, "text"]
Enter fullscreen mode Exit fullscreen mode

Where we have the tag's name, the model, the field, and the tag type in that order. This array is then sent to the view where the tags are built based on the contents of the array.

This was done to access the model in such a way that I do not have to hard code it for specific models(as is the case of associated models and such). I was using public send like this

cols[1]&.public_send(col[2])
Enter fullscreen mode Exit fullscreen mode

This will call only the public method named in cols[2] of the model named in cols[1].

I can't think of another use case for public_send and send, so if you're a ruby veteran, please let me know down below if you have used it before.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .