如鵬網(wǎng) 大學(xué)生計算機學(xué)習(xí)社區(qū)

          CowNew開源團隊

          http://www.cownew.com 郵件請聯(lián)系 about521 at 163.com

            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            363 隨筆 :: 2 文章 :: 808 評論 :: 0 Trackbacks

          1、map的聲明方法:
           m={"1"=>"3","2"=>"aa"}
          2、if count>10
               ....
             elsif c<3
             else
              ...
             end
          3、while c<3
             ...
             end
          4、正則表示式方法:
          /d3/
          有match等方法
          判斷字符串的匹配:
          if line=~/Perl|Python/
             ...
          替換:
          line.sub(/Perl/,'Ruby')將第一個Perl替換成Ruby
          line.gsub(/Perl/,'Ruby')將所有Perl替換成Ruby
          5、block的使用:
           as = ["2","66"]
          as.each{|a|print a}
          6、變量的作用域:@成員變量,@@類變量,$全局變量
          7、
          class Person
            def initialize(name,age)
              @name = name
              @age = age
            end
            def say()
              print "我叫"+@name
              print "#@age"
            end
          end

          p = Person.new("tom",20)
          p.say()
          print p.inspect#反射顯示對象的內(nèi)容
          8、類方法與類變量:
          class Person
            @@count = 0
            def Person.doIt
            end
          end
          9、數(shù)組的子數(shù)組:
          a=[1,2,3,5]
          a[1..3]→[2,3,5]
          切片
          a=[1,2,3,5]
          a[0..2]=[4]
          a→[4,5]
          10、for語句:
          for i in 0..100
             print i
          end
          11、block:
          def a
            for i in 0..100
              yield i
            end
          end

          a{|f|puts f}
          帶返回值的block:
          def a
            for i in 1..100
              if yield i
                puts "接受"+i.to_s
              end
            end
          end

          a{|v|v>90}

          應(yīng)用強大的block:
          m={}
          (1..100).each{|a| m[a]=a**a}
          m.each{|a|puts a}
          12、block與變長參數(shù):
          class FileProcess
            def FileProcess.process(*args)
              f = File.open(*args)
              yield f
              f.close()
            end
          end

          FileProcess.process("c:/boot.ini","r"){
            |file|
            while line=file.gets
              puts line
            end
          }
          另一種用法:
          FileProcess.process("c:/boot.ini","r") do |file|
            while line=file.gets
              puts line
            end
          end
          13、閉包:
          class MyClass
            def initialize(&action)
              @action = action
            end
            def begin()
              for i in 1..100
                if i>90
                  @action.call(self)
                end
              end
            end
          end

          a = MyClass.new(){puts "超限"}
          a.begin()

          lambda:
          a = lambda{|n| puts n}
          a.call(3)
          14 數(shù)字:
          6.times{|i|puts i}
          1.upto(5){|i| puts i}
          50.step(100,10){|i| puts i}
          15 轉(zhuǎn)化
          a="33"
          b="55"
          puts Integer(a)+Integer(b)
          puts a.to_i+b.to_i
          puts a*3
          16 字符串中內(nèi)嵌ruby表達(dá)式
          puts "He#{'l'*100}o"
          puts "1+1=#{1+1}"

          city = "peking"
          y=2008
          puts "#{city}-#{y}"
          注意只有用雙引號括起來的才支持內(nèi)嵌表達(dá)式
          17 puts "2005|03|08".split(/\|/)
          18 Range
          ('a'..'f').each{|c| puts c}
          Range轉(zhuǎn)化為數(shù)組:puts ('a1'..'a9').to_a
          19 返回bool的方法通常以?結(jié)尾,修改對象狀態(tài)的方法以!結(jié)尾
          20 自定義復(fù)數(shù):
          class FuNum
            def initialize(shi,xu)
              @shi=shi
              @xu=xu
            end
           
            def xu
              @xu
            end
            def shi
              @shi
            end
            public :shi,:xu
           
            def +(other)
              newshi = @shi+other.shi
              newxu = @xu+other.xu
              return FuNum.new(newshi,newxu)
            end 
           
            def to_s()
              return "#{@shi}+#{@xu}i"
            end
          end

          i = FuNum.new(1,2)
          j = FuNum.new(2,9)
          puts i+j
          21 交換兩個變量(并行賦值):
          a=20
          b=30
          c=40
          a,b,c=c,b,a

          print a,b,c
          python也支持
          22 Case語句:
          a=1
          salary=case a
                when 0..100 then 3000
                when 101 then 333
                else 888
                end
               
          puts salary
          Case語句的When部分還支持正則表達(dá)式
          case line
          when /title=(. )/
          puts"Titleis#$1"
          when/track=(. )/
          puts"Trackis#$1"
          when/artist=(. )/
          puts"Artistis#$1"
          end
          23 異常處理
          begin
            raise "33333333"
          rescue RuntimeError
            #$!表示異常信息
            print $!
            #再次拋出
            raise
          end

          begin
            raise "33333333"
          rescue RuntimeError
            #$!表示異常信息
            print $!
            #再次拋出
          ensure
            print "無論如何都被打印"
          end

          還可以在rescue中調(diào)用retry來重新執(zhí)行程序塊
          24 模塊
          CowNew.rb模塊文件
          module CowNew
            PI = 3.14
            def CowNew.calc(r)
              return 2*PI*r
            end 
          end

          調(diào)用者M(jìn)ain.rb
          require "CowNew"
          puts CowNew.calc(3)
          從文件加載模塊:
          load "E:/greeninst/eclipse3.2.2/eclipse/workspace/r/CowNew.rb"
          25 文件處理:
          f = "E:/greeninst/eclipse3.2.2/eclipse/workspace/r/CowNew.rb"
          puts IO.read(f)
          puts IO.readlines(f)

          逐行處理:
          f = "E:/greeninst/eclipse3.2.2/eclipse/workspace/r/CowNew.rb"
          i=IO.foreach(f){|line| puts line}

          f = "E:/greeninst/eclipse3.2.2/eclipse/workspace/r/CowNew.rb"
          i=0
          IO.foreach(f) do |line|
            i=i+1
            print i,"  ",line
          end
          26 流:STDOUT<<33<<"adfa"
          27 Ruby專用Http服務(wù)器,支持eruby,免得使用Apache,調(diào)試方便:
          require "webrick"
          include WEBrick
          server = HTTPServer.new(:DocumentRoot=>"E:/俺的文檔/個人資料/網(wǎng)站安全/cownew空間/")
          server.start()

          在目錄web下創(chuàng)建rhtml文件,增加服務(wù)器:
          require "webrick"
          include WEBrick
          server = HTTPServer.new(:DocumentRoot=>File.join(Dir.pwd,"web"))
          server.start()
          自定義Servlet:
          require "webrick"
          include WEBrick

          class HelloServlet<HTTPServlet::AbstractServlet
             def do_GET(req,res)
               res["Content-Type"] = "text/html"
               res.body="aaaaaaaaa"
             end
          end
          server = HTTPServer.new(:DocumentRoot=>File.join(Dir.pwd,"web"))
          server.mount("/hello",HelloServlet)
          server.start()

           

          posted on 2007-08-02 20:09 CowNew開源團隊 閱讀(420) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 土默特右旗| 理塘县| 磐安县| 汶川县| 长岛县| 福海县| 广东省| 唐海县| 徐汇区| 台山市| 锦屏县| 芜湖市| 永福县| 庆阳市| 库尔勒市| 阳原县| 长兴县| 永清县| 皋兰县| 施甸县| 西昌市| 化州市| 葫芦岛市| 额尔古纳市| 吴堡县| 甘南县| 交口县| 吐鲁番市| 德江县| 宝山区| 龙里县| 务川| 天门市| 台东县| 惠东县| 霍城县| 海林市| 睢宁县| 济南市| 长宁区| 绥滨县|