Enough is enough, i seriously don’t think i’m built to use any online blogging engines. Simply because i’m built to prefer typing in my terminal, using vim, and prefer playing with terminal-interfaced stuff, instead of so called idiot-proof online tools … i guess i’m probably more idiot than idiots.
Moreover, blogspot is blocked by the great wall of china (at least this is what i’ve encountered in shanghai), and though i can continue to access it via some customized proxy settings, it is terribly slow, slow & slow. In fact, i find it amazing that i am able to bear with it for so long …
Anyway, enough is enough, here i am, back to basics, typing my blog in vim, on the engine side, i’m using the git-based blogging engine shinmun, and deploying it at heroku …
Ha, it cannot be any easier than what i have now … what a wonderful world !!
(i'm gradually do the migration to the newer blog, as and when i find time)
Thursday, August 19, 2010
Friday, August 13, 2010
Benchmarking any rake task
While writing serializable_proc, i wanted to find out how the RubyParser-based implementation fair against ParseTree-based one, here's a rake task i've written to support the following use cases:
And:
And here's the code to acheive the above:
And here's the output i get for the x2 run:
# yield benchmark results by running :spec task x5 (default)
$ rake benchmark[spec]
And:
# yield benchmark results by running :spec task x2
$ rake benchmark[spec,2]
And here's the code to acheive the above:
1 | # Benchmarking |
And here's the output i get for the x2 run:
...
(blah blah, the output of running :spec task x2)
...
===============================================
~ Benchmark Results for Task :spec ~
nth user system total real
#1 0.000000 0.000000 1.030000 1.052567
#2 0.000000 0.000000 1.020000 1.040352
===============================================
Friday, August 6, 2010
Reliably getting a formatted time string in ur desired lang
Today, i'm spent quite abit of time playing with the very simple Time#strftime:
What i want to acheive is to reliably get the chinese version:
I've tried playing with the environment variable $LANG:
Seems good, but nope, Time#strftime still gives me the same english output. I've trying recompiling a different ruby using rvm with the exported $LANG ... nope, doesn't work either. It seems that not matter how $LANG is set (thus affecting LC_TIME), ruby just doesn't care abt it. Yet, i swear that on flyhzm's machine, he has been getting the chinese version, no matter how hard he has tried to get the english version.
Battled, beaten & worn out, i've concluded that the most reliable way (and definitely what most people do), is to use the i18n. Given i already have this /path/to/i18n.yml:
The following works for mri 1.9.1:
Without modication, i got the following unfriendly output for the chinese port under mri 1.8.7, jruby & ree-1.8.7:
After much googling, stumbled across this post abt $KCODE. Here's the final modified code for the 1.8.7 equivalents:
For people doing rails, here are some useful resources:
* http://guides.rubyonrails.org/i18n.html
* http://github.com/svenfuchs/rails-i18n
1 | puts Time.now.strftime("%B %d, %Y %H:%M") |
What i want to acheive is to reliably get the chinese version:
八月 06, 2010 17:24
I've tried playing with the environment variable $LANG:
$ export LANG=zh_CN.utf8
$ locale
LANG=zh_CN.utf8
LC_CTYPE="zh_CN.utf8"
LC_NUMERIC="zh_CN.utf8"
LC_TIME="zh_CN.utf8"
LC_COLLATE="zh_CN.utf8"
LC_MONETARY="zh_CN.utf8"
LC_MESSAGES="zh_CN.utf8"
LC_PAPER="zh_CN.utf8"
LC_NAME="zh_CN.utf8"
LC_ADDRESS="zh_CN.utf8"
LC_TELEPHONE="zh_CN.utf8"
LC_MEASUREMENT="zh_CN.utf8"
LC_IDENTIFICATION="zh_CN.utf8"
LC_ALL=
Seems good, but nope, Time#strftime still gives me the same english output. I've trying recompiling a different ruby using rvm with the exported $LANG ... nope, doesn't work either. It seems that not matter how $LANG is set (thus affecting LC_TIME), ruby just doesn't care abt it. Yet, i swear that on flyhzm's machine, he has been getting the chinese version, no matter how hard he has tried to get the english version.
Battled, beaten & worn out, i've concluded that the most reliable way (and definitely what most people do), is to use the i18n. Given i already have this /path/to/i18n.yml:
1 | en: |
The following works for mri 1.9.1:
1 | require 'rubygems' |
Without modication, i got the following unfriendly output for the chinese port under mri 1.8.7, jruby & ree-1.8.7:
\345\205\253\346\234\210 06, 2010 20:27
After much googling, stumbled across this post abt $KCODE. Here's the final modified code for the 1.8.7 equivalents:
1 | require 'rubygems' |
For people doing rails, here are some useful resources:
* http://guides.rubyonrails.org/i18n.html
* http://github.com/svenfuchs/rails-i18n
Wednesday, August 4, 2010
Bitten by Array#*
Have u tried Array#* ? Its a pretty cool way to return an array, built by concatenating the original array by n times:
However, u should be aware that all elements of the new array actually points to the same instance, as proven by their object_id:
This means that:
1 | a = [1] * 2 |
However, u should be aware that all elements of the new array actually points to the same instance, as proven by their object_id:
1 | x = 'a' |
This means that:
1 | # Reassigning one element does not affect another |
Wednesday, July 28, 2010
Lazy evaluating with Enumerable#any?
Most of us know this:
For some of us who likes Enumerable#any?, this won't work:
That is because :m1 & :m2 are already evaluated before calling Enumerable#any?, to have lazy evaluation, we can do this:
For the perversive ones:
1 | class Awesome |
For some of us who likes Enumerable#any?, this won't work:
1 | # Case 2: |
That is because :m1 & :m2 are already evaluated before calling Enumerable#any?, to have lazy evaluation, we can do this:
1 | # Case 3: |
For the perversive ones:
1 | # Case 4: |
Thursday, July 22, 2010
Everyone should start writing macros
Did some cleaning & catching up of specs for railsbestpractices.com yesterday, added quite a number of spec macros to dry up stuff, as well as cutting down the complexity within the spec files. Personally, i really really & really like macros, because it is fun to write & easy to write, it has many beneficial side effects:
#1. Since it is so simple to use macros, everyone in the team is more willing to participate in spec writing. Personal experience has proven that if specs/tests are hard to write, some developers tend to skip it.
#2. Since it is so easy to read, maintenance becomes much easier
#3. Developers are smart people (and should be paid well, but that's another story), by replacing repeated copy & paste & minor edit-here-and-there with macros-writing-and-usage, they feel smarter, a huge morale boast ~> happier team, & when people are happy, they tend to show more love towards the project, take ownership ~> project in a better state.
Btw, macro writing is no rocket-science, let's get started:
#1. Macro for 3rd party declarative
Ok, we all know that authlogic is well tested, so there is really no point in writing specs for it. Yet, how do we know the model is pulling in authlogic support ?? Here's what i've done:
The usage in the spec file is:
Of course, i can probably remove the include statement altogether & do the auto-including elsewhere, but taking this route, other developers may throw WTF-is-should_act_as_authentic error ~> BAD !!
#2. Macro for project-specific declarative
I wanna make sure the support from the home-baked Markdownable is pulled in. Since it is home-baked, just making sure the module is mixed in is not good enough, i need to ensure the functionality is there as well. Thus (nested within the above mentioned RailsBestPractices::Macros::ClassMethods module):
And the usage:
#3. Macro for any other vanilla functionality
The macro definition:
Finally, the usage:
Of course, it doesn't make sense to define macro for every functionality available. The general thumb of rule is that when similar code appears more than once, u can consider defining a macro for it. However, if similar-code is to appear more than once, u will probably start by extracting the code & placing it inside a module (eg. Markdownable), so this probably falls under #2. Macro for project-specific declarative.
Last but not least, the above macro definition can probably benefit from some refactoring, but i guess i'll leave this exercise to u :]
#1. Since it is so simple to use macros, everyone in the team is more willing to participate in spec writing. Personal experience has proven that if specs/tests are hard to write, some developers tend to skip it.
#2. Since it is so easy to read, maintenance becomes much easier
#3. Developers are smart people (and should be paid well, but that's another story), by replacing repeated copy & paste & minor edit-here-and-there with macros-writing-and-usage, they feel smarter, a huge morale boast ~> happier team, & when people are happy, they tend to show more love towards the project, take ownership ~> project in a better state.
Btw, macro writing is no rocket-science, let's get started:
#1. Macro for 3rd party declarative
1 | class User < ActiveRecord::Base |
Ok, we all know that authlogic is well tested, so there is really no point in writing specs for it. Yet, how do we know the model is pulling in authlogic support ?? Here's what i've done:
1 | module RailsBestPractices |
The usage in the spec file is:
1 | describe User do |
Of course, i can probably remove the include statement altogether & do the auto-including elsewhere, but taking this route, other developers may throw WTF-is-should_act_as_authentic error ~> BAD !!
#2. Macro for project-specific declarative
1 | class Post < ActiveRecord::Base |
I wanna make sure the support from the home-baked Markdownable is pulled in. Since it is home-baked, just making sure the module is mixed in is not good enough, i need to ensure the functionality is there as well. Thus (nested within the above mentioned RailsBestPractices::Macros::ClassMethods module):
1 | def should_be_markdownable |
And the usage:
1 | describe Post do |
#3. Macro for any other vanilla functionality
1 | class Implementation < ActiveRecord::Base |
The macro definition:
1 | def should_have_user_ownership |
Finally, the usage:
1 | describe Implementation do |
Of course, it doesn't make sense to define macro for every functionality available. The general thumb of rule is that when similar code appears more than once, u can consider defining a macro for it. However, if similar-code is to appear more than once, u will probably start by extracting the code & placing it inside a module (eg. Markdownable), so this probably falls under #2. Macro for project-specific declarative.
Last but not least, the above macro definition can probably benefit from some refactoring, but i guess i'll leave this exercise to u :]
Wednesday, July 21, 2010
Relatively unknown Object#instance_exec
Do u happen to know Object#instance_eval's brother, Object#instance_exec ?? Here's a uninteresting way of coding wo Object#instance_exec:
And here's a more interesting way to do it, w Object#instance_exec:
Yet another way to do it can be:
Enjoy !!
1 | Someone = Struct.new(:name, :password) |
And here's a more interesting way to do it, w Object#instance_exec:
1 | Someone = Struct.new(:name, :password) |
Yet another way to do it can be:
1 | Someone = Struct.new(:name, :password) |
Enjoy !!
Stubbing only the nth instance's method, should or shouldn't ?
A few days back, cross-stub version 0.2.0 has been released, with this release, cross process instance method stubbing is now possible. While doing review of zan's work, we've discussed the possibility of having more control on which exact instance the stub should be applied to, as illustrated below:
Initially, i was quite happy to have brought up this. But after some thinking, i'm convinced that this a LOUSY idea, as it requires the user to have intimate knowledge of how many times a class's instance have been created, which can be tedious (if not impossible) if the instance method is provided by ruby core/std libs, or popular gems. It also results in extremely brittle tests, as nth can easily beconme xth, or zth ...
I think the issue is very fundamental, it goes down to how the code is written. Given the following example:
If i were to apply stubbing for Time#strftime, i would affect both Papa#free_today? & Mama#free_today?. Taking one step back, if we to affect only Papa#free_today?, we need to consider the context where the Time#strftime stub is-to-be applied, & do the following revision:
Therefore, by stubbing Papa#today_dow, we won't affect Mama#free_today?. Moreover, taking this approach gives us the chance to do some cleaning up:
Imagine if we go the other approach of doing nth instance's method stubbing, though feasible, the test will be extremely brittle (nightmarish maintenance), as nth can easily be changed to xth, or zth.
CONCLUSION: No, we don't need better control over nth instance's method stubbing, cross-stub's instance method stubbing is going to remain as it is. If u need it, drop me a mail with an example to justify the need :]
1 | Time.xstub(:day, :instance => 1) # applies to ONLY 1st Time instance |
Initially, i was quite happy to have brought up this. But after some thinking, i'm convinced that this a LOUSY idea, as it requires the user to have intimate knowledge of how many times a class's instance have been created, which can be tedious (if not impossible) if the instance method is provided by ruby core/std libs, or popular gems. It also results in extremely brittle tests, as nth can easily beconme xth, or zth ...
I think the issue is very fundamental, it goes down to how the code is written. Given the following example:
1 | class Papa |
If i were to apply stubbing for Time#strftime, i would affect both Papa#free_today? & Mama#free_today?. Taking one step back, if we to affect only Papa#free_today?, we need to consider the context where the Time#strftime stub is-to-be applied, & do the following revision:
1 | class Papa |
Therefore, by stubbing Papa#today_dow, we won't affect Mama#free_today?. Moreover, taking this approach gives us the chance to do some cleaning up:
1 | class Parent |
Imagine if we go the other approach of doing nth instance's method stubbing, though feasible, the test will be extremely brittle (nightmarish maintenance), as nth can easily be changed to xth, or zth.
CONCLUSION: No, we don't need better control over nth instance's method stubbing, cross-stub's instance method stubbing is going to remain as it is. If u need it, drop me a mail with an example to justify the need :]
Sunday, July 18, 2010
Tidying up specs suite with otaku
In the specs suite of cross-stub, i've built a custom echo server using this approach to support testing of stubbing in another process. Nothing wrong with it, except that it is not so fun & clean. Anyway, over the weekend, i've extracted out the echo server code, and put it inside a new & fun project otaku.
Here's my tidied up specs (which serve as a good example usage of otaku):
Here's my tidied up specs (which serve as a good example usage of otaku):
1 | shared 'has other process setup' do |
Building a simple server/client with eventmachine 2
Following previous entry, i've come up with a drop dead simple service framework based on eventmachine, just a fun project actually, as u can see from its name "OTAKU". Since code speaks a thousand words, especially for someone like me, let's jump straight into some coding:
Of course, life is never as simple as the above, since we have Otaku.start accepts a proc, most people tend to code in-a-taken-for-granted-way taking advantage of contextual references:
Now now, really unexpected rite ? The reason is that otaku converts the proc passed to Otaku.start, convert it to plain ruby code, & eval in the server process, thus, naturally, any contextual references are lost. As a workaround:
Well, i must admit this workaround is abit not-so-comfortable ... anyway, i'm still thinking (hard) on how the contextual references can be extracted under the hood, without having user to go through the trouble of declaring the context hash explicitely. Anyway, this is what otaku has to offer for now :]
Have a look at this slightly more complicated usage of otaku.
1 | require 'otaku' |
1 | require 'otaku' |
Of course, life is never as simple as the above, since we have Otaku.start accepts a proc, most people tend to code in-a-taken-for-granted-way taking advantage of contextual references:
1 | mark = '*' |
Now now, really unexpected rite ? The reason is that otaku converts the proc passed to Otaku.start, convert it to plain ruby code, & eval in the server process, thus, naturally, any contextual references are lost. As a workaround:
1 | Otaku.start(:mark => '*') do |data| |
Well, i must admit this workaround is abit not-so-comfortable ... anyway, i'm still thinking (hard) on how the contextual references can be extracted under the hood, without having user to go through the trouble of declaring the context hash explicitely. Anyway, this is what otaku has to offer for now :]
Have a look at this slightly more complicated usage of otaku.
Thursday, July 15, 2010
Building a simple server/client with eventmachine
In writing cross-stub, i need to write specs to ensure cross process stubbing indeed work as expected. This is achieved by building a simple server/client using eventmachine, here's how echo_server.rb looks like:
And echo_client.rb:
And finally the example usage:
Personally, i prefer placing the server & client code inside a single file echo_service.rb, since the client code is pretty coupled with the server code. One gotcha i've encountered while working with this server/client thingy is that error in the server hangs the client, that's why having the server log file is really useful in debugging.
1 | require 'rubygems' |
And echo_client.rb:
1 | # Keeping things DRY, avoid repeated typing of requiring statements & |
And finally the example usage:
1 | require File.join(File.dirname(__FILE__), 'echo_server') |
Personally, i prefer placing the server & client code inside a single file echo_service.rb, since the client code is pretty coupled with the server code. One gotcha i've encountered while working with this server/client thingy is that error in the server hangs the client, that's why having the server log file is really useful in debugging.
Marshal/Unmarshal problem with Jruby
While working on cross-stub today, ran into a marshal/unmarshal problem with jruby, caused by binary string encoding issue. Anyway, let's start by taking a look at some code:
Here's what i got when running the above in different rubies:
Did quite abit of research, either my googling skill sucks, or nobody has the same problem as me ... yet eventually found something at stackoverflow, which lights up the bulb, & here's the amended code that works for all the above-mentioned rubies:
1 | time1 = Time.now |
Here's what i got when running the above in different rubies:
#1. The expected output of Ruby 1.8.7 & Ruby 1.9.1 & Ree 1.8.7
# >> true
#2. The unexpected output of Jruby-1.5.1
# >> (eval):1:in `_load': marshaled time format differ (TypeError)
Did quite abit of research, either my googling skill sucks, or nobody has the same problem as me ... yet eventually found something at stackoverflow, which lights up the bulb, & here's the amended code that works for all the above-mentioned rubies:
1 | require 'base64' |
Wednesday, July 14, 2010
Minimalist ... even more minimal than BasicObject
In cross-stub, it is possible to declare stubs using a proc:
Implementation wise, i need a way to find out the methods delared within the proc in order to do further processing. The most straight-forward way is to have a bare minimum object, after instance_eval of the proc, do a diff of the original set of instance methods & the set of methods for this manipulated object. The following illustrates the concept:
Well, i've in fact considered using BasicObject, but then, it doesn't appear to be as basic as i hope it should it. Thus i decided to handroll my own Minimalist solution:
As u can see (in order to prevent any methods clash with user-defined-to-be-stubbed-methods) what i've done is just:
#1. renaming instance_eval & methods to the more cryptic __instance_eval__ & __methods__, and
#2. undefining all methods, except those with prepending & appending __
It works well in ruby-1.8.7, but then, running the above in ruby-1.9.1 displays the following warning:
With the solution suggested in this discussion, Minimalist is revised to:
Now the warning doesn't show. Yet in ruby-1.8.7, i got plenty of eval warnings:
Hmmm ... undaunted, i got this final version of Minimalist:
Yet for jruby-1.5.1, it yields:
The final final final (i promise) Minimalist becomes:
1 | Someone.xstub do |
Implementation wise, i need a way to find out the methods delared within the proc in order to do further processing. The most straight-forward way is to have a bare minimum object, after instance_eval of the proc, do a diff of the original set of instance methods & the set of methods for this manipulated object. The following illustrates the concept:
1 | (minimalist = Minimalist.new).instance_eval(&block) |
Well, i've in fact considered using BasicObject, but then, it doesn't appear to be as basic as i hope it should it. Thus i decided to handroll my own Minimalist solution:
1 | class Minimalist |
As u can see (in order to prevent any methods clash with user-defined-to-be-stubbed-methods) what i've done is just:
#1. renaming instance_eval & methods to the more cryptic __instance_eval__ & __methods__, and
#2. undefining all methods, except those with prepending & appending __
It works well in ruby-1.8.7, but then, running the above in ruby-1.9.1 displays the following warning:
warning: undefining `object_id' may cause serious problem
With the solution suggested in this discussion, Minimalist is revised to:
1 | class Minimalist |
Now the warning doesn't show. Yet in ruby-1.8.7, i got plenty of eval warnings:
(eval):1: warning: discarding old say
(eval):1: warning: discarding old say
(eval):1: warning: method redefined; discarding old bang
Hmmm ... undaunted, i got this final version of Minimalist:
1 | class Minimalist |
Yet for jruby-1.5.1, it yields:
warning: `instance_eval' should not be aliased
The final final final (i promise) Minimalist becomes:
1 | class Minimalist |
Saturday, July 10, 2010
Painless inserting of nicely highlighted code block to my blog
If u happen to like the way i display code blocks in this blog & have read my previous post abt how i have given this blog a face-lifting, here are some extra steps i've taken to conveniently allow me to:
# 1. edit whatever code i want to display in vim
# 2. use a user-defined command (while still in vim) :Xc to process the entire (or selected lines) of the current buffer to yield nicely-highlighted code block & load it to the X11 clipboard (using xclip)
# 3. use middle mouse click (hehe, i'm using thinkpad, anyway, this can be done by doing left & right click together on mouse) to paste it to whereever i like (eg. the textarea i'm typing in right now)
Enough said, let's get into action:
# S1. Installing the required packages:
# S2. Inserting the user-defined command :Xc to ~/.vimrc
# S3. Revising the existing ~/bin/code2html.rb to accept extra args
To try out, restart ur vim to edit any file, to convert the entire file, run the following command in vim:
Alternatively, if u want to convert the selected lines (in visual mode, using Shift-v to select the specific lines):
And to do the pasting, just do middle (mouse) click on any textarea u are working on.
# 1. edit whatever code i want to display in vim
# 2. use a user-defined command (while still in vim) :Xc to process the entire (or selected lines) of the current buffer to yield nicely-highlighted code block & load it to the X11 clipboard (using xclip)
# 3. use middle mouse click (hehe, i'm using thinkpad, anyway, this can be done by doing left & right click together on mouse) to paste it to whereever i like (eg. the textarea i'm typing in right now)
Enough said, let's get into action:
# S1. Installing the required packages:
pacman -S xclip
# S2. Inserting the user-defined command :Xc to ~/.vimrc
1 | func! Xclip() range |
# S3. Revising the existing ~/bin/code2html.rb to accept extra args
1 | #!/usr/bin/env ruby |
To try out, restart ur vim to edit any file, to convert the entire file, run the following command in vim:
:Xc
Alternatively, if u want to convert the selected lines (in visual mode, using Shift-v to select the specific lines):
:'<,'>Xc
And to do the pasting, just do middle (mouse) click on any textarea u are working on.
Turning off the irritating beep
# 1. Console beep:
# 2. Firefox Beep
Goto ‘about:config’, search for ‘accessibility.typeaheadfind.enablesound’ & set it to ‘false’.
xset b off
xset -b
xset b 0
# 2. Firefox Beep
Goto ‘about:config’, search for ‘accessibility.typeaheadfind.enablesound’ & set it to ‘false’.
Hand-rolling screenshot taking solution
Here's my handrolled solution for taking screenshot on my arch linux, using imagemagick, feh & xbindkeys for taking & displaying the just-taken screenshot on my arch linux:
# 1. Installing the required packages
# 2. Scripting ~/bin/take-screenshots
#3. Inserting the <PrtSc> key binding in ~/.xbindkeysrc
Restart ur X server & pressing <PrtSc> should pop up the just-taken screenshot.
# 1. Installing the required packages
pacman -S xbindkeys feh imagemagick
# 2. Scripting ~/bin/take-screenshots
1 | #!/bin/bash |
#3. Inserting the <PrtSc> key binding in ~/.xbindkeysrc
1 | "~/bin/take-screenshots" |
Restart ur X server & pressing <PrtSc> should pop up the just-taken screenshot.
Friday, July 9, 2010
New Face New Look
Finally given this blog a face-lifting. Well, 99.9% of the credit goes to (1) coderay, (2) syntax highlighting railscast, & (3) the new template designer of blogger.com, while the rest falls under some minor hackish tweaking on my part. Anyway, here's the fragment that customizes the blog template:
Plus the help of the script ~/bin/code2html.rb:
As ~/bin is already in my $PATH, all i need to do to convert any code to the nice code blocks u see here is to run:
Hmmmm .. not too bad rite ? Now, what is left is to set aside some time to do some cleaning up for the rest of this blog (sweat).
1 | <style type='text/css'> |
Plus the help of the script ~/bin/code2html.rb:
1 | #!/usr/bin/env ruby |
As ~/bin is already in my $PATH, all i need to do to convert any code to the nice code blocks u see here is to run:
code2html.rb <PATH_TO_ANY_FILE>
Hmmmm .. not too bad rite ? Now, what is left is to set aside some time to do some cleaning up for the rest of this blog (sweat).
Subscribe to:
Posts (Atom)
Labels
Blog Archive
-
▼
2010
(21)
-
►
July
(15)
- Lazy evaluating with Enumerable#any?
- Everyone should start writing macros
- Relatively unknown Object#instance_exec
- Stubbing only the nth instance's method, should or...
- Tidying up specs suite with otaku
- Building a simple server/client with eventmachine 2
- Building a simple server/client with eventmachine
- Marshal/Unmarshal problem with Jruby
- Minimalist ... even more minimal than BasicObject
- Painless inserting of nicely highlighted code bloc...
- Turning off the irritating beep
- Hand-rolling screenshot taking solution
- New Face New Look
-
►
July
(15)