Sameer Siruguri

My Blog

Archive for the category “ruby”

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…

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.

Autocomplete

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…

HTML and SQL Basics Using A Ruby Web Scraper Example

In this lesson, we will learn some basics about two languages that are fundamental to how (many) web applications work – HTML and SQL. To make this a bit real, the goal of this exercise will be to write a script in Ruby that can do the following: look at the newest stories on Reddit, and insert into a local database the following information about the top 10 of those stories – the author, how many points it has, its category, its title, and the date it was posted.

HTML, or Hypertext Markup Language, is the syntax understood by most browsers. It has been around for over thirty years and was designed initially by Tim Berners-Lee (who recently appeared on the Internets pleading for your help in keeping the Internets open – go watch this video!)

SQL, or Structured Query Language, is used in creating and querying what are called “relational” databases – where the stored data refers to various objects that have relationships to each other (a typical example would be an e-commerce database, where products are related to the companies that sell them.) Read more…

The Unsolved Mysteries Of The World

I was inspired by this blog post by Keir Clarke 1)who incidentally has more map visualizations of Google Auto Suggest behavior on how Google Auto Suggest was completing the query Why Is X So… for various state names (in the United States), to wonder what equivalent wisdom of the crowds Google had gathered for other placenames associated with where people live.

I thought it’d be a simple matter of feeding a list of city or country names to a Google API, and realized that there isn’t one – turns out that unlike Bing, Google isn’t so happy for the common consumer to share in all its indexy goodness. Well, no matter, I figured I wasn’t trying to build a commercial-grade application, so I could do just as well by scraping the contents of a browser, via Selenium and Ruby. Read more…

References   [ + ]

Building Your Own Gem: Adventures in Ruby Programming

I just built my second gem – I thought I’d have written about the first, but somehow it escaped my attention. The first one is already on RubyGems, and generates a seed for you from the Weather Underground API by pulling together a string from some of the weather data on the feed.

When I started writing the second one, I realized that I didn’t remember the very first command I had run to create the gem’s template files – the gemspec, the lib/ and test/ folder, and so on. So here’s a quick round-up of some good tutorials on gem-cutters. I used bundle btw, and here’s a shameless plug: I’ve uploaded a slightly modified set of template files generated from bundle gem, with this one including the requisite files to write tests using minitest. Read more…

Ruby Metaclasses Part II: Why @@ Is Not Really a “Class Variable”

We are on our way to understanding why Ruby has metaclasses. The short answer – TL;DR – is that metaclasses are necessary because Ruby implements class inheritance in a “flat way” – all classes derive from one class called (very plainly), Class. Class can’t hold all the methods that need to be inherited by the various classes in your program, so you need each class to have a special parent class – which is what the metaclass is.

Well, let’s get back to understanding all the details of class inheritance first. In our last lesson, we got to how we create “class instance variables.” Let us now figure out what the @@ notation is all about.

Read more…

Ruby Metaclasses: What Are They Good For? Part I

I’ve been playing around with Ruby for a while now, and I know that you can create meta-classes, but why this is a feature designed into the programming language hasn’t been very clear to me. Until today, when I found a use-case that really brought it home to me.

A critical part of understanding the reasoning behind having meta-classes was actually when I took some time to understand what the @ and @@ notations really do. A lot of tutorials and descriptions will say that this is the syntax for defining, respectively, “instance variables” and “class variables.” This isn’t entirely true.

Read more…

Rails App With Goodies: Adding Devise, CanCan And More Automatically

I have been teaching myself Rails now and listening to many of Ryan Bates’ most excellent Rails Casts, about which I cannot say enough good things.

I figured there’s usually a small list of things that most web apps tend to need, and if you’re a beginner Rails programmer like myself, you have probably spent some time trying to learn how to put all this code in. In fact, if you have RailsCast religion like I do, you have been probably copy pasting it from Ryan’s tutorials, except now you need to build a new app that does some other cool thing and you can’t remember all the Devise, CanCan, DoorKeeper, OAuth magic you painstakingly put together the first time around.

And even if you know these gems inside-out like the logo of your favorite brand of yogurt, and can paste in their configurations with both hands cuffed, wouldn’t it be nice to have a simpler way to do it?

Well, now you do! Head on over to this Github repo and quickly get Devise, CanCan and OmniAuth (with two providers – Facebook and Twitter!) running in your app.*

Read more…

Ruby and Rails on Mountain Lion: Having Problems?

I started following the remarkable guide over on Created By Pete, but ran into some problems.

When you install Ruby via rbenv, it needs OpenSSL, and it appears that you need to install the version that Homebrew gives you:

brew install openssl

The installation notes that this version might conflict with the version that’s already installed with OSX: Read more…

Post Navigation