I'm a developer, math enthusiast and student.

Dhaivat's articles

  1. LevelDB in Ruby

    Typically when you set out with a Rails application, your data lives inside a MySQL (or PostgreSQL, or SQLite, or Oracle) database. But, it turns out that the traditional relational database is not a great fit for all types of data. For example, if you want very fast access to data that isn’t flushed to disk all that often, you could hold that information in RAM. In this case, you might want Redis or Memcached. If your data can be represented as a graph, you’ll want to check out Neo4j or OrientDB. But sometimes, you don’t want or need a full-blown database server; instead, you can make do with a simple library that can be packaged along with your app. This is where LevelDB fits in: it is a key-value storage library that’s meant to be fast.

    In this article, we’ll cover how to use LevelDB with a Ruby wrapper and also discuss where you should and shouldn’t use LevelDB by presenting common usecases.

  2. Integrating VictorOps with Salesforce Using its REST Endpoint

    This article was sponsored by VictorOps. Thank you for supporting the sponsors who make SitePoint possible!

    When you have a live application with real users, problems don’t show up according to your schedule. You need a way to manage and resolve issues that come up all of a sudden, and this process needs to be as simple and painless as possible. This is where VictorOps comes in. It allows you to manage who is responsible for fixing live problems, enables communication among the team when these issues arise, and integrates with the rest of your systems to make detecting problems a piece of cake.

    In this article, we’ll cover how you can use VictorOps as a REST endpoint to integrate it with other services. In particular, we’ll focus on SalesForce integration. To make this integration, you’ll need a VictorOps account.

    Integration and the REST Endpoint

    First of all, what do we mean when we say that VictorOps gives you a “REST endpoint”? The idea is that VictorOps allows you to access resources using a RESTful API over HTTP. What’s the point, you ask? Well, it is usually very easy to make HTTP requests, and we can use the REST endpoint to quickly set up integrations with other services. For example, a lot of companies use Nagios to monitor services within a deployment. We can integrate VictorOps with Nagios using a plugin, which simply takes alerts and pushes them over to VictorOps’s REST endpoint. The breadth of services you can integrate using the REST endpoint is incredible; as long as you have a little code that can fire off some HTTP requests to ‘alert.victorops.com’, you can make the integration happen.

    Let’s take a look at how to set up one of these integrations. A lot of teams use Salesforce as a communications medium. We want people who use Salesforce (which generally includes people on the non-technical side of things) to be able to communicate with the people working with their company’s servers. So, when cases are created in Salesforce, we should get alerts through VictorOps. This allows you to have one place (VictorOps) where you can see IT problems arise throughout your infrastructure. Let’s see how to set up that integration.

    Salesforce Integration

    Salesforce has a way to hook in little bits of code that decide what to do when specific things called “triggers” happen. These little bits of code are written in a language called “Apex” (which is more or less specific to Salesforce) that is meant to have a syntax very similar to Java. Fortunately, you don’t have to spend any of your time learning how to write Apex in order to get Salesforce integration to work since the kind folks at VictorOps have a repository that contains the required code snippets.

    For security reasons, we need to tell Salesforce that our code is going to be making requests to the VictorOps REST endpoint. To do that, we’ll have to add “http://alert.victorops.com/” as a remote site by going through ‘Security Controls => Remote Site Settings => New Remote Site’ in the Salesforce dashboard. After you do that, you should see this on the ‘All Remote Sites’ page:

    All Remote Sites page in Salesforce

    Before we can get the Apex code talking with VictorOps, we need to put in our API and route keys from VictorOps into the Apex code. Get yourself of a copy of the code first from this repository. Then, we need the right keys from VictorOps. From the VictorOps dashboard, we go to ‘Settings => Integrations REST Endpoint’. After enabling the integration, you’ll see a URL like this:

    API Key URL in VictorOps

    The part of the URL in red is your “API key”. Now, we’ll create a routing key. Your routing key allows you to route certain alerts to certain groups of people within your VictorOps organization. To set one up, again head over to ‘Settings’ in the VictorOps dashboard, click on ‘Integrations’ and down to ‘Incident Routing’. Add a rule with a routing key (e.g. “salesforce_integration”). At this point, you’ll probably be routing that routing key to ‘everyone’:

    Routing Key in VictorOps

  3. Heap Data Structure in Ruby

    The study of data structures and algorithms does not have a huge following in the Ruby community because we often ask “what’s the point?” In practice, we rarely have to implement classicial structures such as stacks and queues. But, by learning about them, we can improve our ability to reason about code. More importantly, data structures are very interesting to study!

    In this article, we’ll cover the “heap” data structure along with some of the associated algorithms. I’ll assume that you have a basic handle on asymptotic notation and Ruby.

  4. Sorting Algorithms in Ruby

    The point of understanding sorting algorithms has very little to do with the actual act of sorting. Rather, the different algorithms are great examples of various techniques that can be applied to large set of problems. In this article, we’ll take a look at some of these algorithms and the underlying ideas, as well as implementations in Ruby.

  5. Machine Learning: Ruby and the Naive Bayes Theorem

    We’ve all heard of the “Big Data” trend in computer science: everyone and their grandmother is trying to garner insights from large datasets. Often, the tool for the job is machine learning. A large portion of the Ruby community revolves around web development; even here, some basic machine learning knowledge can come in handy when applied creatively. This article will get you up to speed with a simple (but often quite effective) machine learning technique: the Naive Bayes Classifier.

    We’ll go through the underlying theory, which does require some math background but not too much. Then, we’ll build a reusable implementation of the classifier and work it through an example dataset.

  6. Dynamic Programming with Ruby

    This article will give you a taste of a common algorithm design technique called “dynamic programming” by exploring two different problems and presenting solutions in Ruby. It turns out that dynamic programming is actually a very versatile approach that can be applied to all sorts of problems in a ton of disciplines.

  7. Drawing with Processing and Ruby

    Processing is an environment/programming language that is meant to make visual, interactive applications extremely easy to write. It can be used for everything from teaching children how to code to visualizing scientific data. Fortunately, all of this goodness is no longer bottled up inside Processing anymore; you can use it from Ruby! In this article, […]

  8. Choosing the Right Serialization Format

    When saving or communicating some kind of information, we often use serialization. Serialization takes a Ruby object and converts it into a string of bytes and vice versa. For example, if you have an object representing information about a user and need to send it over the network, it has to be serialized into a set of bytes that can be pushed over a socket. Then, at the other end, the receiver has to unserialize the object, converting it back into something that Ruby (or another language) can understand.

    It turns out that there are lots of ways to serialize Ruby objects. I’ll cover YAML, JSON, and MessagePack in this article, exploring their pianos and fortes to see them in action with Ruby. At the end, we’ll put together a modular serialization approach using some metaprogramming tricks.

  9. Elegant Network Communication with RabbitMQ

    Network communication is fundamental to tons of applications, but it is very difficult to get right. Dealing with traditional sockets (TCP, UDP, etc.) is frustrating because the concepts associated with them are too low level for a lot of the work that we do. For example, sockets are operated as streams, meaning that you have come up with your own way to distinguish between packets of information.

    This is one of the gaps that messaging systems fill. They make writing networked applications much easier by providing message-based, rather than stream-based, communication. That means instead of having to read from a seemingly never-ending stream of information, you get your information nicely packaged as messages. In addition, messaging systems allow us to easily design complex network topologies with “queues” in the middle (more on that later).

    In this article, we’ll explore RabbitMQ. We’ll take a look at its underlying concepts and how it compares to other message queuing systems. Also, the article will cover how to use “Bunny”, the gem that lets Ruby developers interact with RabbitMQ.

  10. Agent: Go-Like Concurrency in Ruby


    Concurrency and parallelism are hard. With the traditional threading model, a developer has to constantly ensure threads are not in conflict, which is a very difficult task. The team behind Go implemented fantastic concurrency primitives within the language that make reasoning about concurrent code far easier.

    Fortunately, all that goodness is no longer locked up inside Go; there’s now a library for Ruby! It’s called “Agent” and it implements the Golang-concurrency magic in Ruby.

  11. Go for Rubyists, II

    In this article, we’ll be looking through some more of Go’s features through a Ruby lens. In case you missed it, you can find Part I here, although you don’t have to read it to understand this article. We’ll be running through Go’s answer to traditional OOP strategies (e.g. inheritance). Go essentially eliminates traditional inheritance […]

  12. Go for Rubyists

    The great folks over at Google have developed an awesome language called Go. At first glance, it seems like Ruby and Go are distant cousins, at best. However, their complementary skill sets provide a perfect match. It is definitely worth any Rubyist’s time to take a look at Go as some of the innovations it […]

  13. Introduction to Thor

    Looking at RubyGem’s list of “Most Downloaded” gems, we notice lots of familiar names: “rake”, “rails”, “rack”, “actionmailer”, etc. But, “thor” is an exception; few of us have heard of it and even fewer have written code with it. So, what exactly is Thor? It is a way to write powerful command line utilities in […]

  14. Comparing Background Processing Libraries: Sidekiq

    This entry is part 3 of 3 in the series Comparing Ruby Background Processing Libraries

    This entry is part 3 of 3 in the series Comparing Ruby Background Processing Libraries This is the final part in the background processing series. You can check out the previous posts from the links above. I’ll summarize what we have covered so far. The principles behind background processing (queues, processes etc.). Using delayed_job, which […]

  15. Comparing Background Processing Libraries: Resque

    This entry is part 2 of 3 in the series Comparing Ruby Background Processing Libraries

    This entry is part 2 of 3 in the series Comparing Ruby Background Processing Libraries This is part II of the series on comparing Ruby background processing frameworks. You can check out the previous post linked above. Summing up the first post, it covers the principles behind background processing (queues, processes, etc.). Also, the article […]

  16. Comparing Ruby Background Processing Libraries: Delayed Job

    This entry is part 1 of 3 in the series Comparing Ruby Background Processing Libraries

    This entry is part 1 of 3 in the series Comparing Ruby Background Processing Libraries Once you start working on Rails applications beyond the typical “tutorial style” blog, you will probably need some kind of processing outside of just responding to user requests. For example, if you need to send emails to some of your […]

  17. Build a Port Scanner in Ruby

    Sometimes, we box ourselves up in our HTTP-only world. Sometimes, Rails eclipses all other facets of Ruby. In this article, we’ll build a simple port scanner in pure Ruby, using the sockets support that comes with the Ruby distribution. Port scanners aren’t typically things we expect Rubyists create; that’s the kind of stuff that the […]

  18. Docker for Rubyists

    Docker is a pretty awesome tool and one that I think will have a pretty big impact on how we do development. But, if you haven’t had much experience with Linux containers (or virtual machines), Docker’s underlying ideas as well as its benefits may be a little difficult to understand. This article will give you an […]

  19. Arduino and Ruby: Create a Gmail Notifier

    In this article, we’ll build a complete system that allows you to blink an LED when you get email or a ton of other such events. To run the LED, we’ll use the incredibly popular Arduino platform, and to get data from Gmail, we’ll use the all-mighty Ruby along with a computer. Let’s dive right […]

  20. Editors for Rubyists

    For any growing language, there’s always a ton of tools you can use, as well as very strong opinions about them. Ruby is no exception. There’s several editors, IDEs, etc. that all have excellent Ruby support aimed at keeping you operating at maximum “output” (I’m not one of those crazy managers that measures this output […]

  21. What Makes Ruby Tick

    When I first started working with computers, one thing really amazed me. You can literally print out the code for something like the Morris worm (a malicious bit of code that wreaked all sorts of havoc) and it was quite harmless. But, when fed to the computer in the “proper way” it suddenly turned into […]