小試身手
#!/usr/local/bin/ruby -w class Fib private def initialize # constructor @fib_array = Array.new # constructs an empty Array @fib_array[0] = 0 # variables prefixed with an '@' are instance variables @fib_array[1] = 1 end @@singleton = nil # variables prefixed with two '@'s are class variables public def [] (n) # operator overloading --- read-only while @fib_array.length <= n @fib_array.push(@fib_array[-1] + @fib_array[-2]) # Array supports operations of a stack end @fib_array[n] # the last expression is what the function returns end def Fib.theFib # a class method --- prefixed with the class name and a dot (.) @@singleton = Fib.new if not @@singleton # nil is false @@singleton end end (1..20).each {|i| puts Fib.theFib[i]} # each is analogous to for_each algorithm in C++ # confusingly, in Ruby it's called an "iterator" # {|i| puts Fib.theFib[i]} is analogous to a function object in C++ # in Ruby it's called a "block" # output: # 1 # 1 # 2 # 3 # 5 # 8 # 13 # 21 # 34 # 55 # 89 # 144 # 233 # 377 # 610 # 987 # 1597 # 2584 # 4181 # 6765 puts Fib.theFib[200] # built-in support for big numbers # output: # 280571172992510140037611932413038677189525
除了大數,用標準 C++ 寫是也不會複雜到哪裡去啦。一口氣開始學兩種新語言,才體認 vi 之神妙 ─ Ruby 和 Common Lisp 竟然都認識 XD。
C++好啊XD
雖然剛剛才知道這是ruby
#是註解,這在其他language倒是蠻常見的
@是設定
沒有大括號,考驗的是你縮排的功力..
(有人說:考驗vi的功力 XD)
想知道嗎
精通vi第六版告訴你
嚴重超出版面 XD
何不考慮將版面加寬?
一定是你的字型太大了 XD。
Safari 看起來很好呀 XD。
不然等它掉到下面就不會被遮住了 XD。
剛試了一下,這個版面若要加大,圖片也得修改尺寸。
所以…等想做的時候再做吧 :P。
真的...超出去啦
好吧,剛剛測試,用power book的眼睛比較好,字硬是比windows firefox小一號
ie ,blogeer會少了個人介紹和host link....這看啥啊
<< 回到主頁