??? ~_~学习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 + 2puts("---------------类方法-----------")p Time.newp Array.newp Array["a","b","c"]puts("---------------函数性方法-----------")p Math.sin(100)p sleep(1)print "d\n"puts("---------------定义方法-----------")def hello(a,b) a + bendp hello(1,2)def hello(a,b=3) #可以在方法的定义中加入预设值 a + bendp 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") endendmax(1,1)max(1,2)max(2,1)
?
puts("---------------if &&和||-----------")x = 5if x > 6 && ((y = x) < 10)# && 的用法 print(y,"&&\n")endif x > 6 || ((y = x) < 10) print(y,"||\n")enda = 1b = 2if a == b print "a=b\n"elsif a > b print "a>b\n"else print "a<b\n"endprint x,"\n" if x > 3puts("---------------unless-----------")unless a == b puts "a!=b"else puts "a=b"endputs("---------------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 endendputs("---------------===-----------")=begin ===用法,可以理解为属于=endp (1..3) === 4p /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")endfor name in ["1","2","3","4","5"] print("第",name,"个ruby\n")endputs("---------------while-----------")sum = 5while sum > 0 print("第",6-sum,"个ruby\n") sum -= 1endputs("---------------until-----------")sum = 5until sum <= 0 print("第",6-sum,"个ruby\n") sum -= 1endputs("---------------each-----------")(1..5).each{|i| print("第",i,"个ruby\n")}puts("---------------loop-----------")=beginloop{ print("ruby\n")#loop一直执行}=endputs("---------------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.newp ary #=>[]p ary.class #=>Arraystr = "dsds"p str.class #=>Stringp str.instance_of?(String) #=>truep str.instance_of?(Array) #=>falsep str.is_a?(String) #=>truep str.is_a?(Object) #=>trueputs("-----------------------自定义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" endendbob = HelloWorld.new("Bob")jim = HelloWorld.new("Jim")bob.hellojim.helloputs bob.namebob.name = "Lucy"puts bob.nameHelloWorld.hi("Ruby")puts HelloWorld::Versionclass String#扩充类 def count_word ary = self.split(/\s+/) return ary.size endendp "This is a big mistake".count_wordclass RingArray < Array#继承类 def [](i) super(i % size) endendstr = 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 :priendac = AccTest.newp 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 endendp0 = Point.newp1 = Point.new(1,1)p p0p p1p0.swap(p1)p p0p p1
?
puts("-----------------------module--------------------")p Math::PImodule HelloModule Version = 1.0 def hello(name) puts name end module_function :helloendp HelloModule::VersionHelloModule.hello("d")include HelloModulehello(1)p Version
??? 以上为今天写的一些很基础很基础的东西,其实需要注意的细节有很多。主要还是先过一遍,到需要用到的时候再翻书也不迟。:)
赶快上路吧,不要有一天我们在对方的葬礼上说,要是当时去了就好了。