First of all thank you for reading. Thanks to those who thanked me for blogging. Been very busy and hardly found time to blog.

In the past few years, Ruby's send method saved me from having to write several lines of code. Don't you ever forget about it.

Let me show you a simple example which is actually very fun because it has to do with social networks or third party services.

Goal: we want to display the logo of Twitter, Facebook and Google along with the links if the user is not authenticated (has not successfully authorized the application to access and write data to the third party service) or if the user is not a current user.

This is how we write that if we use devise and omniauth:

  - resource_class.omniauth_providers.each do |provider| 
    = link_to "Connect via #{provider.to_s.titleize}" ,  omniauth_authorize_path(resource_name, provider), 
      class: "#{provider.to_s}_login_icon clearFix" unless (current_user and current_user.send(provider.to_s))

So by now you've figured out that I have some instance methods on a module that would either return true, false or nil.

# on user.rb

include User::Authentications 

# on user/authentications.rb

module User::Authentications 

  def self.included(base)
    base.send(:include, InstanceMethods)

  module InstanceMethods

    def facebook

        @fb_user ||='facebook').token)
        return nil

   def twitter
     #some code here

   def google_apps
     #some code here 


Took out the code for Twitter and Google apps. But you should realize the benefit of the send method if you were developing something more complex and had more instance methods. Prior to this, I remember using send method for a psychology quiz application.

More on Ruby's send method:

on_class = "Post"
on_class.constantize.send("method_name", arg1)

We can use constantize to convert string to a constant as the name suggests.

comments powered byDisqus

Hello! I am Kat. I've been a developer for several years. When I'm not sitting down, reading and working for long hours, I am traveling to some place of interest.
If you want to reach out, you can send me an email or add me up on Google Plus.