January 2012
1 post
2 tags
tconsole 1.0 is out!
I’ve been using a little project I spiked out called tconsole for a couple of months now, so I figured I should mark the gem as 1.0 and get on with things. tconsole is a command line tool that helps me and other Rails developers test more efficiently by not requiring us to reload our environment every time we run a test. tconsole has some helpful features like allowing us to easily test...
December 2011
8 posts
Figuring Out Where a File Was Required
I’ve been working on test performance a lot lately. Tonight I ran into a strange issue and needed to figure out where a file was being required from. That can be pretty tricky in a Rails app, since often files are being autoloaded.
I used this code up at the top of the file in question to figure out where it was loaded from:
puts caller.join("\n")
When the file was loaded by the Rails...
Be Sure to Include A Rating for Gravatar
When we launched Treehouse, we decided to use Gravatar for user avatars on profile pages. Here’s my profile page, for example. When we rolled it out I copied the Gravatar code on their site and ported it to Ruby. It was a pretty simple task.
Unfortunately, I heard reports from a few users after launch that their profiles weren’t showing up correctly, and were instead showing the...
How I GTD with Email
I read David Allen’s Getting Things Done quite a while back when I was younger, worked at an insurance company, had VP in my title and thought that was awesome, and felt like more than anything else I just needed to get things done. Now, six or so years later, my mind summarizes the book something like this:
Keep a list on paper. Add things to it as you realize you need to do them.
Double...
Cellular Automata Method for Generating Random... →
I’ve always been super interested in cellular automata and thought it was interesting to see such a practical use case for them.
Teen Texting
Gruber linked to an article by Nielsen about Teen Texting. He quotes the article:
The number of messages exchanged monthly (SMS and MMS) hit 3,417 per teen in Q3 2011, averaging seven messages per waking hour.
At first that data point was pretty astounding to me. The average teen sends 3,417 text messages per month? 7 per waking hour? I can’t imagine texting anything near that...
Ditching Rdio
I’ve been an Rdio member for a while now, but something kept nagging me. I love music. I have a job that lets me listen to it pretty much all day, and I definitely take advantage of that, but the whole time I used Rdio it felt like it was causing me to keep music at arms length.
I think Rdio’s social features and recommendation features are amazing because they told me what music I...
Sharing Reminders with iCloud
I didn’t realize this was possible, but you can share reminders with other people in iCloud. It’s pretty easy to do:
Log into iCloud
Go to your calendar.
Click on the share link for the reminder list you want to share.
Enter the email address of the person you’d like to share the reminders list with.
Once you do that, the person you’re sharing the list with will get...
Test Performance Tip from José Valim
I came across this post by José Valim and while two out of the three test speedup recommendations didn’t apply to me, this one did:
Add the following to your spec/test helper:
Rails.logger.level = 4
I thought it was worthwhile to give it a try. On the entire Treehouse test suite, our test execution time went from 2 minutes 28 seconds to 2 minutes 1 second. That’s around an...
November 2011
3 posts
Emoji Cheat Sheet for Campfire and GitHub →
Because, you know, getting your emoji on in Campfire and in GitHub comments is pretty important.
Credit Card Number Generator
While working on Treehouse, this Credit Card Number Generator came in handy to give me a ready supply of seemingly valid (but actually not valid) credit card numbers that would come back with errors in our payment processor’s sandbox. Super helpful if you need to test out bad transactions and your payment processor doesn’t give you bad credit card numbers to test with.
September 2011
22 posts
Pure CSS Fancy Breadcrumbs
I’ve been working on converting Control Center to Rails 3, and in the process I’ve been adding new features. One thing I’d like to add is breadcrumbs, but I’ve got a personal rule that Control Center shouldn’t use any images for its layout, just to keep installation super simple.
After a bit of thinking I was reminded of Chris Coyier’s Shapes of CSS again, and...
1 tag
Exploring Ruby: The Numeric integer? Method
I noticed the integer? method on Numeric while reading through the docs. It’s not that interesting a method in and of itself, as it just returns true or false depending on whether a numeric is an integer, but one thing I thought of while looking at it is that I usually don’t check if something’s an integer, I usually just convert it to an integer with to_i and work with it. That got me...
1 tag
Checking for not nil
I don’t know why, but I’ll always get goofy when I’m testing if something’s not nil. I hate the idea of checking it like this:
if something
...
end
Then, for whatever reason, I remembered that I can use unless!
unless something.nil?
...
end
I’ve been programming Ruby for 5 years and I still forget to use unless. Pitiful.
1 tag
Factor Out Email Addresses
When you start working on your web app, you’ll have a few spots where you type in your company’s email address. You’ll probably have it as contact link on your contact page and a few other spots throughout the site, like in your mailers. As your site grows, though, you won’t realize that you’re continuing to type it out. Then you’ll hit what I hit this morning,...
1 tag
Exploring Ruby: The Numeric abs2 Method
This is a really simple method. It returns the square of the number it’s called on.
2.abs2 => 4
I thought it was interesting, though, that it’s called abs2 rather than square or squared. I couldn’t find any notes about why it was named that way. Does anyone have any info or ideas on why that might be?
1 tag
Exploring Ruby: The Numeric fdiv Method
fdiv is a pretty useful little method. It lets you divide integers but get a Float as the result. Here’s an example:
1 / 2 => 0
1.fdiv(2) => 0.5
Here’s a hangup I’d have on using fdiv, though. Pretty much everyone I know uses to_f on one of the numbers to cause the result to be a Float instead, so I think in general that’s more readable since pretty much every Rubyist knows and uses that...
1 tag
Exploring Ruby: The Numeric coerce Method
I’ve meant for a long time to work through a lot more of the Ruby core library and standard library to beef up my knowledge of those libraries. I’ll see people using methods on pretty standard objects (like Numerics and Enumerables) that I don’t know, and that always bothers me.
I figured since I blog almost as much for myself as for anyone reading this that I’d log some...
2 tags
ace: A Static File Server Written In Node.js
Last night I was working on a bit of front-end code that needed to be running on a web server, so I went looking to see if I could find a node.js based static web server. I ran across ace, written by Alex MacCaw, which does exactly what I wanted. If you have node.js and npm installed you can install ace by running the following command:
npm install -g ace
Once you have ace installed you can start...
JavaScript Existential Operator
I love me some JavaScript, but checking for whether or not values are defined can be a real pain. That’s why I was super excited to hear Jeremy Ashkenas mention a discussion around a JavaScript existential operator in the video on CoffeeScript that I linked to a few days ago. I found this email thread on the es-discuss mailing list that talks a bit about the idea of an existential operator....
1 tag
Mail Templating Services: Not Worth The Effort
I’ve worked on two different mail templating services in my career. What do I mean by mail templating service? Well, in both the instances where I’ve used them they’ve been services that let a person define emails in a web interface with some sort of templating language. When an email is sent from your web app you send the underlying data for the email to the service, the service...
2 tags
Question: Proper JSON Property Naming
Different languages have different naming conventions. For example, in Ruby I would name an attribute on an object with underscores, like:
object.some_attribute
But in JavaScript, I would use camel case for property names, like:
object.someAttribute
When I’m working with JSON, though, I always feel at a loss as to the best naming convention to use. Camel case feels more natural when...
1 tag
In Lion, don't think of apps 'spacially'
I’ve had a hard time with OS X Lion, mainly because Spaces has changed pretty drastically. I used to organize my spaces by topic (web, code, and communication) and would configure apps to appear on the appropriate space. With Lion I could do similar, but I also have these great new full screen apps. The full screen apps are awesome when you’re in the app, but they’re hard to deal...
Anonymous asked: How long have you been developing web applications?
Anonymous asked: Hi, may i know what programming language that u learn first and eventually transalte to ruby and javascript? and may i know how do u learn programming? i mean read from book/ website or source code?
Other than that, did u always immediately go learning something that are new and hot in web industry? for example, nodejs been hot lately, did u join the nodejs immediately or just dont care it...
Other than that, did u always immediately go learning something that are new and hot in web industry? for example, nodejs been hot lately, did u join the nodejs immediately or just dont care it...
1 tag
I really enjoyed this talk on CoffeeScript by Jeremy Ashkenas. I’ve been using CoffeeScript for a ton of the work I’ve been doing with JavaScript lately, and it’s been a real joy to use.
Asserting Myself
I use test/unit (or minitest in Ruby 1.9), mostly because it does what I need it to and RSpec has taken more than a couple of minutes to pick up every time I’ve tried to pick it up. That said, one thing that gets me pretty frequently is the assert assertion. assert lets you assert that something is true:
assert something_true
assert !something_not_true
The problem for me comes in when I...
Classes Don't Have To Be Models
For some reason, when I’m working on Rails apps I get tunnel vision and forget that I can write normal classes that aren’t ActiveRecord models. These are things like:
Classes that do interesting things with my ActiveRecord models but that don’t have much to do with the database beyond that.
Classes that encapsulate complicated logic.
Classes that help out with workflows.
...
1 tag
Code Golf: Streaks
I’ve got an algorithm that looks at an enumerable and figures out how many items at the front of the array are true. Here’s my implementation:
def streak(items)
items.find_index { |a| !a } || items.length
end
Anyone have an even better algorithm? I’d especially like something that’s easier to understand but still really concise.
I really like the subtle header animation that happens on the Strobe site header as pages load. Strobe’s invite only, but you can find out more about what they’re up to on their site.
Adding Search with IndexTank
I published a post called Adding Search with IndexTank on Think Vitamin this morning. I’ve been super impressed with IndexTank’s service and love not having to think about the system administration side of search indexing. I’d definitely recommend checking them out.
August 2011
16 posts
Lonely Hackers
This past weekend I went to the Ruby Hoedown 2011 and had an amazing time. I love getting the chance to hear really great talks about interesting programming topics, but I even more love just getting the chance to spend time talking to other developers about how they’re writing apps and working with their teams.
As I normally do, I rode part of the way to and from the Hoedown with Tommy...
Hoedown 2011 Slides - Handlebars.js!
I spoke about Handlebars.js at the 2011 Ruby Hoedown. I think the talk went pretty well - I always just kind of black out and have no clue what happened during talks, but I do know that we had some really great discussion about the future of the front end and when it makes sense to use traditional views versus mostly scripted views. Cool stuff!
If you’re interested, here are the slides for...
Profiling Your Tests
When my Rails test suites are running slow I like to toss this code into test_helper.rb:
class ActiveSupport::TestCase
setup do
@timer = Time.now
puts "Starting #{@method_name}"
end
teardown do
time = (Time.now - @timer)
puts "Ending #{@method_name}: #{time}"
end
end
and then run it with the following command line:
bundle exec rake test TESTOPTS="-v"
That gives you a...
1 tag
Ruby's Flexible - Don't Modify Existing Files (or...
One of Ruby’s finer points is that it’s amazingly flexible. Found some functionality in a library that you’re not quite satisfied with? Patch it! Missing a feature in a different library? Add it.
Like Spider Man says, though, with that power comes great responsibility. If you’re going to modify some code, do it in your own files and folders, not the files and folders of...
1 tag
The Importance of Learning the Ruby Core Library
A lot of times I’ll notice code that would be way more concise and easily written if the developer had just known a bit more of the Ruby Core Library at the time they wrote the code. That includes myself. The other day I stumbled across this beauty.
number.to_s.gsub(/-/, "").to_i
Know what that’s doing? It’s turning a number into the non-negative version of itself - i.e. finding...
1 tag
integer_to_currency
It’s usually recommended that you deal with money in Rails apps as an integer, and then just divide by 100 any time you’re displaying it. It gets pretty old to write a ton of helper methods on models to do that, even if they are really short, so I usually just toss this helper into my apps application_helper.rb to help out:
def integer_to_currency(amount_in_cents)
...
1 tag
Rails Related Bash Aliases
I have a few aliases defined for bash that help me save a few keystrokes when working on projects. I figured they’re worth sharing:
# run unit tests
alias rtu="bundle exec rake test:units"
# run functional tests
alias rtf="bundle exec rake test:functionals"
# run recent tests
alias rtr="bundle exec rake test:recent"
# run all tests
alias rt="bundle exec rake test"
# shortcut for bundle...
1 tag
Functional Testing User Agent Strings with Rails
It took me a few minutes to figure this one out, so I figured I’d share it. If you’re having to do user agent detection in an action for some reason, and need to test that action, you can set the user agent string with @request.user_agent = "User Agent String" Here’s an example:
test "that android phones get redirected to tiny_video.mp4" do
@request.user_agent = "Mozilla/5.0...
…also known as Obamacare. I have no problem with folks saying Obama cares....
– Barack Obama, throwing down the schoolyard diss
My friends and I have been coddled long enough by a billionaire-friendly...
– Warren Buffet, Stop Coddling the Super-Rich
Avoiding data detectors in iOS's Mail.app
One problem I’ve been trying to figure out for a while is how to get data detectors not to take effect in Mail.app for HTML emails I’m sending from Hopefully Sunny. They’ve been on my nerves for a while because data detectors have been making the forecast emails ugly by linking days of the week and words like Today and Tomorrow. Here’s an example of what was happening:
...
You can’t safely spill sunscreen or wine on an electronic device, or get sand in...
– From Why I Hate The Kindle
Pure CSS Badges
When I saw Chris Coyier’s (of http://css-tricks.com fame) talk at ConvergeSE this year and heard him talk about CSS shapes, I immediately realized that you could do goofy little badges purely in CSS.
I whipped up a 12 pointed badge and an 8 pointed badge. Something feels kind of off about the 8 sided one to me, though, so I definitely think the 12 sided one is my favorite.
Here’s...
We had debt at 120% of GDP, far higher than this, after World War II, and no one...
– Warren Buffet, from an interview with CNBC
Pessimism is for losers.
– Bought some US stocks by Rob Delaney
Alfred Extensions
I’ve been in love with Alfred, the neat little app launcher for Mac, for a while. They recently added the ability to write AppleScript extensions for Alfred, and I also recently saw that Rdio’s desktop app supports AppleScript, so I had to pair the two together. The results came out pretty well! Download the extensions below, unzip them, and then double click each of them to...