SuperHero.js for Javascript Best Practices

Javascript has been evolving way more quickly than I have been able to keep up. I’m still behind on some of the recommended best practices and modern techniques. Luckily someone had the good sense of putting up and documenting these things. I hope s/he/they keep up the good work to spare the rest of us.

This wonderful resource is SuperHero.js. It keeps up-to-date a list of resources on best practices and latest techniques in the world of Javascript for the rest of us peasant coders. Kudos to the site maintainer(s).

Duck Typing in Javascript

PHP was the first programming language I learned. But Ruby has been the language that I love. One of the patterns Ruby champions is the idea of Duck Typing. It’s a great way to avoid using if/else statements too excessively. In Javascript, this can be easily implemented as well. This article does a great job explaining it with some sample code. It’s a good thing!

Ruby’s #length vs #size vs #count Methods

Before googling for the article, I knew going in that .size is an alias to .length. But I didn’t know there was more to the story. And what about .count?

Here’s what Josh Susser wrote verbatim:

In Ruby, #length and #size are synonyms and both do the same thing: they tell you how many elements are in an array or hash. Technically #length is the method and #size is an alias to it.

In ActiveRecord, there are several ways to find out how many records are in an association, and there are some subtle differences in how they work.

* post.comments.count – Determine the number of elements with an SQL COUNT query. You can also specify conditions to count only a subset of the associated elements (e.g. :conditions => {:author_name => “josh”}). If you set up a counter cache on the association, #count will return that cached value instead of executing a new query.

* post.comments.length – This always loads the contents of the association into memory, then returns the number of elements loaded. Note that this won’t force an update if the association had been previously loaded and then new comments were created through another way (e.g. Comment.create(…) instead of post.comments.create(…)).

* post.comments.size – This works as a combination of the two previous options. If the collection has already been loaded, it will return its length just like calling #length. If it hasn’t been loaded yet, it’s like calling #count.

Retain Vim Color Scheme in sudo

Ever wonder why vim loses its color schemes and settings when you are in sudo mode? I hate it. But there’s a way to preserve your vim settings while in sudo mode.

sudo -E vim some_file.txt

The

1
-E

switch is the holy grail. Or you could use sudoedit if you have it set to using vim. And it’d have the same effect.

Speed Up Wake from Sleep Time on Mountain Lion

I bought a very beefy MacBook Air this summer. But I was frustrated with how slow the machine’s wake-from-sleep time was whenever it’s been in sleep mode for an extended period of time. For the longest time I just couldn’t figure it out. But as it turns out, it’s a new “feature” designed to make the batteries last longer.

Basically after certain number of hours, in order to save on energy, Mt. Lion saves most of the content in RAM to disk, thus making the wake up taking much much longer than desired. Here’s the blog I found with instructions to change that.

And here’s the instruction just in case. Open your Terminal via Applications > Utilities > Terminal.app, and issue the following command:

sudo pmset –a standbydelay 43200

43200 is seconds, which is 12 hours. I almost always never leave the laptop unused more than 12 hours, so I set mine to 86400, which is 24 hours. To find out what the current setting is, issue this command:

pmset -g

And there you have it!

Resources for Working with Objective-C’s Core Data

I started dabbling with building an app in Objective-C and Xcode recently. And Core Data seems to be a nice way to work with persisting data across app launches. But its learning curve is notoriously steep (Apple’s own documentation doesn’t help at all).

But Techotopia has a nice and short overview of what Core Data is and how it works in a way that’s not intimidating. But the best source for me by far is “Core Data for iOS and OS X with Simon Allardice” from Lynda.com. I’m a visual/audio learner. And the best way for me to learn is usually through a Youtube video or some clips from Lynda’s web site.

Upgrading Rails 2.x to Rails 3

I gathered a small collection of sites on how to upgrade from Rails 2.x to Rails 3 for my last job. But we never got around to using it. But instead of throwing away the bookmarks, I thought it might be a good idea to save them somewhere more “permanent”.

One method I keep hearing from others who have done it is to make sure you have decent test coverage for your app. And then upgrade one baby steps at a time: First upgrade to the next immediate major version of Rails, and then the one after that… etc. For example, Rails 2.3.x => 3.0, and then 3.0 => 3.1, and then 3.1 => 3.2… etc. And along the way, fix your code and make sure all the tests pass.

Happy coding.

Resolving “error: failed to attach to process ID 0” in Xcode

This was a bitch to track down.

error: failed to attach to process ID 0

Basically it means Xcode is not able to attach a build of your app to the simulator. I was, however, able to build it directly on the iPhone. To solve this problem, simply do the following:
* Click on Simulator and reset the simulator by going to the “iOS Simulator” main menu and choose “Reset Content and Settings…
* Back in XCode, choose the menu "Product" > "Edit Scheme". On the left menu panel, click on "Run <em>Your_Project_Name</em>.app" > "Run _Your_Project_Name.app". The info display on the right should have the "Debug Process As" radio button picked as "Me (your-user-name)". If not, make sure you do not login as root. On Stackoverflow there are many reports of running as root will have problems
* Next, still in Xcode, go to the menu "Window" > "Organizer". On the top menu, click on "Projects". Go ahead and delete "Derived Data".
* Finally, go to "~/Library/Application Support/iPhone Simulator/6.0/Applications" and delete everything that’s in there

Restart Xcode and the simulator. And Bob’s your uncle.

Export SVN Repositories to Git on Mac OSX

Once you go git, you can never go back.

1. First, make a directory into which you want the newly minted git repo to live
2. cd into that directory
3. Then execute the following:

git svn clone file://localhost/Users/your-user-name/location/of/svn/repo

And that’s it!

Moving A Folder from One Git Repo to Another While Keeping History

Moving stuff from one git repo to another while keeping the history intact is a cool way to break stuff out of old repos and migrate them around. Here’s how to do it:

1
2
3
4
5
6
7
8
9
10
11
12
13
git clone --no-hardlinks /path/to/sourcerepository directory-to-move
cd directory-to-move

# remove remote origin just so we can't break anything in the source repository
git remote rm origin

# filter for desired files
git filter-branch --subdirectory-filter new-directory-name HEAD

# move around files, etc.
# then commit
git add .
git commit -m "isolated files"

Refernece: Moving files from a Git repository to another keeping history

Lives of .bashrc, .bash_profile, .profile, and Beyond

Before reading this article on the differences, I’d been stuffing all my export, alias and other things in either .bash_profile or .profile files on my Macs.

Though most settings will run just fine in either file, there is an important difference between .bash_profile and .profile:

If you need something to be executed or sourced (i.e. source ~/.profile) as you open a new terminal tab or terminal window, stick it inside of .bash_profile. Items in .profile only get run once when you login.