Sameer Siruguri

My Blog

Archive for the category “learning”

Javscript Regex Cheatsheet

I always forget how to do this:

r = /^[^0-9]+/

string = '1a'

me = 'b2'

/* this returns false */


/* this returns true */


then how about capture groups – you can’t iterate over them in one go, that’s the problem, you have to do it via exec() which you run multiple times on the same str variable, weirdly enough. The return value is an array with always just two elements: the entire match and the capture group match.

let str = '<h1>Hello, world!</h1> <h1>Hello1, world!</h1> <h1>Hello3, world!</h1>';
 let reg = /<h1>(.*?)<\/h1>/g;
 let match;
 while (match = reg.exec(str)) {

voila – you get first the 2 elements, “<h1>Hello, world!</h1>” and “Hello, world!” and then the two elements, “<h1>Hello1, world!</h1>” and “Hello1, world!” and so on

A React and Node Cheatsheet

Oh dear God, I have decided to try and learn me some React now. I am starting with this tutorial – – which is itself a year and half old, so who knows if I’ll manage it. At any rate, it’s worth writing down some basic concepts.

Read more…

Docker Compose Steps

Quick note to remind myself how to re-run my Docker containers:

  1. Is my Docker Compose part of a network? Make sure all required services are running elsewhere.
  2. Did I change anything in the build steps? (E.g., with Rails, did I change the Gemfile?) – run docker-compose build --no-cache
  3. Clean things up: Run an rm step that can remove exited containers.

Read more…

Books for Beginner Coders

Someone emailed me and asked what good books to read for beginning-level coders.

I usually recommend that if you are starting to learn how to code, you should first extend your research by asking the same question (“What’s a good beginners’ book for…?”) for a list of popular programming languages – here’s a highly selective list I got Read more…

Android Fundamentals for Programmers

This post documents some fundamentals of Android programming, for someone who’s already been programming in other languages. Here are things you should know:

Rage and Rant

The Android layout XML markup basically re-invents CSS. Fuck you, Google. Why can’t you have just extended open standards, and made it easier on all of us? “Do the Right Thing,” my sainted ass.

XML attribute to make your view be as big as its parent? match_parent. Heard of divs, Google? What’s wrong with fucking HTML divs?

Bah. Anyway, smoke on that a bit first, and now let’s go on.


  • Activity is a class in the Android SDK. An activity is responsible for managing user interaction with a screen of information.
  • Every widget is an instance of the View class or one of its subclasses (such as TextView or Button.)
  • ViewGroup is a View subclass that contains and arranges other widgets.
    • Other ViewGroup subclasses are FrameLayout, TableLayout, and RelativeLayout.
    • FrameLayout is the simplest ViewGroup and does not arrange its children in any particular manner. The arrangement will use the android:layout_gravity attribute, instead of the ViewGroup’s pre-determined layout.
  • Sometimes, you want your widgets to have “resource IDs.” To generate a resource ID for a widget, you include an android:id attribute in the widget’s definition.
  • There is a + sign in the values for attributes that you are creating and not merely referencing.
  • Some class names are food-themed. Why?


  • Everything on screen is a “view”; that is, a View. So to create an interaction, you have to get the view via its ID (that you set in a corresponding resource XML file), and then call a method on the appropriately-casted View object that sets an event listener. This means you have to learn a whole new vocabulary of event listener methods, instead of finding pleasantly that the methods are named the same as, oh I don’t know, some library that already implements event-based actions for a visual interface, now I wonder where one could find such a language…

The View

  • Pulling things into the view involves specifying them in your content_*.xml file. These things are pulled in either as strings (android:text=”@string/…) or images (android:drawableLeft=”@drawable/…) I guess there are other possible “drawable” values you can set to put your images in different parts of a given widget. At least one of them is drawableEnd, which I am guessing intelligently moves the right-most image object to the left-most spot, for right-to-left screens.
  • The type of view used depends on the device configuration and other factors. The resources corresponding to each of these is in its own, appropriately-named, folder. There is a list of configuration qualifiers that Android recognizes and the pieces of the device configuration that they refer to.


  • The Log.d(String, String, Throwable) signature logs the entire stack trace at the point the Log.d function was called, if the third argument is simply a “blank” instance of Exception (i.e., new Exception())
  • One can debug using “exception breakpoints” which is nice – and is in fact a feature of Byebug too, one I wasn’t aware of.


I’ll say this about statically-typed languages, though – it’s nice to have an IDE that can do fairly complex code analysis. Knowing that static fields can be converted to local variables? Gold.

Some of the IDE actions could equally be done in dynamically interpreted languages – finding the library that defines a symbol name, say, or knowing that a defined variable has never been used (which is in fact something Perl will tell you in warnings mode, so it’s a pity other languages aren’t as good about it. (I’m looking at you, Ruby.))


  • Oh yay. Java won’t apply the modulus operator to negative numbers to return a positive value. Which is exactly how none of the dynamically interpreted languages do it. Fun.

The Emulator

  • To rotate, hit Fn-Ctrl-F12


Some of these resources will probably come in handy later:



In our previous post on this topic, we started exploring some basic text analysis techniques. We looked at term counts or term frequencies as a measure to help automatically generate tags. The list of words by term frequency is also referred to as a “word cloud” sometimes, especially when the ‘cloud’ is visualized by arranging the words in a circular shape, and having more frequent words displayed in larger text.

In the first cut at a tagging algorithm, we probably started to see a few good candidates, but the first problem we would have run into is that one of these words, or something similar, is the most common: ‘a’, ‘an’, ‘the’, ‘of’, ‘that’, etc.

Read more…


In this post, we will go over some basic theory behind statistical analysis of text. This theory encompasses some of the ideas used in a lot of modern big data analytics and is surprisingly relevant and useful even today, over forty years after it was invented.

Let’s start with a simple question – what is any document about? Alternatively, we can pose this question as, if we had a document that needed to be tagged, how can we do that automatically, that is, write a program to do it? Read more…

Some Common Tricks with Ruby on Rails

Today we will use Ruby on Rails along with some Javascript libraries to code a few common patterns –

  • Autocomplete in a text box
  • Uploading files by dragging and dropping them into an area inside a browser.


The Javascript library, jQuery.token-input, is great for quickly building a Javascript-based autocomplete solution. You have to do only three things to get started: Read more…

Setting Up A New Ubuntu Box: My Personal Recipe

Here are all the things I like to do when I am setting up a new Ubuntu box (works on Ubuntu 14.04 x64, YMMV).

This first set of steps is best executed as the root user on the system.

    1. Update your Debian repositories: apt-get update
    2. Install Emacs (this is my personal favorite editor) – apt-get install emacs24
    3. Set up Apache2 with
      apt-get install apache2
      a2enmod userdir

      This also installs ufw.

    4. Secure Apache – make sure to add the UserDir directive. The above module will allow it. Restart Apache.
  1. Create a www-data user, and a home folder (/var/www). Give ownership of the home folder to this user, use bash as the login shell, and set up a password. Set up SSH for this user, enable them to login to the home directory, give them sudo privileges.
    usermod -s /bin/bash -d /var/www www-data
    usermod -G sudo www-data
  2. Enable ufw to only allow ports 22, 80, 443 – additionally, enable 220 and change the SSHD configuration to listen at port 220 instead of 22 by editing (more security tips at the OpenSSH configuration page):
    apt-get install vim
    vim /etc/ssh/sshd_config
    service ssh restart
  3. Apache will need the following modules enabled:
    1. a2enmod ssl
    2. a2enmod rewrite
  4. You need to be able to compile new packages – you need apt-get install build-essential
  5. Some useful packages don’t come pre-installed:
    apt-get install zip sendmail
  6. Sendmail: Configuring this is weird. You need to do a bunch of stuff to enable TLS and SASL, and then configure /etc/hosts to have an FQDN.
  7. Install some goodies for Rails – namely, Ruby, Ruby Gems, QT, Phusion Passenger and Postgres. See more details below.

Read more…

Programming: The One True Way

There are any number of people trying to advise people on the One True Way to program. Recently, a thread on Reddit quoted someone on 4chan who had their own opinion on this, an opinion that I thought was beyond ridiculous. The Redditor wanted to know if the 4chan post had any validity, and it moved me to respond with this:

Read more…

Post Navigation