May 4 2010 by Tye Shavik
We use helpers to refactor our render statements and we end up with a lot of helpers like this:
def comment_fields(f,course,discussion,comment)
render :partial => 'comments/fields',
:locals => {
:f => f,
:course => course,
:discussion => discussion,
:comment => comment }
end
In these helpers you’re typing out each variable name 3 times, once in the function definition and twice in the locals hash. Wouldn’t it be great if you only had to type the name of the variables in the definition and have the local hash generated automatically? Well we’ve created a nifty helper called locals that will do just that. It works like this:
def comment_fields(f,course,discussion,comment)
render :partial => 'comments/fields',
:locals => locals(binding)
end
The binding object is built-in to Ruby and allows you to access a list of variable names in the local scope. The locals helper will use this object to generate a hash of variables that you can pass to the render function.
You will need to put this function in your application_helper.rb:
def locals(helper_binding,*locals)
options = locals.extract_options!
result = {}
vars = eval("local_variables",helper_binding)
for var in vars
result[var.to_sym] = eval("#{var}",helper_binding)
end
result.merge(options)
end
locals(binding) will grab all the local variables that exist at the time it’s evaluated (not just the variables that are in the function definition). You can pass a hash to locals to specify additional variables to pass.
So for example this code:
def comment_fields(f,course,discussion,comment)
user = comment.user
render :partial => 'comments/fields',
:locals => locals(binding,
:comment_count => discussion.comments.length)
end
Is the same as:
def comment_fields(f,course,discussion,comment)
render :partial => 'comments/fields',
:locals => {
:user => comment.user,
:f => f,
:course => course,
:discussion => discussion,
:comment => comment,
:comment_count => discussion.comments.length }
end
May 2 2010 by Tye Shavik
While writing code I often find I change simple if/else statements into single line statements using the ternary (?:) operator. For example this function from Markadee:
def mine?(user)
if current_user == user
t('users.show.your')
else
"#{user.name}'s"
end
end
The if/else takes up 5 lines but the statements are short enough to be put on one line:
def mine?(user)
current_user == user ? t('users.show.your') : "#{user.name}'s"
end
Usually I will write out the entire if/else statement then change it to single line form afterwards once I see how short it is. To save myself some time I created two Textmate bundle commands to help do this. The first will take a 5 line if/else (such as the one in the mine? function above) and turn it into a single line statement. This requires you select the entire if/else then press the hotkey ⌥⌘T.
The second will do the reverse operation, turning a ternary operator statement into an if/else. The hotkey for the reverse function is ⌃⌥⌘T and will grab the entire current line so you don’t need to select the statement first.
Download the Textmate Bundle commands here.
Mar 9 2010 by Andrew Brown
I spent 1.5 hours searching the Internet and the JQuery docs to figure out how to perform sub or gsub on a string with a regular expression. I had to use a crazy mix of keywords to find the answer. Hopefully this blog post gets index on Google so the next person looking to do regular expressions in JQuery doesn’t have to waste so much time looking for the sub or gsub equivalent because the JQuery people can’t stick to programming conventions that makes sense and are already known by most programmers. This is a case of unnecessary naming cleverness. Give me back my time JQuery, you time vampire.
Oh, let us not forget the solution:
"This is a test".replace(/test/,'an explosion')
Mar 5 2010 by Andrew Brown
I’ve spent hours in the past crafting my resume, portfolio and website but everytime I’ve been hired, the didn’t look at my resume, they never saw my portfolio, and they didn’t even visit my website. I had always offered sample work for free out of selflessness and not expecting anything from it, and they all turned around offering long lasting work.
You want to get hired as web-developer? Don’t show a paper that suggest results, give them something to chew, if it tastes good, they’ll want more.
Mar 2 2010 by Andrew Brown
Look around on your desk and pickup the closest thing near you.

I picked up my Swingline red stapler. Why did I buy it?, how often do I use it and why do I need it? I am never stapling and I’m not going to staple anything in the next year.
I have a pile of papers on my desk that I’m holding onto because I need to do my taxes. The pile looks daunting, but if I were to throw out the junk in that pile, doing my taxes would seem much easier.
I need to frequently travel, and I have a pile of clothes. I don’t wear half of them but when it comes to packing, the pile looks daunting, if I were to throw out the clothes I don’t wear my packing would seem much easier.
I have a iMac that only gets used to play my itunes music, a Nikon D80 I take photos with but I don’t take the photos off the camera until three months later.
I don’t need these things but Its hard to part with them. I know I can justify keeping them because myself as an irrational human being wants to keep them, but I want you to listen to your robot mind.
Stop holding on to shit you don’t need and remove the visual clutter that is blocking your end goal.
I’m getting rid of my red stapler today and I want you to question what you picked up, and if you can come up with one reason to justify on how its distracting you from your end goal then throw it out now.
Your end goal awaits you.
Feb 22 2010 by Andrew Brown
To be a professional Ruby on Rails developer you need to properly commit your changes to the git repos without making a big mess. Compare the two git trees below.

The problem is when your working with multiple people on a git repos you can’t work in master otherwise when you pull changes, you’ll have a messy merge and when you push you’ll make a bunch of messy lines in your git tree, and you’ll get yelled at for making those messy lines.
I want to go through the steps with you to get your git tree to look like one straight line. Look at the model below to understand what you need to do.

Lets go through the step of adding a new feature:
Make sure we’re on master
Make sure our master is up-to-date
Create a new branch called new_feature
git checkout -b new_feature
Make changes and commit as much as you want
1. Now we’re ready to push our changes so lets make sure our
master is up-to-date
git checkout master
git pull
2. Lets rebase (If everything was up-to-date skip this step)
git checkout new_feature
git rebase master
3. Merge new_feature into your master
git checkout master
git merge new_feature
4. Push changes
That’s all there is to it! Single clean line, nobody is yelling at you, and you can make fun of your coding friends who have never rebased before.
Jul 20 2009 by Andrew Brown
I wanted to be able to group my posts by months in my wordpress template. I couldn’t find a quick copy and paste online so I wrote my own snippet of code to achieve that result. I had to endure writing php. Since I’ve been a Rails Developer I think I’ve gotten lazier or php is archaic.

<div class="archives">
<?php
$months = $wpdb->get_results("SELECT DISTINCT MONTH(post_date) AS month , YEAR(post_date) AS year FROM $wpdb->posts WHERE post_status = 'publish' and post_date <= now( ) and post_type = 'post' GROUP BY month, year ORDER BY post_date DESC");
$posts = $wpdb->get_results("SELECT id, post_title, MONTH(post_date) AS month , YEAR(post_date) AS year FROM $wpdb->posts WHERE post_status = 'publish' and post_type = 'post' ORDER BY post_date DESC");
foreach($months as $this_month){ ?>
<div class="month">
<h2><?php echo date("F", mktime(0, 0, 0, $this_month->month, 1, $this_month->year)); ?> <?php echo $this_month->year; ?></h2>
<?php for ($i = 0; $i <= count($posts); $i++){?>
<?php if(($this_month->year == $posts[$i]->year)&&($this_month->month == $posts[$i]->month)){?>
<p><a href="<?php echo get_permalink($posts[$i]->id); ?>"><?php echo $posts[$i]->post_title; ?></a></p>
<?php } ?>
<?php } ?>
</div><!-- month -->
<?php } ?>
</div><!-- archives -->