月归档:十二月 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 条评论