Migrating To MacVim From Textmate

For first-time users, Vim is daunting. Not to say ugly.
Here’s how MacVim looks out of the box:
Default Vim screenshot

Even monster hacker Yehuda Katz had a hard time with it.

But it need not be that daunting. Just install janus (Carlhuda’s vim plugins distribution), and everything will look almost like they did in Textmate:
Janus MacVim screenshot

Here’s the one-step install command:
curl https://raw.github.com/carlhuda/janus/master/bootstrap.sh -o - | sh

Have fun!

No Comments

Ruby Hacking Secrets From Barney Stinson

Introduction

Yup, that guy from How I Met Your Mother. Ever wonder how he manages to hook up with pretty girls so easily? Is it the suit? The money? The good looks? Actually, his secret is his Ruby ninja skillz.

I’m sure you’re familiar with the adage “Success is being in the right place at the right time”. For the purposes of this discussion, success means getting to hook up with a pretty girl. The right place is right beside the target girl, whoever she is. The right time is when she’s lonely, desperate, or horny.

How does Barney find the right place and the right time? You guessed it right, by stalking them on Facebook.

But Barney doesn’t use the lame Facebook search form. That’s too inefficient for him. He uses Ruby, the Facebook API, and the awesome fb_graph gem. In this article, I’ll give you a peek at the awesome Facebook stalking and searching you can do using these three tools.

SETUP

I’m assuming you already have Ruby 1.9.2 installed. MRI, MacRuby, whatever, as long as it implements 1.9.2 and you run it using RVM.

After that, all you really need is the fb_graph gem:

1
gem install fb_graph

Searching Facebook Using fb_graph

1
2
irb
require 'fb_graph'

Let’s search for the word ‘lonely’.

1
results = FbGraph::Searchable.search('lonely')

Yep, it’s that easy! What you get is an Array of Hashes, each hash containing a search result.

After taking a closer look at the results, you probably don’t like most of what you got – most are people who “Liked” Justin Bieber’s recent song, “One Less Lonely Girl”.

So let’s refine our search a little. Let’s get only search for Facebook statuses. Yep, that’s most likely what we want – girls who have out “I’m lonely” in their status.

1
2
results = FbGraph::Searchable.search('lonely').
              select{|r| r['type'] && 'status' == r['type']}

Now all the results are just status messages.

We can refine this further, though. We want to get only those that point to a User object, because that’s what we’re really after (evil grin).

1
2
3
  results = FbGraph::Searchable.search(search_string).
    select{|r| r['type'] && 'status' == r['type']}.
    select{|r| r['from'] && r['from']['id']}

You might want to take a peek at the users you got from your search:

1
2
3
4
5
6
7
8
9
10
r = results.first
=> <too long, snipped>
user = FbGraph::User.fetch(r['from']['id'])
=> <too long, snipped>
 
user.name
=> "Lolita Bonita"
 
user.picture
=> "https://graph.facebook.com/800200356633259/picture"

And the actual status:

1
2
r['message']
=> "I'm so lonely. I really need a guy beside me."

Now we’re on to something!

A little bit more scripting, and we can automate the download of those user pics!

For The tl;dr Crowd

If you’re too lazy to do all that Ruby, you can download my project on github and just run it:

1
ruby run.rb lonely results.html

This will run a Facebook search for ‘lonely’, download the profile pics of the matching users, and put their name, picture and status (with link to their facebook page) in an HTML file, results.html.

This is…Legendary!

This article was also posted here. You can also view more articles there from my awesome colleagues at Exist.

,

2 Comments

XMPP Development in Ruby

Recently I wasted a day and half in debugging an XMPP-based service I’m working on. As is usual, the real reason I lost so much time was because I was totally unfamiliar with the terrain. I should have spent an hour or two on XMPP basics before I waded into this unfamiliar territory.

If you’re just starting on XMPP development with Ruby, here are some tips and resources that you might find useful.

One client at a time
For each account, there should only be one XMPP client logged in at any time. If client A is already logged in, and then client B logs in, client A will be disconnected and will stop receiving notifications. Just like in Yahoo! Messenger: when you login from a second client, the first client will be disconnected. It’s so basic, but I managed to waste the better part of a day on debugging perfectly working code before I realized that this was what’s going on in my system.

Use switchboard for testing
When debugging network applications, you want to make sure that the application is indeed the problem and not the network. For web applications, you use curl for testing. switchboard aims to be the curl for XMPP development. curl is a command-line (hence scriptable!) http client; XMPP is a command-line XMPP client.

github page: http://github.com/mojodna/switchboard/tree
command-line usage: http://mojodna.net/2009/07/16/switchboard-curl-for-xmpp.html

Useful Resources
If you’re going to do XMPP development in plain Ruby, there’s a peepcode screencast that you might find useful. It might be a bit dated, though, and most likely there are higher-level libraries that you can use.

In my case, I was working on harvesting blog entries from Superfeedr, so I use the Superfeedr gem, superfeedr-ruby. An alternative is the more streamlined and highly opinionated superfeedr-rb.

Even if you’re not doing Superfeedr work, it’s worth spending the time to study superfeedr-ruby, if only to study how it uses Skates. Skates (formerly Babylon) is a framework for building XMPP applications in Ruby, using EventMachine for handling connections.

,

No Comments

Bawal ang Wangwang is Cool, But We Can Do Awesome

Bawal ang Wangwang is cool, but here are two awesome yet easily doable projects for the PNoy administration.

Cool
http://www.gmanews.tv/story/197939/dept-of-finance-launches-crowdsourcing-website

Awesome
Put all docs of the DOF and DBM into Scribd.
Crowdsource the work, too, to save money. I’m sure all the students who volunteered in Ondoy relief efforts will work on this, too.

Cool
Bawal ang Wangwang

Awesome
Put the performance documents of the PNP – from the regional offices down to the sub-station level – into GoogleDocs. Crime rates, conviction rates, everything. Put it as a spreadsheet for easy third-party analysis. After the Mendoza incident, I’m pretty sure a lot of IT folks will come up with ways to slice and dice the data.

No Comments

Book Review: Tapworthy

Tapworthy
Tapworthy
Designing Great iPhone Apps
by Josh Clark
Publisher: O’Reilly Media
Released: June 2010
320 pages

I’m a programmer who occasionally dabbles in user interface design when I’m doing solo projects. I know enough to know when a UI is bad or good, but I’m at a loss on how to create good UI from scratch. On my iPhone games, I do my own UI. There’s the iPhone Human Interface Guide, but I feel that it’s a bit hard to digest and I feel that it’s too specific to non-gaming applications.

Tapworthy takes iPhone HIG then mixes it with insights from recent user interface research and human psychology. The result is a very readable guide with specific do’s and don’ts to follow in designing your iPhone app interface. The tips, along with the long but readable discussion on the principles behind those tips, are general enough that even game developers will find them useful.

For example, the recommended hit region for user interface elements should be 44×44 pixels. This is a very important principle to follow (n00b mistake: on an app I was developing, I started with swipeable coins that are 20×20. Not surprisingly, on my first user tests, the users found the coins too small and very hard to swipe). The iPhone HIG actually mentions this, but this tip is buried in there and is only mentioned three times. In Tapworthy, this tip has its own section title (“The Magic Number is 44″) and is mentioned more than a dozen times to really drill in the point.

Another solid tip from the book: Do not use text in designing the app’s icon. It won’t be readable, and it will just add visual clutter to the icon. This tip is actually part of an entire 30-page chapter (Chapter 7) devoted to App Icon Design.

I can’t find anything that I don’t like about this book. It can use some improvements, though:

  • Given that roughly half of iPhone apps are games, it could add more tips specific to gaming applications. Maybe games aren’t in the author’s repertoire, and that’s a topic for another book.
  • The book contains lots of internal references, e.g. “…a concept explained in the Facebook case study on page 236…”. On the PDF version, the references aren’t links, though. What’s more, on the ePub version, when reading it on Stanza for the iPhone, the links are meaningless because the ePub pages on Stanza doesn’t really match the PDF’s page numbers. This isn’t major, though, and it’s something O’Reilly can easily fix with an updated digital release.

If I have to describe this book in a nutshell, I’ll say it’s Alan Cooper’s About Face, translated to the iPhone world. Great work.

, ,

No Comments

Blizzard is the Enemy of Rails

Not Django, not Java, but Blizzard. They’re actively working to reduce Rails3 adoption.

Take a look at these dates:

Rails3 beta1 release date: Feb 5, 2010

Starcraft 2 beta release date: Feb 17, 2010

Rails 3 RC release date: July 26, 2010

Starcraft 2 release: July 27, 2010

,

No Comments

Corona SDK

It seems I can develop some games much, much faster with the Corona SDK. It will cost me $99, and it’s not as macho as mastering Objective-C, but I think I’ll end up saving hundreds of hours with this kit.

I think Corona is a great kit for writing games in general, especially arcade games and board games. There’s no built-in support yet for tiled maps, and it looks like Cocos2D will have that built-in first. But even with that disadvantage, developing on Lua is so much more productive than developing on Objective-C that I’ll still come out ahead.

Some apps aren’t a good fit, though:

  • those that extensively use the CocoaTouch UI controls. In Corona, you have to create your own buttons, text fields, etc. There’s the ui library, but it only covers a few controls.
  • those that access APIs that aren’t wrapped yet by Corona. For example, access to the address book seems to be missing.

There’s the worry that Corona SDK will violate section 3.3.1. Folks from Corona gave some reassuring messages, but of course, with Apple, you can’t have absolute uncertainty. My take is that even if Apple gives Corona the boot, Corona will become Android-only, and Corona SDK is worth $99 even as an Android

I’m taking the $99 plunge. If nothing else, I think Lua will give me a refreshing break from my day-to-day Ruby and Javascript coding.

, ,

No Comments

work.sh

From a cold boot, to start working on one of my Rails projects, I need to do the following:

      open Terminal, then Start MySQL or PostgreSQL

        in a separate Terminal tab: cd Documents/projects/ ; mate .

          (if applicable) in a separate Terminal tab: Start Solr or Ferret

            Start Firefox

              Start Evernote

That’s a lot of keystrokes and mouse actions. No wonder I end up playing games most of the time, because Age of Empires III takes just one click.

I can configure MySQL and PostgreSQL to run on startup, but that’s wasteful. That’ll slow down the machine’s startup time, and sometimes I use just MySQL, on other projects I use PostgreSQL, and on other projects I use neither but instead use MongoDB. And that will just take away one step in this 5-step process to get from cold bootup to work bliss.

Ideally, there’s a one-click work button, a sideproject1 button, a sideproject2 button, etc. Press the right button and the Mac will be transformed into a mode optimized for the work to be done: run the needed apps, close unneeded apps, play music from the “work playlist”, change the desktop background, etc.

I have decided to learn AppleScript to build those magic buttons. Here’s what I’ve come up with so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/bin/sh
 
# From: http://stackoverflow.com/questions/1589114/opening-a-new-terminal-tab-in-osxsnow-leopard-with-the-opening-terminal-windows/1590818#1590818
# (See answer by i0n that says "courtesy of Dan Benjamin" http://twitter.com/danbenjamin)
 
# Sets up my Infinitely terminal windows
 
# 1. Run MongoDB
# 2. Run redis in a new terminal tab
# 3. cd to the project
# 4. Launch Evernote
# 5. Launch Firefox
# 6. Launch GitX
 
 
# 1. Run MongoDB in a new terminal
/usr/bin/osascript <<mongo
activate application "Terminal"
tell application "System Events"
	keystroke "t" using {command down}
end tell
tell application "Terminal"
	repeat with win in windows
		try
			if get frontmost of win is true then
				do script "cd ~; ./mongo.sh" in (selected tab of win)
			end if
		end try
	end repeat
end tell
mongo
 
# 2. Run redis in a new terminal
/usr/bin/osascript <<redis
activate application "Terminal"
tell application "System Events"
	keystroke "t" using {command down}
end tell
tell application "Terminal"
	repeat with win in windows
		try
			if get frontmost of win is true then
				do script "cd ~; ./redis.sh" in (selected tab of win)
			end if
		end try
	end repeat
end tell
redis
 
# 3. cd to the workers project
/usr/bin/osascript <<workers
activate application "Terminal"
tell application "System Events"
	keystroke "t" using {command down}
end tell
tell application "Terminal"
	repeat with win in windows
		try
			if get frontmost of win is true then
				do script "cd ~/Documents/projects/infinitely/workers; mate ." in (selected tab of win)
			end if
		end try
	end repeat
end tell
workers
 
sleep 1
# 4. Launch Evernote
open /Applications/Evernote.app
 
# 5. Launch Firefox
open /Applications/Firefox.app
 
clear

There’s still no “Open iTunes and play songs in my ‘work’ playlist” section, but I’m working on it. And here’s something to make it more interesting: AppleScripting with Ruby!

This script borrowed heavily from the ideas of the people in this Stackoverflow thread.

, ,

2 Comments

iPhone SDK TOS 3.3.1: One Month later

If you’ve been living in a cave, “3.3.1″ refers to section 3.3.1 of the iPhone Developer Program License Agreement. Section 3.3.1 for the iPhone 4.0SDK beta reads:

3.3.1 — Applications may only use Documented APIs in the manner prescribed by Apple and must not use or call any private APIs. Applications must be originally written in Objective-C, C, C++, or JavaScript as executed by the iPhone OS WebKit engine, and only code written in C, C++, and Objective-C may compile and directly link against the Documented APIs (e.g., Applications that link to Documented APIs through an intermediary translation or compatibility layer or tool are prohibited).

It’s been about a month since 3.3.1 came out. The most obvious impact of this is on Adobe Flash CS5. Prior to 3.3.1, Adobe has announced that Flash CS5 will include a Flash-to-iPhone compiler. Now, Adobe has cancelled that feature.

Here’s a roundup of how the other iPhone development tool providers have reacted to 3.3.1. Except for funkaster, writer of shinycocos, most of them seem to be optimistic that they’re in the clear.

AnscaMobile (Corona SDK):
http://blog.anscamobile.com/2010/04/corona-and-iphone-os-4-0/

Torque (iTorque):
http://www.torquepowered.com/community/blogs/view/19715

Unity:
http://blogs.unity3d.com/2010/04/10/unity-and-the-iphone-os-4-0/

funkaster (shinycocos):
http://github.com/funkaster/shinycocos/commit/9ebda7fcfc853db266b4b49c01bdbc5fa5a9be42#comments
(see the comments below the git commit)

Rhomobile:
http://rhomobile.com/2010/04/09/iphone-4-0-sdk-rules/

Me? I’ll stick to learning plain XCode, Objective-C, plus the Cocos2d Library.

,

1 Comment

(CONSTANT == var) vs. (var == CONSTANT)

I’ve been programming for quite some time now, but until now I never understood why some programmers prefer
CONSTANT == var over var == CONSTANT

It’s to avoid a common C coding mistake of using = instead of ==. If you use the former form, and you inadvertently use = instead of ==, the compiler will catch your mistake because you’d be trying to assign a value to a constant.

I never really needed this convention because I have always been careful with my ==’s. One of the earliest C lessons I’ve learned. Still, it’s helpful to understand why some veterans prefer one form over the other.

No Comments