October 29, 2013 #ruby

Problem

$ rvm install 1.9.3

Searching for binary rubies, this might take some time.
Found remote file https://rvm.io/binaries/osx/10.9/x86_64/ruby-1.9.3-p448.tar.bz2
Checking requirements for osx.
Installing requirements for osx.
Updating system - using Zsh, can not show progress, be patient...
Error running 'requirements_osx_brew_update_system ruby-1.9.3-p448',
please read /Users/qihuan-piao/.rvm/log/1383014621_ruby-1.9.3-p448/update_system.log
Requirements installation failed with status: 1.

Solution

$ gcc -v

Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn)
Target: x86_64-apple-darwin13.0.0
Thread model: posix

rvm install 1.9.3 --with-gcc=clang

Reference

http://stackoverflow.com/questions/8139138/how-can-i-install-ruby-1-9-3-in-mac-os-x-lion

July 14, 2013 #heroku #cache

TL;DR

  • Heroku doesn’t provide HTTP caching by default
  • Use CDN like Cloudflare or Amazon CloudFront
    • Clouldflare has free plan, takes you less than 3 minutes to setup.

HTTP Caching

Just a quick example of HTTP caching.

HTTP caching example

You can really feel the difference after you enabled it. Since most of your static assets, like js, css and image files, are gonna use browser cache without hitting your rails application, the user experience is improved a lot.

In my hobboy project quoty.me, user had to download the black colored background image(22k) every time they visit a page, so there was a time lag to notice the background changed from white to black, which bothered me a lot. And that was why I noticed the HTTP caching is not enabled in Heroku.

Heroku’s HTTP Caching

Heroku doesn’t provide HTTP caching by default. In order to take advantage of HTTP caching, you’ll need to configure your application to set the appropriate HTTP cache control headers and use a content delivery network (CDN) or other external caching service.

I’m using cedar stack in Heroku, but I have to use “external”(oppose to add-ons or solutions provided by Heroku) stuff to make HTTP caching work.

I’ve also tried Rack::Cache with Memcache but somehow it didn’t work for me.

Setup Cloudflare

This time I decided to give Cloudflare a try. It’s free, easy to setup, and just work.

I’ll cut off how to setup Cloudflare, as the tutorial in its website is realy excellent. Just go to https://www.cloudflare.com/ and follow the instructions, within 3-4 steps you’re done.

The other thing you have to do is to update your nameserver to the one provided by Cloundflare, like “IAN.NS.CLOUDFLARE.COM”. I’m using GoDaddy for my quoty.me domain, the link to setup nameserver looks like this.

GoDaddy Nameserver setup link

Further Reading

June 1, 2013 #testing

The talk

Let's Make Testing Fun Again at WindyCityRails 2012 | Lanyrd

Communication

Tests give you about the code. This happens over time, where you find that tests have become hard to write, the tests are becoming complicated and bogged down.

Rather than use it as an opportunity to blame the test or to blame testing, you should use it as an opportunity to learn something about your code, that there is a dependency that could potentially be simplified, or something you could be doing better.

Keep relevant setup close

This is something that "relative good"(in terms of "absolute good"). It's generally good to keep the setup of your tests close to the tests where it is.

What you want to avoid is the case where some sort of before setup way at the top of the file and you have stuff, and more stuff, and more stuff, and eventually in the end, what's the @user.name? I don't remember.

An Example


describe User do
  before do
    @user = Factory.build(:user)
  end

  # and more stuff

  # and more stuff

  it "uses the user" do
    @user.name.should == # i don't remember
  end
end

This is where you see sometimes people say "You should never have setup outside your original test".

What to do about duplications and complicate shared setups

I think a lot of times if you need a complicated setup to test code, that often means your code is too complicated you need to start working out to move your dependencies. I tend to have a higher tolerance for duplication in my tests than I do in my regular code, specifically because I'm more interested in having the communication close, and less interested in maybe being clever about extracting setups in my tests. Sometimes I do if there's a piece of stuff that's really tightly coupled, and I can give it a descriptive enough name that I still feel like I have the benefit of keeping the setup close in the actual test.

Test Simple Values

By using literal in check, when the spec fails, the error message is much easier to understand.

Spy, don't mock

A jasmine
test example.

var cheeseburger = {
  cheeses: function() {
    // Ajax call to cheese server
  }
};

it('spies on the cheese server', function() {
  spyOn(cheeseburger, 'cheeses');
  cheeseburger.cheeses();
  expect(cheeseburger.cheeses).toHaveBeenCalled();
});

  • Readability advantage
  • Easy to find where actually fails

BTW, if in spec, we need write like this:


it 'spies on the cheese server' do
  # in the reversed order
  cheeseburger.should_receive(:cheeses)
  cheeseburger.cheeses
end

More about Spy on Ruby and Rails

thoughtbot/bourne gem can let you have the rspec-like test spies syntax.

Spy vs spy, good explanation on what is test spy the benefit of it.

What Do We Love?

These are quite frank and interesting questions.

  1. Do we still love writing tests?
  2. Or do we just love having written tests?
  3. Or do we just love saying that we've written tests?
May 30, 2013 #testing

Recently I got lots of inspirations and thoughts on how to write good tests. Thanks to Cookpad, the company I’m working on, I get the chance to work on a very large scale rails app. But it also introduces me to some very bad tests. Sometimes I found myself so difficult to add any spec, as it needs lots of extra efforts to make the fixture data, also one model got too many dependencies and collaborators, etc. I know it’s wrong, the code smells, just don’t know where to start.

So when I watched this video “Fast Test, Slow Test” by @garybernhardt, who also runs DestroyAllSoftware screencast, I found it very helpful and wanted to take some notes on it.

Three Goals in Testing

  • Prevent Regression
  • Prevent Fear
  • Prevent Bad Design

How to Fail in Testing

  • Selenium as primary testing
  • “Units Test” are too large
  • Fine-grained tests around legacy code

What you’re saying here is that we acknowledge that this code sucks, so we’re going to go in and write tight tests around it that solidifies interface and just bake the badness forever. This is the worst way to do unit testing, go in your legacy system and write test around bad code.

Too many dependencies in test

You end up with a test suite where it tends to either completely succeed or completely fail. It doesn’t give you any fine-grained feedback about what has actually gone wrong it just tells you you broke something. And you are left to dig these stack traces. In the ideal test suite you don’t have to dig through stack traces because ideally once test fails it will tell you exactly was broken (of course you never achieve that)

More than 8 lines for a model test

I would ask myself why do I need to setup so much of the world to test this one small piece of behavior. That causes me to decompose it which causes better system design.

10% System/Acceptance Test, 90% Unit Test

(in terms of test cases, not lines of code)

That applies to mostly object heavy system like web app, that have a lot of logic and not a lot of boundaries.

Advice on models that very tied to database

Question

If you're working with model objects that they're very tied to the database specifically things like you have to save that one object before you can associate with the other object and that means hitting the database. What's your advice in terms of writing fast unit test?

Answer

Take all of that behavior that's on your model objects and pull out of their into service classes that are stateless, that interact with the model objects. So view calls a service, service contains the intelligence and the service manipulates the model objects.

You can still have methods on your model objects do things like wrap specific queries in class methods, or wrap specific mutations when multiple fields are commonly manipulated together.

It's okay to have those on your model object and then have the service use those but if you pull the behavior of the system out into the service, and you control the boundary between the service in the model by those methods you've written on the model, then you can mock out more easily and safely.

May 19, 2013 #inspiration

Yesterday I was trying to catch up with the Google I/O 2013 to see what’s new. After I read some wrap up articles like Everything announced at the Google I/O 2013 keynote in one handy list - The Next Web, I was very intrigued and ended up watching the whole 3.5 hours long keynote video.

Given that I’m not an Android user or developer, I still felt the improvements on Android system quite impressive. But my biggest inspriation is not on any specific topic, it’s how Google thinks of solving problem in the terms of technolodgy, not just web or mobile, they always think in a big picture. Much bigger than I do.

I’ve been a web developer for over 5 years and now I do feel I’ve got a handy toolkit to solve some problems. But I also feel I’ve constrained myself to this area, limited my imagination. Anything that can’t solve with the skills I’ve got is impossible. Web is bigger than I perceived, and yet it is just a part of technolodgy. We all know that it’s just a tool to solve problems, not the only way to do it.

I need to think out of the box. It’s too comfortable to just sit there and see their innovations, instead of trying to be the guy on the stage to amaze people with your innovations. I’ve spent less and less time on predicting the future of the technolodgy, but more and more on reality issues, like where to live, when to get married or so. I guess that’s the inevitable trend as you getting old but at least I shouldn’t let what I’ve learned blind myself. Keep looking, don’t settle.

I’d like to end this post with the quote of Steve Jobs.

Stay hungry, stay foolish

May 16, 2013 #apple

I gave my iPad to my mom when I went back to China during the golden week, so I’m wondering if now is a good timing to buy a new iPad? Are they going to release a new model soon? “Buy or wait”, that’s the only options for me every time when I think about any product of Apple.

Macrumors Buyers Guide

Then I found this buyersguide by Macrumors. I know Macrumors website for a long time and used to subscribe to their RSS feeds, but I never noticed they have a this website.

Macrumors Buyers Guide

Here you can see each Apple model with red or yellow round buttons. I guess there’ll be green ones once the “timing” is right to buy that product. As it says:

provide our best recommendations regarding current product cycles, and to provide a summary of currently available rumors for each model. This page is based on rumors and speculation and we provide no guarantee to its accuracy.

When to Buy a New iPad

If you click the iPad link you’ll see this:

When to Buy a New iPad

You can see the last release was October 23, 2012. Average
release cycles is 311 days so statistically there are still around 100 days left, but judging from the recent releases it could be next month! Recent rumors might be helpful, to at least give you something to fill in the blankness of waiting.

Maybe this time I’ll give iPad mini a try.

The Photo: Apple 2005-2013

Apple 2005 and 2013

2005. I was still in college. The phone I had was just a “pure” phone to call and text people, and even didn’t have a camera.

2013. Now I have Macbook, iPhone, iPad, Kindle, all “rich” devices. With 3G network I can connect to Internet anytime anywhere. Life’s been changed so much.

The Video: Samsung GALAXY S4

I never had a Samsung GALAXY phone, but after watched this video I was surprised by some of the new features. I’ve been using iPhone for 3 or 4 years, I’m so used to it and sometimes it makes me blind to see other innovations.

The video is 4:30 long, please take a look when you have time.

February 23, 2013 #details

As you see below, this was how my blog header looks like before I write this post. There was no differece between top page(or home page, landing page) and the post page. If put into rails term, these are the #index and #show page, they’re sharing the exactly same header partial.

Thinking About My Target

Based on the data from Google Analytics, around 85% of the access are from search engines like Google or Yahoo, so for them the contents I provided should be most important.

But for the direct access to the top page, they could be my friends, could be the headhunters, my future boss or colleague, or just some random strangers, anyway these people are my target who I really want to pay more attentions to, make them impressive, show a little different things from the other audience.

A Good Example

Ryan Singer from 37signals, one day I happened to visit his blog, the moment I saw the page I knew this is one thing I also want to have in my blog!

Let’s take a look.

As you can see, the header in top page and every post page is different. Yes it’s low techy techy, easy to implement, not challenging at all, but it still matters.

Time for Action!

I’m sure every web developer understand this and every product they built must have a more beatiful and well designed landing page to catch people’s eyes. But do you apply the same principle to your own blog? You may use Wordpress or Octopress or any other blog generate tools to build your blog, and perhaps you even made a good About me page, but did you take one more step further to care about this detail? If you were aware of this and chose not to do it, it’s perfectly fine but to me, I don’t like to miss this detail.

Anyway here is my new blog header, just a simple introduction, I may add some more info later.

I guess this post may be nonsense to some of you. But if this is one thing that represent myself, at least I’d like to put more efforts to make it a little more “me”.

February 17, 2013 #details

If you browse amazon reviews chances are you already saw this Amazon Verified Purchase label. This is not a new feature, I think it’s been there for a while but I just want to point that out.

Amazon Verified Purchase

It’s easy to understand, it means the reviewer purchased that item at Amazon, could highly increase the credibility of the review.

I think it’s easy to implement, the logic is simple but it’s a good indicator for users to decide if they should follow or trust the review.

Also I tried it myself. When I click to write a review for the book I bought on Amazon, I get this option to choose if I’d like to label myself as purchased the item.

Amazon Write Review Options