??? ~_~学习Ruby第二天,主要还是基础知识学习。诸如对象、变量、常量、条件、循环、方法、类、模块等的基本表示。
puts("---------------实例方法-----------") p "10,20,30,40".split(",") p [1,2,3,4].index(2) p 1000.integer? p /Ruby/ =~ "dsdRuby" name = ["1","2","3"] p name[0] p 1 + 2 puts("---------------类方法-----------") p Time.new p Array.new p Array["a","b","c"] puts("---------------函数性方法-----------") p Math.sin(100) p sleep(1) print "d\n" puts("---------------定义方法-----------") def hello(a,b) a + b end p hello(1,2) def hello(a,b=3) #可以在方法的定义中加入预设值 a + b end p hello(1,5) def max(a,b) if a > b print(a,">",b,"\n") elsif a == b print(a,"=",b,"\n") else print(a,"<",b,"\n") end end max(1,1) max(1,2) max(2,1)
?
puts("---------------if &&和||-----------") x = 5 if x > 6 && ((y = x) < 10)# && 的用法 print(y,"&&\n") end if x > 6 || ((y = x) < 10) print(y,"||\n") end a = 1 b = 2 if a == b print "a=b\n" elsif a > b print "a>b\n" else print "a<b\n" end print x,"\n" if x > 3 puts("---------------unless-----------") unless a == b puts "a!=b" else puts "a=b" end puts("---------------case-----------") tags = ["P","A","I","IMG"] tags.each{|tag| case tag when "C","A","B" #when允许有多个值匹配 print tag," has child\n" when "I","IMG" print tag," has no child\n" else print tag," can not be used\n" end } array = ["1",1,nil] array.each{|a| case a when Numeric print a," is a number\n" when String print a," is a String\n" else print a," is a something\n" end } while line = gets case line when /ruby/i print(line," find ruby\n") when /bub/i print(line," find bub\n") else end end puts("---------------===-----------") =begin ===用法,可以理解为属于 =end p (1..3) === 4 p /zz/ === "sszzy" p String === "2" p Numeric === 2
?
puts("---------------times-----------") 5.times{|i| print("第",i+1,"个ruby\n") } puts("---------------for-----------") for i in 1..5 print("第",i,"个ruby\n") end for name in ["1","2","3","4","5"] print("第",name,"个ruby\n") end puts("---------------while-----------") sum = 5 while sum > 0 print("第",6-sum,"个ruby\n") sum -= 1 end puts("---------------until-----------") sum = 5 until sum <= 0 print("第",6-sum,"个ruby\n") sum -= 1 end puts("---------------each-----------") (1..5).each{|i| print("第",i,"个ruby\n") } puts("---------------loop-----------") =begin loop{ print("ruby\n")#loop一直执行 } =end puts("---------------break next redo-----------") i = 0 ["phthon","ruby","perl"].each{|name| i += 1 if i == 2 break #立刻停止循环 end print(i,name,"\n") } i = 0 ["phthon","ruby","perl"].each{|name| i += 1 if i == 2 next #跳到下次循环 end print(i,name,"\n") } i = 0 ["phthon","ruby","perl"].each{|name| i += 1 if i == 2 redo #重新进行本次循环 end print(i,name,"\n") }
?
puts("-----------------------class--------------------") ary = Array.new p ary #=>[] p ary.class #=>Array str = "dsds" p str.class #=>String p str.instance_of?(String) #=>true p str.instance_of?(Array) #=>false p str.is_a?(String) #=>true p str.is_a?(Object) #=>true puts("-----------------------自定义class--------------------") class HelloWorld Version = 1.0 #常量 @@count = 0 #类变量 attr_accessor :name #相当于java中的getter与setter方法 def initialize(name = "Ruby") #初始化方法,相当于构造方法 @name = name @@count += 1 print @@count," 初始化方法\n" end def HelloWorld.hi(name) print name," 类方法\n" end def hello print @name," 实例方法\n" end end bob = HelloWorld.new("Bob") jim = HelloWorld.new("Jim") bob.hello jim.hello puts bob.name bob.name = "Lucy" puts bob.name HelloWorld.hi("Ruby") puts HelloWorld::Version class String #扩充类 def count_word ary = self.split(/\s+/) return ary.size end end p "This is a big mistake".count_word class RingArray < Array #继承类 def [](i) super(i % size) end end str = RingArray["0","1","2","3","4","5"] p str[6] p str[-1] p str[3] class AccTest def pub puts "pub方法" end public :pub def pri puts "pri方法" end private :pri end ac = AccTest.new p ac.pub #p ac.pri #错误 class Point #protected attr_accessor :x , :y protected :x= , :y= def initialize(x = 0.0 , y = 0.0) @x = x @y = y end def swap(other) xtemp = @x ytemp = @y @x = other.x @y = other.y other.x = xtemp other.y = ytemp self end end p0 = Point.new p1 = Point.new(1,1) p p0 p p1 p0.swap(p1) p p0 p p1
?
puts("-----------------------module--------------------") p Math::PI module HelloModule Version = 1.0 def hello(name) puts name end module_function :hello end p HelloModule::Version HelloModule.hello("d") include HelloModule hello(1) p Version
??? 以上为今天写的一些很基础很基础的东西,其实需要注意的细节有很多。主要还是先过一遍,到需要用到的时候再翻书也不迟。:)