01110000 01100001 01110011 01110011 01101001 01101111 01101110

Dynamic namespaced class instantiation in ruby

Posted: February 12th, 2010 | Author: Kevin | Filed under: Development, Tips | Tags: | No Comments »

Lets assume we have a module called Fruit, within the Fruit module is a bunch of Fruit classes.

module Fruit
   class Apple
   end
   class Orange
   end
   class WaterMelon
   end
end

If the name of the fruit comes from some data or memory store, and we want to instantiate the class dynamically via the fruit name, you might attempt to do something like this:

>> fruit_name = "Apple"
=> "Apple"
>> class_name = "Fruit::#{fruit_name}"
=> "Fruit::Apple"
>> klass = Object.const_get(class_name)
NameError: wrong constant name Fruit::Apple
        from (irb):14:in `const_get'
        from (irb):14

The problem here is #const_get does not support retrieving constants within a nested namespace. In order to combat this, we must first retrieve the module in which the class is in, then get the class.

>> Module.const_get("Fruit").const_get(fruit_name)
=> Fruit::Apple
>>

Here is an example in action:

module Fruit
  class Apple
    def self.color
      "red"
    end
  end
 
  class Orange
    def self.color
      "orange"
    end
  end
 
  class WaterMelon
    def self.color
      "green"
    end
  end
end
 
%w{Apple Orange WaterMelon}.each do |fruit_name|
  klass = Module.const_get("Fruit").const_get(fruit_name)
  puts klass.color
end
 
# output
red
orange
green

VIM tip of the day: quickly reformat your document

Posted: July 25th, 2009 | Author: Kevin | Filed under: Development, Tips, Tool | Tags: , , | No Comments »

Quickly reformat your document in VIM with:

gg=G
  • gg goes to the beginning of the document
  • = kicks off the indent filter (:help = for more information)
  • G goes to the end of the document

Another way to achieve the same result through visual mode is.

ggvG=
  • gg goes to the beginning of the file
  • v enables visual mode
  • G goes to end of the file
  • = apply indentation

Similarly, if you are on a line that needs formatting, simply use ==, or v=. You can also select a block of code in visual mode and press = to reformat the selection.

Breath this command and make your team happy.

If you find this post useful, please drop a line in the comments.


Using git-svn with non-standard repository layouts

Posted: April 16th, 2009 | Author: Kevin | Filed under: Development | Tags: , | 1 Comment »

Using git to interface with svn repositories is awesome. You get all the advantages of git: local commits, squashing/amending commits, cheap branching, local stash, smart merging, wicked fast operation, etc. However, when you try to have git work properly with a non-standard svn repository layout, things tend to get wonky.

git-svn tries to be smart, it does so by doing what it thinks you want it to do and not exactly what you tell it to do.

First of all, a standard svn layout looks something like this:

- awesome_project
  - trunk
  - branches
    - 1.0.x
    - feature_a
    - feature_b
  - tags 
    - 1.0.0
    - 1.0.1

Getting git to talk to such a repository is easy as pie:

$ git svn clone -s url://to/awesome_project

The -s option tells git to use the standard layout and automatically map your trunk/branches/tags as… master, branches, and tags. You probably want to tell it to fetch a specific revision to speed things up if you have a large repository.

But like everything else in life, things are not always that easy. You may run into times where you need to have git talk to a non-standard repository layout. Maybe something like this:

- awesome_project
  - trunk
  - branches
    - fork_a
      - trunk
      - 1.0.0
      - 1.0.1
    - fork_b
      - trunk
      - 1.0.0

In my case, I happen to work on fork_a almost exclusively, so the most logical thing to do was:

$ git svn clone url://to/awesome_project/branches/fork_a

If you did something like that before, you may find that git svn thinks you meant to use awesome_project as the project root. The reason why it does that is git svn starts from the beginning of the history, and moves up until it finds /branches/fork_a. Unfortunately, when git sees standard svn directories (trunk, branches, and tags) in the project root, it incorrectly corrects your error for you.

Don’t you hate it when the computer does what it thinks you want it to do instead of what you tell it to do?

The solution to this problem is first initialize an empty git repository:

$ git svn init /path/to/awesome_project

Open up .git/config and change your svn-remote section to look something like this:

[svn-remote "svn"]
    url = http://awesome_host/awesome_project
    trunk = branches/fork_a/trunk:refs/remotes/svn/trunk
    branches = branches/fork_a/*:refs/remotes/svn/branches/*

Save the changes and do a fetch on a more current revision.

This will treat each folder under branches/fork_a, including trunk, as git remote branches.

The key here is to not use the full url to the specific fork/branch you want to clone; instead, use the svn project root as the url and specify the path relative to the project root in the trunk/branches/fetch options in .git/config. This configuration setting is identical to using git svn clone/init with the –prefix option.

After doing a fetch (specifying a decently recent revision). Your git branch -r looks something like this:

$ git branch -r
svn/branches/1.0.0
svn/branches/1.0.1
svn/branches/trunk

And you’re done! You can now create feature/working branches from these remote branches and work with git and git svn as you normally would.

Hopefully in a future release, we will be able use regexes in place of globs for matching paths in .git/config. It will no doubt make interfacing with nonstandard svn repository layouts using git much easier.

Hopefully this helps some of your out there. Your repository layout may not be identical to the example I provided above, but this technique should be applicable to any reasonably organized repository layout. Right now my solution does not map the tags from svn (1.0.0, 1.0.1) as git tags. If you happen have a better solution, please share!


Impressions with Netbeans 6.7 M3 for PHP

Posted: April 10th, 2009 | Author: Kevin | Filed under: Development, PHP, Tool | Tags: , , , | 7 Comments »

A recent contract job required me to work on-site, which meant that I had to use my laptop as my primary development rig. In eclipse, my low-end 1st gen Macbook Air crawls to its knees (surprise!) when I load it up with a 5000+ file project (including libraries).

Textmate is normally my tool of choice. But since we use a monster framework, code completion and inline documentation will undoubtedly boost my productivity and increase my familiarity with the existing API + codebase, not to mention the added benefit of a debugger to step through the code to know wtf is going on.

I went on a mission to find a new IDE. I decided to revisit Netbeans for PHP (I use it for Java already) by trying out the prerelease. After using Netbeans 6.7 M2 (and recently M3) for 2 months at work, aside from the occasional bugs, I could not be more impressed by its performance and feature-set. If you do decide to try it out, make sure you use the latest milestone release. That would be 6.7 M3 as of this post.

Lets look at some of the key features that I use on a daily basis.

Read the rest of this entry »