#sublimetext

Sublimetext2 Build System

Sublimetext2 ships with its Build System. You can run a ruby file by Cmd + B (or through Tools -> Build menu).

One thing bothers me is you have to SAVE that file first, you can’t run “untitled” ruby code even you specified which build system (language) to use. This was the reason I still keep Textmate in my Mac, thanks to this plugin called Anypreter I can finally say goodbye to Textmate.

About Anypreter

You can install it through Package Control. It supports PHP, Python and Ruby.

After specify the language (suppose you haven’t saved it), Ctrl + Shift + X to run and check the result. Rightclick in the document works too.

Futher Reading

#pow #rails

In your rails app, when you need an envionment variable like ENV['TWITTER_CONSUMER_KEY'] for local deveopment, where do you put them? Simply set them before you start your rails server as a one time thing, or just put them under ~/.profile or ~/.zshrc?

Well it works but I’m not happy with that. Because first it belongs to a specific project and expose them to global env make me feel a little bit uncomfortable, second, what if you happend to have more than one twitter integrated app, how do you name the variables to solve naming collision?

If you’re using Pow, there is a perfect solution for you.

.powrc and .powenv

Pow provides these 2 files for you to config pow and setup any environment variables.

Before an application boots, Pow attempts to execute two scripts — first .powrc, then .powenv — in the application’s root. Any environment variables exported from these scripts are passed along to Rack.

Convention here is putting .powrc under git version control, and override or setup any project specific environment variables to .powenv.

Let’s do it

```diff .gitignore

  • .powenv ```

```ruby .powenv export TWITTER_CONSUMER_KEY=foo export TWITTER_CONSUMER_SECRET=bar


BTW, you must run this command to restart pow manually so these scripts will be loaded.

$ touch ~/.pow/restart.txt ```

Pow Document: Customizing Environment Variables

#diary

Finally migrated all my posts from Wordpress to Octopress. I think it’s quite popular among hackers and geeks these days, if you don’t know it just google it, tons of results.

There’re still a lot of works to do, like some posts with messy format or broken links, Disqus comments need to be migrated, categories are nonsense etc, but I’ll announce this anyway. Done is better than perfect right?

I’m not gonna mention any migration details here, I’d like to look back and have a summary or something now.

Over 300 posts since 2009/10/07

I’m really surprised by myself. I was not aware of that I’ve done so many writings, and these’re only published ones(I have a lot of drafts). Even though many of them are just simple code gist and programming related.

It’s so easy now to “execute” an action to a resource. Like a photo, favourite a tweet or even +1 a youtube movie. So our foot prints or activities are spread everywhere and some of them might be important or meaningful to you, that just a light-single-built-in action is not enough. I want to keep those non-physical, intangible but essential “things” here, as part of my life.

Japanese, English and Chinese

There was a time I was trying to write down every single post in these 3 languages. I believe the plugin I used in Wordpress is called qTranslate, which let you write with each language or all of them.

After a while I noticed it was too hard for me. Translating same contents into a different language is boring and wasting of time, at least I tried and found out it doesn’t work for me. So this time, I want this blog to be an English mainly blog, with occasional different language posts. My plan is writing emotional feelings in Chinese, and social activities or something related to Japan in Japanese.

I’m still keeping those language mixed posts here even though I think it’s not useful to my “readers”, not sure how many I got, I’d like to take those posts as old-days foot prints. “あの時がんばったな!ー” hehe.

About the priority or weight of each language

I want to be a top player in this programming field. In order to achieve that English is a MUST condition. I still remember when I first time read a programming book in English, it was so difficult that I hardly understood the meanings. But I keep trying and trying, now I’m very comfortable with it and believe it’s more natural than any other language in programming field.

Besides the effort I put in English, I also want to start writing in my native language, Chinese. Although English is more friendly to me than before, it’s still difficult to describe my feelings and emotions precisely. I need more vocabularies. I need to look back to think about if there is a grammar mistake. So when I think about some topics such as meanings of life, or what I want to left to the world, am I making this world a little bit better, etc. In these cases I need to grab my most familiar weapon :)

For Japanese, hmmm.. I’ll leave it for now :)

Google Adsense

If you’re an old readers of me, I hope you are, you may notice that there’s no ads anymore. Yeah I used to put those ads for experiment. With 500-600 daily PVs, it gave me around 4,000¥ every month. Not that much but it’s better than nothing, so I kept ads for a long time. But to be honest, as a personal blog it’s just annoying. I didn’t like it personally, and it’s for sure it gives people bad impression, I don’t like to miss a job opportunity if some head hunters open my blog and leave with a “WTF?!..”.

Let’s just be simple and clean.

That’s it~ see you next time.

#themes

Just found out a theme called - SOLARIZED.

And actually it supports a lot of Editors and terminals, just go to its website.

Now my iTerm looks like this:

iTerm with SOLARIZED theme

I don’t say it’s fancy but if you’re a little bit tired of default theme, you may want to try out something new. And changing themes or wallpapers regulary usually makes me feel better :)

You can find more iTerm themes here

#rails

Yesterday I upgraded one of my old-pending-hobby projects to latest Rails version, which is 3.2.8 now. Since I have several other projects in old version, I’d like to keep a note for what I’ve done.

As you can see in my commit, all you have to do is update the version in Gemfile and hit bundle update in your terminal.

```ruby Gemfile gem ‘rails’, ‘3.2.0’

group :assets do gem ‘sass-rails’, “ ~> 3.2.3” gem ‘coffee-rails’, “~> 3.2.1” gem ‘uglifier’, ‘>= 1.0.3’ end


Then add new configuration to `development.rb` and `test.rb` environment file. That's all :)

```ruby config/environments/development.rb
  # Log the query plan for queries taking more than this (works
  # with SQLite, MySQL, and PostgreSQL)
  config.active_record.auto_explain_threshold_in_seconds = 0.5

  # Do not compress assets
  config.assets.compress = false

```diff config/environments/test.rb

  • Allow pass debug_assets=true as a query parameter to load pages with unpackaged assets

  • config.assets.allow_debugging = true

  • Raise exception on mass assignment protection for Active Record models

  • config.active_record.mass_assignment_sanitizer = :strict ```
#rails

The order may different

Suppose you’re retrieving records using array of ids.

ids = [100, 1, 6]
User.where(:id => ids).map(&:id)
# => [1, 6, 100]

The order of the data may different from ids array, what if you want to keep its order?

for MySQL

There is a function in mysql called FIELD()

FIELD(str,str1,str2,str3,…) Returns the index (position) of str in the str1, str2, str3, … list. Returns 0 if str is not found.

So by combing this FIELD() function, here is the code.

ids = [100, 1, 6]
User.where(:id => ids).order("field(id, #{ids.join(',')})").map(&:id)
# => [100, 1, 6]

This will generate SQL below:

SELECT `users`.* FROM `users` WHERE `users`.`id` IN (100, 1, 6) ORDER BY field(id, 100, 1, 6)

for other databases

I didn’t dig too much but most of google results suggest sort it manually using ruby. Here is an example:

ids = [100, 1, 6]
users = User.where(:id => ids)
users = ids.map {|id| users.detect {|user| user.id == id } }
users.map(&:id)
# => [100, 1, 6]