Why does Rails do utf8=✓

I noticed Rails apps always does utf8=✓ in their URLs. Rails at one point of time even placed a snowman unicode glyph. Here’s what Yehuda Katz has to say on this regard:

This parameter was added to forms in order to force Internet Explorer (5, 6, 7 and 8) to encode its parameters as unicode.

Specifically, this bug can be triggered if the user switches the browser’s encoding to Latin-1. To understand why a user would decide to do something seemingly so crazy, check out this google search: http://www.google.com/search?sourceid=chrome&ie=UTF-8&q=diamond+with+a+question+mark+in+it. Once the user has put the web-site into Latin-1 mode, if they use characters that can be understood as both Latin-1 and Unicode (for instance, é or ç, common in names), Internet Explorer will encode them in Latin-1.

This means that if a user searches for “Ché Guevara”, it will come through incorrectly on the server-side. In Ruby 1.9, this will result in an encoding error when the text inevitably makes its way into the regular expression engine. In Ruby 1.8, it will result in broken results for the user.

By creating a parameter that can only be understood by IE as a unicode character, we are forcing IE to look at the accept-charset attribute, which then tells it to encode all of the characters as UTF-8, even ones that can be encoded in Latin-1.

Keep in mind that in Ruby 1.8, it is extremely trivial to get Latin-1 data into your UTF-8 database (since NOTHING in the entire stack checks that the bytes that the user sent at any point are valid UTF-8 characters). As a result, it’s extremely common for Ruby applications (and PHP applications, etc. etc.) to exhibit this user-facing bug, and therefore extremely common for users to try to change the encoding as a palliative measure.

All that said, when I wrote this patch, I didn’t realize that the name of the parameter would ever appear in a user-facing place (it does with forms that use the GET action, such as search forms). Since it does, we will rename this parameter to _e, and use a more innocuous-looking unicode character.

Very funky although this has since become my standard way of determine if the application is running on Ruby on Rails.

Saving Resque

Open source project Resque needs some volunteers:

Rescuing Resque: Let’s do this

Anyway, Resque has been psuedo-abandonware for a while now. In January, Terence Lee from Heroku got in charge of the project, but it’s a big job. Too big of a job to properly manage alone. It’s hard to sift through 60 pulls and 120 open issues, some of which have been open for a few years. And manage the 1.x line while working on new stuff for 2.0. And debug issues that basically boil down to “When I upgrade Resque and this random gem, everything breaks, but when I downgrade that other gem, it works again. But Resque throws the error.”

So Terrence gave a presentation at Frozen Rails, and in it, he outlined what needs to be done for Resque 2.0, and asked for some help getting it out the door. So myself and a few other people are gonna pitch in and help out, and we’d love to have you.

Resque is a Redis-backed Ruby library for creating background jobs, placing them on multiple queues, and processing them later. You can contribute through GitHub.

How to install mysql2 gem in Ubuntu

Got an error message when you install the mysql2 gem in Ubuntu? Before you install mysql2, make sure you have libmysqlclient-dev installed or you’ll get the following:

[code lang=”bash”]kahwee@kahwee-desktop:/$ gem install mysql2
Building native extensions. This could take a while…
ERROR: Error installing mysql2:
ERROR: Failed to build gem native extension.

/home/kahwee/.rvm/rubies/ruby-1.9.2-p0/bin/ruby extconf.rb
checking for rb_thread_blocking_region()… yes
checking for mysql_query() in -lmysqlclient… no
checking for main() in -lm… yes
checking for mysql_query() in -lmysqlclient… no
checking for main() in -lz… yes
checking for mysql_query() in -lmysqlclient… no
checking for main() in -lsocket… no
checking for mysql_query() in -lmysqlclient… no
checking for main() in -lnsl… yes
checking for mysql_query() in -lmysqlclient… no
checking for main() in -lmygcc… no
checking for mysql_query() in -lmysqlclient… no
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers. Check the mkmf.log file for more
details. You may need configuration options.

Provided configuration options:
–with-opt-dir
–without-opt-dir
–with-opt-include
–without-opt-include=${opt-dir}/include
–with-opt-lib
–without-opt-lib=${opt-dir}/lib
–with-make-prog
–without-make-prog
–srcdir=.
–curdir
–ruby=/home/kahwee/.rvm/rubies/ruby-1.9.2-p0/bin/ruby
–with-mysql-config
–without-mysql-config
–with-mysql-dir
–without-mysql-dir
–with-mysql-include
–without-mysql-include=${mysql-dir}/include
–with-mysql-lib
–without-mysql-lib=${mysql-dir}/lib
–with-mysqlclientlib
–without-mysqlclientlib
–with-mlib
–without-mlib
–with-mysqlclientlib
–without-mysqlclientlib
–with-zlib
–without-zlib
–with-mysqlclientlib
–without-mysqlclientlib
–with-socketlib
–without-socketlib
–with-mysqlclientlib
–without-mysqlclientlib
–with-nsllib
–without-nsllib
–with-mysqlclientlib
–without-mysqlclientlib
–with-mygcclib
–without-mygcclib
–with-mysqlclientlib
–without-mysqlclientlib

Gem files will remain installed in /home/kahwee/.rvm/gems/ruby-1.9.2-p0/gems/mysql2-0.2.6 for inspection.
Results logged to /home/kahwee/.rvm/gems/ruby-1.9.2-p0/gems/mysql2-0.2.6/ext/mysql2/gem_make.out[/code]

To resolve it, install libmysqlclient-dev:

[code lang=”bash”]sudo apt-get install libmysqlclient-dev[/code]

And now you can install mysql2 successful:

[code lang=”bash”]kahwee@kahwee-desktop:/$ gem install mysql2
Building native extensions. This could take a while…
Successfully installed mysql2-0.2.6
1 gem installed
Installing ri documentation for mysql2-0.2.6…
Installing RDoc documentation for mysql2-0.2.6…[/code]

Great!

[I tried this on Ubuntu 10.10.]

Ruby on Rails and Merb will merge

Yay. We always see opensource branch and branch. Rarely do we see them come together.

Merb is an open, ever-changing project, and some its best ideas have come from not-core regular-Joe community members. It’s gotten where it has because of the community, and the community will get us even further in the future. Your ideas, feedback and even complaints will be 100% welcome in the future, just as they have been in the past. I believe in the tremendous value an open community and just generally open attitude bring to the table, and am counting on those things to continue ushering in the future of Ruby.

On to the news: beginning today, the Merb team will be working with the Rails core team on a joint project. The plan is to merge in the things that made Merb different. This will make it possible to use Rails 3 for the same sorts of use-cases that were compelling for Merb users. Effectively, Merb 2 is Rails 3. (Source: Yehudakatz)

I don’t use Rails by the way. I use more of CakePHP. But anyway, I think the merger is a good direction. Of course this is arguable since the merger would mean one less competing Rails framework. But this is software, not business. Merger means greater understanding. I believe there are many ways of doing one same thing, but one way that would be most efficient in most given situations and that’s why people build frameworks.

Want to install god?

I love it when a software has a good and catchy name but I love it more when a software has a name that looks funny in the a Linux install command:

[code]$ sudo gem install god[/code]

A Better Way to Monitor

God is an easy to configure, easy to extend monitoring framework written in Ruby.

Keeping your server processes and tasks running should be a simple part of your deployment process. God aims to be the simplest, most powerful monitoring application available.

God is useful, learn more about god.

How to do a parseInt in…

Recently, due to my Java class, I’ve been typing parseInt() in my PHP code instead of intval().

Here is a compilation of all sorts of ways to parse string into integers:

PHP

[code lang=”php”]var $num = intval("0001");[/code]

JavaScript/ActionScript

[code lang=”javascript”]var num = parseInt("0001", 10);[/code]

Java

[code lang=”java”]int num = Integer.parseInt("0001");[/code]

Apparently you need to type really long to parse stuff in Java.

Python

[code lang=”python”]num = int("0001")[/code]

Ruby

[code lang=”ruby”]num = "0001".to_i[/code]

Now that I suddenly remember them all, I decide to quickly write them all down. Because the next moment, I’ll forget them or mix them up again. Please correct me if I am wrong.

The disadvantages of Media Temple as your host

I’m losing patience with Media Temple. I pay around 20USD per month for Media Temple’s Grid-Service and I’m starting to wonder if it’s just some sort of a marketing thing. They made it sound so reliable and blah blah but in reality it’s feels kinda slow. Media Temple serves huge files pretty quickly. Unfortunately, when it comes to responding to requests, they seem slowly than other web hosts.

Anyway I filed a support ticket complaining that their database seemed problematic.

My support ticket on 2008/03/31 11:28

I get this Error establishing a database connection.

And I can’t access phpmyadmin, it says too many connections. What’s happening?

The database seem to be problematic…

By the way, Firefox’s dictionary suggested ‘phpmyadmin’ to be corrected to ‘badminton’. And this is the reply I get from them. Basically, they’re trying to get me to pay more money again.

Support response on 2008/04/01 20:39

Sorry for any inconvenience that you may have experienced by this. We also apologize for any lack of information provided to you. I’ll try to explain the best that I can.

Essentially you are in a shared server environment. The (gs) Grid runs off of a cluster of servers and provides resources to multiple users at once. In that, you also share database servers and the resources provided. When a “Bad Neighbor” performs some sort of unauthorized action, such as running scripts or queries that are configured to run for long periods of time and consume large amounts of resources.

When you are receiving the “too many connections” error, this means your are experiencing the “Bad Neighbor Affect”. Meaning that someone else has caused the server to reach a high load. When this happens, the database is isolated and that particular “Bad Neighbor” is bumped off of the database server and notified of their actions. During this time is when you are receiving the error. Once everything is resolved the database is restored and accessible again. Continue reading “The disadvantages of Media Temple as your host”

How much time I spent on reorganizing my computer

Okay, as I was saying, my computer’s getting kinda cluttered and things are just getting harder and harder to find. I use Windows Vista Business, I have multiple copies of Apache, MySQL, Ruby, PHP and other this sort of programs in my computer. If you don’t know what they are, you probably wouldn’t like my blog, maybe you’d be better off at BoingBoing or watching some inspiring videos at TED.

The problem with having multiple copies is that you don’t know when to launch what. Every time I want to – say – launch MySQL, I start thinking really hard which folder should I go into. And when my mind is kinda switching off I often find my mouse pointer moving towards Safari browser icon and visiting Digg.com. Counter-productivity at the very best or worst – you decide.

I always tell myself, okay, next time when I re-setup my computer I’ll do this and that and so on… Each time I re-setup my computer, I always find a flaw with my existing setup. I will restructure my folders again, partition things differently this time. And I always have this optimistic thought that this time I’m really getting it right.

I rarely do. Two months later I would look back at my system thinking of new ways to refine my setup for the future.

Maybe the problem isn’t with me. Maybe the problem is with just the way computers are. All these labyrinth-like hierarchy of folders, it’s more messy than my family tree. Some day, we should kill the ideas of folders, they’re a good way of organizing things but today with people storing more and more files, the concept of folders is hardly a good idea.