Archive for category Programming

Project update: roots.cx

Just like a typical Architect, I started this project with the Infrastructure and a few but firm decisions on what technologies to use:
- Web, not desktop
- CoffeeScript and jQuery
- save the App/Pages in JSON format
- Jasmine for testing
- a directory structure, to be consistent in where source files and compiled files will be placed
- a Guardfile to automate compilation of CoffeeScript files to Javascript, and Sass files to CSS
- Jasmine installed as a gem to automatically run the test suite

After spending a little more than a week working on this, I’ve discovered roots.cx, from an awesome guy at Carrot Creative.

It integrates almost everything I’ve done on the project so far, and more:
- automatically compile coffeescript files to js
- provide a facility for splitting the source coffee/js files into multiple smaller files
- compress the js files into one file
- write css files in a coffeescript-like syntax, then compile them to css
- view partials
- write the views in a coffeescript/haml-like syntax

The only thing I found missing was Jasmine integration.

After playing with it for a few minutes, it felt right. Just like Rails, OpenACS, and Delphi in the early days. The easy things are easy, and it’s transparent enough that I can imagine that it doesn’t throw any roadblocks to make the hard things harder.

I threw away all my previous work. No hard feelings there. What’s more valuable from that work was the low-level stuff that I learned. I’m sure I’m going to need that knowledge somewhere down the road. And the Jasmine issue was easily resolved – I just moved over my Jasmine files and the Guardfile.

Onward!

 

No Comments

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.

,

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.

, ,

No Comments

(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

CS193P Study Group: Session 2

Session 2 was conducted on the same venue as, and right after the PhRUG meetup.

Attendees:
topher
rad
eumir
buddy
tin
nico

There were two new attendees, tin and nico, who promised to catch up with the previous assignments.

We discussed memory management. The following questions were raised:



When should you call release on an object?

When you’re the one who did the alloc for the object.



When should you not call release?

When you did not do the alloc for the object.



Why bother with alloc and init at all, when, for example, you can use class methods that do it for you?
For example, why use:

NSString *my_string = [[NSString alloc] initWithFormat:@"format"];

when you can instead the format below?

NSString *my_string = [NSString stringWithFormat:@"format"];

Answer: The stringWithFormat call returns an autoreleased object. On the Mac OS X desktop, there’re practically no difference. On the iPhone, you want to limit the number of objects in the autorelease pool and use alloc/init most of the time so that you can be explicit (and optimize) on when you call release.



As before, let’s try to do Week 3 in one week. That means lectures 5 (Views, Animation, OpenGL) and 6 (View Controllers), and assignment 3 (HelloPoly II). Let’s aim to view the lectures and finish the assignment by next week and have a meetup Thursday next week, February 25.

For those that are catching up and still working on previous assignments, feel free to post questions about them, both on the forum and on meetups.

,

No Comments

Free Cocos2D course

The folks at web-geeks.com have organized a free class for Cocos2d and iPhone SDK 3.1.2:
http://web-geeks.com/uncategorized/cocos2d-course/

It starts on Feb.15. Even if you don’t know anything about Objective-C and Cocoa, you still have some time to cram. You can actually get by with what you’ll learn from the first three chapters of this book: Beginning iPhone Development: Exploring the iPhone SDK.

See you in the class!

Er, the class is virtual, so see you in the class forum instead.

No Comments

CS193P Study Group: Session 1


We had our first session last night, February 4. Johann of Sagada Solutions was kind enough to host the meet-up.

Attendees:
paul
topher
rad
johann
eumir
buddy (cakey)

We skipped discussion of assginment 1A as it was really very simple and no one had problems with it.

Assignment 1B has four sections, so we took turns in discussing our solutions. Paul discussed section 1, Topher section 2, Rad section 3 and Eumir section 4.

Topher mentioned some confusion in creating the project under XCode 3.2/Snow Leopard. The screenshot provided by the handout was for XCode 3.1/Leopard. If you’re on XCode 3.2/SL, see the screenshot below on how the New Project dialog should look like.

Create New project dialog for Assignment 1B under XCode 3.2/Snow Leopard

Everyone was excited and wanted to try a bit faster pace. Tentatively, the next meetup is on Thursday, February 11. It will cover Lectures 3-4 and assignments 2A and 2B. We’ll check our progress over the weekend, and if this pace doesn’t seem workable, people should cry chicken as early as Monday, February 8.

Lastly, the off-topic discussions were also productive. Here are the links to the tools mentioned in passing during last night’s meetup:

Topher’s framework of choice for building iPhone apps:
http://github.com/blog/389-three20-iphone-ui-goodness

Rad’s framework of choice for building iPhone games:
http://www.cocos2d-iphone.org/

Rad’s framework of choice for Flash games:
http://flixel.org/

Interesting paper prototyping tools:
http://apress.com/book/view/9781430228233
http://www.appsketchbook.com/

No Comments

The CS193P Study Group is starting next week


The CS193P study group is starting. Some changes in the mechanics:

  1. The study group will move at half the pace of the CS193P class. This means two lectures and 1-2 assignments every two weeks. There will be one meetup every week.

Week 1 starts on January 25, 2010. Everyone views the lectures and works on the assignments on her own. There will be a meetup on February 4 to discuss concepts and assignment solutions.

No Comments