Google自訂搜尋
原文章轉至:http://lightyror.blogspot.com/2006/09/ruby-syntax-suger.html
 

剛剛正在寫一個計算 apache 有多少 process 的 Ruby Script


`ps auxw | grep apache `.split("\n").each do |line|
user,pid,cpu,mem,vsz,rss,tty,stat,start,time,command = line.split("\s")
process_count += 1 if command == '/usr/sbin/apache'
end


注意這一段
user,pid,cpu,mem,vsz,rss,tty,stat,start,time,command = line.split("\s")
寫完之後,興奮之情仍然無法消去
平行 asignment 雖然被人稱為是 syntax suger
但是這樣的 syntax suger 可以很有效的幫助 programmer 整理邏輯
而這樣的寫法,以後我要做什麼擴充像是 user = 'abc' 都很方便
也不用重新查一次 ps 的排列順序

誰說 syntax suger 不重要的


==========
底下是心得
==========

嗯..
簡單的說
我可以偵測CPU使用率,過高的就`kill -9 #{pid}`
對於系統管理員來說,無疑是極大的方便阿!!

hechian 發表在 痞客邦 留言(0) 人氣()

國外有個Google Maps
台灣有個UrMap
在使用API前,一樣都得申請授權碼
http://www.urmap.com/SearchEngine/api/signup.html
但是請記住,授權碼只適用於當前目錄下的所有檔案,但不包含子目錄,詳情請看:
http://www.urmap.com/SearchEngine/api/faq.html#1

而這邊是API的說明文件區:
http://www.urmap.com/SearchEngine/api/documentation/

OK!資源都有了!
現在開始,來使用吧

首先,我們要先在前面引入帶有授權碼的API

而API預設Encoding是Unicode(UTF-8)
因此,如果你是使用Big5來開發的話,請多加上charset


好了,引入之後,開始撰寫吧
喔對了,為了方便起見,引入一下prototype.js吧!
以下用Prototype.js唷!
來張新地圖如何?
在body內要來個div標籤

這個div標籤可以調整大小,這個就交給你們自己搞拉~
然後,我們就可以開始寫拉~
底下我們寫著



OK!現在在地圖上看看顯示了什麼呢?
沒錯.. 我們學校Orz..

可是我看人家的地圖上都有縮放的控制,還可以切換電子地圖、衛星地圖
那該怎樣作呢???

很簡單拉XD
UrMap提供三個控制器
* U_FULLZOOM_CONTROL
* U_MINIZOOM_CONTROL
* U_TYPE_CONTROL

這3個是什麼呢?
哦!
第一個是很長的那條,有顯示縮放層級外加+-按鈕的那個
第二個是只有+-按鈕
第三個是在右上角顯示"電子地圖"、"衛星影像"跟"衛星地圖"拉!

那要怎樣加入呢??
將地圖新增好後,使用addControl來增加

map.addControl(U_FULLZOOM_CONTROL);
map.addControl(U_TYPE_CONTROL);

很簡單吧??
下次再來寫其他的:P


hechian 發表在 痞客邦 留言(0) 人氣()

昨晚又失眠了...
唉...

然後順便想著...
In Ruby, integer can use method 'chr' to convert to the character
but If I want to convert charactor to integer, how can I do?

我記得以前有用過,可是我忘了
一直找不到那個method可以轉換
所以剛剛上了irc://irc.freenode.net/ #ruby-lang
問了一下...
一開始,我並不知道有這method... 所以我自己寫了個
But I don't know how can I get the value...

Following is the chatting log in the channel...

zusocfc: Hi, all
zusocfc: Excuse me.. I want to do a meta programming...
zusocfc: I want to re-define the Integer class, add a method named "asc"
kmeyer: class Integer; def asc;
zusocfc: And I don't know how to get the integers
zusocfc: kmeyer: I know that, I mean I want to do like..
zusocfc: kmeyer:  puts 65.asc # Output "A"
kmeyer: oh.
kmeyer: You want chr
kmeyer: 65.chr # "A"
zusocfc: Orz.. wait
zusocfc: =  ="
zusocfc: puts "A".asc # Output 65
kmeyer: "A".ord
zusocfc: ....
kbrooks: no
kmeyer: err
kmeyer: wait
kbrooks: "A"[0]
kmeyer: "A"[0]
kmeyer: or ?A
zusocfc: ha! Thanks a lot :D
kmeyer: no prob
zusocfc: but.. If I want to add a method for practice like that, how can I do?
zusocfc: kmeyer: uh... it return 0
zusocfc: returned*
kmeyer: "a".asc ?
kmeyer: then swap out [0] for self[0]
zusocfc: kmeyer: got it.. thanks :D
zusocfc: kmeyer: excuse me... I have a problem... now there has a code "65.chr", how chr gets the value '65'?
kmeyer: it's smart :P
kmeyer: err
zusocfc: kmeyer: err...
kmeyer: chr is a method on Fixnum
kmeyer: self
kmeyer: but it's actually in C
zusocfc: kmeyer: OK!! Thank you :D

ya~ you can use "self" to get the value, and '?' or '[0]' can get the ASCII code of the character!
But, it could not be use when I input a string...
So... I have to write a member funtion for class String that convert each character in a string to ascii code...
Following is the source code...

class String

  def each_asc
    ascii = Array.new
    self.size.times do |c|
      ascii[c] = self[c]
    end
    ascii
  end

end


Usage:

puts "ABCD".each_asc

It will output:
65
66
67
68

hechian 發表在 痞客邦 留言(0) 人氣()

嗯... 在此推薦一本書
Flag的沈時宇出的
Google成功背後的技術 Ajax網頁程式設計
話說沈先生是我們Contagion以前的同事... 咳咳..算了不多說
可是我認為... Google好像成功的原因是它下的方針對才是...

* Ajax
* Ajax.Responders
* Ajax.Base
** Ajax.PeriodicalUpdater
** Ajax.Request
*** Ajax.Updater


hechian 發表在 痞客邦 留言(0) 人氣()


http://www.youtube.com/watch?v=eD05ZkGG3Gw

Still waters run deep
Just remember when we lie to each other
No one wins and losers weep
Reflections will show
This connection we can lean on each other
This is all we need to know

Now i admit i've been a fool sometimes
I believe i may be losing you
It's now or never, we talk before a tear is shed
Under your spell and over my head
You took me in your arms, and you told me
We don't need to worry, but you
You never say you're sorry

Keep this love alive for us, it's all i feel
How can we survive if we change what is real

Still waters run deep
Just remember when we lie to each other
No one wins and losers weep
Deception will show
It's a struggle when you're starved for affection
This is all we need to know

I see the angel and the devil in your heart
I confess, either one is my addiction
And where you're leading me
I don't have the will to fight
Out of the darkness and into the light
But living in your arms, i can take it
Please don't make me shiver
I know together we deliver

Let the gods decide who finds the sweetest thrill
Who can justify the truth?
I love you still

Still waters run deep
Just remember when we lie to each other
No one wins and losers weep
Deception will show
It's a struggle when you're starved for affection
This is all we need to know

Don't let him get too close to you
You've got a stranger on your mind
The dream is free
I know i'm only human if i cry
Or maybe we should kiss and say goodbye

Still waters run deep
Just remember when we lie to each other
No one wins and losers weep
Deception will show
It's a struggle when you're starved for affection
This is all we need to know

Still waters run deep
Just remember when we lie to each other
No one wins and losers weep
Reflections will show
This connection we can lean on each other
This is all we need to know 

hechian 發表在 痞客邦 留言(0) 人氣()

這網站看起來真不錯:P
網址:http://demo.script.aculo.us/

裏面有幾個不錯的Demo
而且都有釋出原始碼

用Rails真的很快Orz..
像是第一個範例
Autocompleting text fields (basic)

程式碼兩行(一行在view,一行是controller)就搞定

第二個也是扯
上面
To:那邊輸入A
CC:輸入B
輸入一個字後先等著不要離開,它就會出現suggestion了..

hmmm... 真是好玩!

hechian 發表在 痞客邦 留言(0) 人氣()