月歸檔:十二月 2008

ruby中10進制和16進制的轉換

16進制到10進制:
>> "B0A0".hex
=> 45216

>> "B0A0".to_i(16)
=> 45216
10進制到16進制:
>> 45216.to_s(16)
=> "b0a0"

發表在 Ruby on Rails | 留下評論

My collection of QUOTES

I have not failed. I’ve just found 10,000 ways that won’t work.
  – Thomas A. Edison

Look for the ridiculous in everything and you will find it.
  – Jules Renard

Failure is not the only punishment for laziness; there is also the success of others.
  – Jules Renard

There is no excellent beauty that hath not some strangeness in the proportion.
  – Sir Francis Bacon

When you look at yourself from a universal standpoint, something inside always reminds or informs you that there are bigger and better things to worry about.
  – Albert Einstein

Character is what you have left when you’ve lost everything you can lose.
  – Evan Esar

A goal is a dream with a deadline.

The time to stop talking is when the other person nods his head affirmatively but says nothing.
  – Henry S. Haskins

Efficiency is intelligent laziness.
  – Anonymous

The public is wonderfully tolerant. It forgives everything except genius.
  – Oscar Wilde

I’m kind of jealous of the life I’m supposedly leading.
  – Zach Braff
 
Nothing is as simple as we hope it will be.
  – Jim Horning
 
If the human mind was simple enough to understand, we’d be too simple to understand it.
  – Emerson Pugh
 
  An intellectual is a person who has discovered something more interesting than sex.
  – Aldous Huxley

Education is the ability to listen to almost anything without losing your temper or your self-confidence.
  – Robert Frost
 
The only paradise is paradise lost.
  – Marcel Proust

Laughter and tears are both responses to frustration and exhaustion. I myself prefer to laugh, since there is less cleaning up to do afterward.
  – Kurt Vonnegut

Happiness is not achieved by the conscious pursuit of happiness; it is generally the by-product of other activities.
  – Aldous Huxley

There is still a difference between something and nothing, but it is purely geometrical and there is nothing behind the geometry.
  – Martin Gardner

You can only be young once. But you can always be immature.
  – Dave Barry

A large income is the best recipe for happiness I ever heard of.
  – Jane Austen

A door is what a dog is perpetually on the wrong side of.
  – Ogden Nash

Laugh and the world laughs with you, snore and you sleep alone.
  – Anthony Burgess

It’s never just a game when you’re winning.
  – George Carlin

Never be afraid to laugh at yourself, after all, you could be missing out on the joke of the century.
  – Dame Edna Everage

All work and no play makes Jack a dull boy and Jill a rich widow.
  – Evan Esar

What if nothing exists and we’re all in somebody’s dream? Or what’s worse, what if only that fat guy in the third row exists?
  – Woody Allen

You will do foolish things, but do this with enthusiasm.
    -Collete

Two roads diverged in a wood, and I–/ I took the one less traveled by, / And that has made all the difference.
  – Robert Frost

Against logic there is no armor like ignorance.
  – Laurence J. Peter

It is not the strongest of the species that survives, nor the most intelligent, but those most adaptive to change.
Charles Darwin

You must be the change you want to see in the world. -Gandhi

"It’s better to be a pirate than join the navy."
Steve Jobs

Laughter is the closest distance between two people.
  – Victor Borge
 
Some have been thought brave because they were afraid to run away.
  – Thomas Fuller

By the time a man realizes that maybe his father was right, he usually has a son who thinks he’s wrong.
  – Charles Wadsworth

The trouble with being poor is that it takes up all of your time.
  – Willem de Kooning

The trouble about trying to make yourself stupider than you really are is that you very often succeed.
  – CS Lewis

At my lemonade stand I used to give the first glass away free and charge five dollars for the second glass. The refill contained the antidote.
  – Emo Phillips

Most turkeys taste better the day after; my mother’s tasted better the day before.
  – Rita Rudner

Men live in a fantasy world. I know this because I am one, and I actually receive my mail there.
  – Scott Adams

Happiness makes up in height for what it lacks in length.
  – Robert Frost

You probably wouldn’t worry about what people think of you if you could know how seldom they do.
- Olin Miller

“敢問夫子惡乎長?”曰:“我知言,我善養吾浩然之氣。”
“敢問何謂浩然之氣?”
曰:“難言也。其為氣也,至大至剛,以直養而無害,則塞於天地之間。…”
《孟子·公孫丑上》

A man can be happy with any woman as long as he does not love her.
  – Oscar Wilde

When a thing has been said and well, have no scruple. Take it and copy it.
- Anatole France

It is always the best policy to speak the truth–unless, of course, you are an exceptionally good liar.
  – Jerome K. Jerome

Many a man who falls in love with a dimple make the mistake of marrying the whole girl.
  – Evan Esar

A wise man will make more opportunities than he finds.
  – Sir Francis Bacon

The important thing is not to stop questioning.
  – Albert Einstein

Any word you have to hunt for in a thesaurus is the wrong word. There are no exceptions to this rule.
  – Stephen King

You have to know how to accept rejection and reject acceptance.
  – Ray Bradbury

Education is a method whereby one acquires a higher grade of prejudices.
  – Laurence J. Peter

The nine most terrifying words in the English language are, ‘I’m from the government and I’m here to help.’
  – Ronald Reagan

The key to being a good manager is keeping the people who hate me away from those who are still undecided.
  – Casey Stengel

The important work of moving the world forward does not wait to be done by perfect men.
  – George Eliot

Nothing succeeds like the appearance of success.
  – Christopher Lasch

Always get married early in the morning. That way, if it doesn’t work out, you haven’t wasted a whole day.
  – Mickey Rooney

發表在 成敗幾何, 我思我在 | 留下評論

聞王昌齡左遷龍標遙有此寄 李白

楊花落盡子規啼, 聞道龍標過五溪.
我寄愁心與明月, 隨君直到夜朗西.

發表在 詩詞精選 | 留下評論

次北固山下 唐 王灣

客路青山外, 行舟綠水前.
潮平兩岸闊, 風下一帆懸.
海日生殘夜, 江春入舊年.
鄉書何處達? 歸雁洛陽邊.

發表在 詩詞精選 | 一條評論

Ruby中如何複製對象 (deep clone)

用Ruby複製一個對象(object)也許沒有你想像的那麼容易. 今天我google了半天, 做個總結吧.
先從最簡單的開始, b = a 是複製嗎? 看代碼說話:
>> a= [0,[1,2]]
>> b=a
>> b[0]=88
>> b[1][0]=99
>> b 
=> [88, [99, 2]]
>> a 
=> [88, [99, 2]]
從上面代碼發現, 一但修改b, 原來的a也同時被改變了. 甚至:

>> b.equal?(a)
=> true
原來b跟a根本就是同一個object, 只是馬甲不一樣罷了. 所以b = a不是複製.
那 b = a.dup呢?? 還是看代碼:
>> a= [0,[1,2]]
>> b=a.dup
>> b[0]=88
>> b[1][0]=99
>> b
=> [88, [99, 2]]
>> a
=> [0, [99, 2]]
情況似乎有所好轉, 在修改b後, a還是有一部分被修改了.(0沒有變,但原來的1變成了99).
所以dup有時候是複製(如在Array只有一級時), 但有時不是複製哦.
再來一個, b = a.clone呢? 上代碼:
>> a= [0,[1,2]]
>> b=a.clone
>> b[0]=88
>> b[1][0]=99
>> b
=> [88, [99, 2]]
>> a
=> [0, [99, 2]]
情況幾乎跟dup一模一樣. 所以clone也不一定可以相信哦!
原來ruby中的dup和clone都是shallow複製, 只針對object的第一級屬性.
汗, 難道在Ruby中沒有辦法複製對像嗎? 也不完全是, 看這個:
>> a= [0,[1,2]]
>> b=Marshal.load(Marshal.dump(a))
>> b[0]=88
>> b[1][0]=99
>> b
=> [88, [99, 2]]
>> a= [0,[1,2]]
=> [0, [1, 2]]
修改b後a沒有被改變!!! 似乎終於成功找到複製的辦法了!!!
為什麼要加"似乎"呢? 因為有些object是不能被Marshal.dump的.如:
>> t=Object.new
>> def t.test; puts ‘test’ end
>> Marshal.dump(t)
TypeError: singleton can’t be dumped
    from (irb):59:in `dump’
    from (irb):59
更完善的複製方案可以考慮給ruby增加一個deep clone功能, 可以參考以下鏈接:
http://d.hatena.ne.jp/pegacorn/20070417/1176817721
http://www.artima.com/forums/flat.jsp?forum=123&thread=40913
好了, 複製這個小問題, 沒想到也能引出這麼長的文章來, 沒想到吧?

發表在 Ruby on Rails | 留下評論

CR, LF, CR/LF 回車 換行

在文本處理中, CR, LF, CR/LF是不同操作系統上使用的換行符.
Dos和windows採用回車+換行CR/LF表示下一行,
而UNIX/Linux採用換行符LF表示下一行,
蘋果機(MAC OS系統)則採用回車符CR表示下一行.
CR用符號’\r’表示, 十進制ASCII代碼是13, 十六進制代碼為0x0D;
LF使用’\n’符號表示, ASCII代碼是10, 十六製為0x0A.
所以Windows平台上換行在文本文件中是使用 0d 0a 兩個字節表示, 而UNIX和蘋果平台上換行則是使用0a或0d一個字節表示.
一般操作系統上的運行庫會自動決定文本文件的換行格式. 如一個程序在windows上運行就生成CR/LF換行格式的文本文件,而在Linux上運行就生成LF格式換行的文本文件.
在一個平台上使用另一種換行符的文件文件可能會帶來意想不到的問題, 特別是在編輯程序代碼時. 有時候代碼在編輯器中顯示正常, 但在編輯時卻會因為換行符問題而出錯.
很多文本/代碼編輯器帶有換行符轉換功能, 使用這個功能可以將文本文件中的換行符在不同格式單互換.
在不同平台間使用FTP軟件傳送文件時, 在ascii文本模式傳輸模式下, 一些FTP客戶端程序會自動對換行格式進行轉換. 經過這種傳輸的文件字節數可能會發生變化. 如果你不想ftp修改原文件, 可以使用bin模式(二進制模式)傳輸文本.

發表在 信息處理 | 4 條評論