Swaathi Kakarla - Hi December!

Hi December!

The Ruby Configure Block

I’ve always liked the way certain Ruby gems allowed developers to configure parts of its workflow.

Airbrake.configure do |config|
  config.api_key = 'your_key_here'
end

This block just looks so clean and to the point. Almost like magic!

But magic is just a bit of zeros and ones. How’d they do it?

The answer lies in Ruby’s singleton classes. Singleton is a design pattern that allows only one one instance of the class to be globally available. It provides a single point of access to some service in your application so that you don’t have to pass around a reference to that service every time you need to use it. It is useful when you find yourself writing only class methods or when you don’t need an object of the class to function.

However fair warning, singleton is also a really hated programming pattern. A singleton is basically a global object - and everyone knows the more globals you have, the more problems you have!

A singleton class could look like this,

class Single
  def self.me
    'Forever Alone'
  end
  
  def self.me_in_the_future
    'Forever alone in the future'
  end
end

But appending a self to every method isn’t very fun. So instead do this,

class Single
  class << self
    def me
      'Forever Alone'
    end
    
    def me_in_the_future
      'Forever alone in the future'
    end
  end
end

Just call Single.me it will return Forever Alone.

However classes aren’t the only ones that can have singleton methods. A string can too!

a = "I am A."

def a.hello
  "Hello! It's me, A."
end

Now you can call a.hello. Wierd isn’t it?

Not really. What we did here is we added a singleton method hello to an object a. The difference between class methods and singleton methods is that class methods are available to all instances of a class object while singleton methods are available only to that single instance. (- via Toptal)

Great. How do we add a configure block?

Here’s the implementation,

module Idly
  class << self
    attr_accessor :configuration

    def configure
      self.configuration ||= Configuration.new
      yield(configuration)
    end
    
    def reset
      self.configuration = Configuration.new
    end
  end
end

class Configuration
  attr_accessor :chutney

  def initialize
    @chutney = 'white'
  end
end

Let’s configure our Idly! (Yes, I’m hungry.)

Idly.configure do |config|
  config.chutney = 'tomato'
end

What this allows you to do is allow your gem to be configured from the outside, without modifying the contents of the gem.

To use the configuration in your gem all you have to do is call,

Idly.configuration.chutney

If you want to reconfigure, simply call the configure block again!

That’s it! Let’s go get some piping hot idly and chutney now.

Read All 470 Words →

Sweetcode Article Send Emails with a Custom Domain Using SendGrid and Rails

SendGrid is a cloud-based email delivery service that assists businesses with email delivery. And that probably is the most boring definition ever, considering the powerhouse of tools that SendGrid provides.

With SendGrid you don’t just get to send emails, you get to monitor link clicks, un-subscription rates, geographical tracking and a lot more. In this tutorial I’ll walk you through building your own email sending service on Rails with SendGrid.

Read All 741 Words →

My TensorFlow repo is trending on Github!

It’s been a great week for the AI team at Skcript. Over the past week, we were trending top 5 in Github (Python category), for 3 days. We were liked and shared on social media endless number of times. And the best part — received great feedback from the AI/ML community.

Read All 181 Words →

Normalizing Data - Part 1 of AI Series

Co-authored by Naveen (AI Dev at Skcript).

In Machine Learning, more often than not applying an algorithm to data is not hard, rather representing the data usually is. But sometimes representing data a certain way works for one algorithm, but completely implodes for another. So with all these variations, is there one compartmentalized approach to follow?

Yes! (well, kind of and ‘kind of’ in ML is ‘good enough’)

Read All 1187 Words →

The Jallikatu Protest

The Jallikatu Protest was the worlds largest peaceful protest to protect the culture of Tamil Nadu’s legendary bull ‘hugging’ festival. Around five million people gathered at the Marina Beach to show their support in continuing the game despite discouragement by the government. There was hardly a Chennaite who didn’t show up at the beach, me included.

It was quite interesting to watch the protest seep into social media. And where there’s social media, there’s data. In abundance!

Combination of classifier and feature extractor accuracy

Read All 915 Words →