ruby笔记
上一篇 /
下一篇 2011-11-02 09:03:36
/ 个人分类:Ruby
#puts "hello world!"#::=beginmoduleclassdef,undefdefined?if,then,else,elsif,case,when,unlessfor,in,while,until,next,break,do,redo,retry,yieldnot,and,ortrue,false,nilrescue,ensuresuper,selfbegin/endBENGIN END_FILE_,_LINE_returnaliasrequire,include[] []=**!~+-*/%+->> <<<= < > >=<=> == ====~ != !~&&||.. ...defined?notor andif unless while untilbegin/end局部变量、方法参数、方法名用一个小写字母开头或者用一个下划线开头,全局变量用美元符号作为前缀¥,实例变量用@开头,类变量用@@开头类名、模块名、常量用大写字母开头。词首字母后面可以是字母、数字和下划线的任意组合@后面不可以直接跟数字类库require include将类库程序名包含在已经定义好的类、方法。从父类继承得到的方法可以直接使用I/O 输入/输出 Ruby语言Kernel模块的方法,Mix-in在根类的Object中puts带换行符号,print不带换行符号printf "Number:%4.3f,string:%s",7.8,"hi!" \ngets=end=begin数据类型浮点型数据小数点后必须跟数字1.1e30表示八进制,0x表示十六进制,0b表示二进制字符串是在单引号,双引号之间的代码1..5 1,2,3,4,5 [1,5]1...5 1,2,3,4 [1,5) a =1;b=2+3a,b=b,a#值互换print a ; print bx=0a,b,c=x,(x+1),(x+2)puts a,b,cprint a,b,cprint aa=1;b=1.0;print a==ba=1;b=1.0;puts a.eql?(b)a=1;b=1.0;print a.equal?(b)a=1.0;b=aprint a.equal?(b)puts 'aab'<=>'acb'puts [5]<=>[4,9]puts (0..9)===3.14puts ('a'..'c')==='c'=~用来比较是否符合一个正则表达式,返回模式在字符串中被匹配到得位置,否则返回nil!~断言不符合一个正则表达式,返回true,falseif 条件 then 语句 end语句 if 条件if conditon statement elsif condition statement else statement end unless= if not case object when condition statement when condition statement else statement end x=3case x when 1..2 print "x=",x,";在1..2中" when 4..9,0 print "x=",x,";在4..9中,或是0" else print "x=",x,";其它可能" end a =1until a>=10print a," "a=a+1enda=1while not a>=10 print a," "a=a+1enduntil= while not for i in 1..9 print i, " "endbreaknextredoretryputs "演示break"c='a'for i in 1..4 if i==2 and c=='a' print "\n" break end print i,c," "endputs "\n\n"puts "演示break"c='a'for i in 1..4 if i==2 and c=='a' c='b' print "\n" next end print i,c," "endputs "\n\n"puts "演示break"c='a'for i in 1..4 if i==2 and c=='a' c='b' print "\n" redo end print i,c," "endputs "\n\n"puts "演示break"c='a'for i in 1..4 if i==2 and c=='a' c='b' print "\n" retry end print i,c," "endputs "\n\n"for i in 2..100 f=true for p in 2..i if i%p==0 and i!=p f=!f break end end print i," " if fend#3.times {print "Hi!"}#9.downto(1) {|i| print i if i <7}#1..9).each {|i| print i if i<7}#1.upto(9) {|i| print i if i<7}#0.step(11,3) {|i| print i}try catch finally throwbegin/end rescue ensure raise谁将被影响 ====来编写代码以类为模块,以消息来驱动程序的执行对象===静态的属性 , 动态的方法子类型从父类型得到属性、方法。我们称之为继承同一个父类,不同子类有不同的行为和状态,我们称之为多态对象的属性、方法只有定义代码的人知道,其它的类不知道,这就是封装封装、继承、多态是面向对象编程的三个本质特征人们可以决定代码世界中一类事物的属性、方法,当然可以修改代码世界中一类事物的属性、方法,而且可以委托其它的类来修改,甚至删除。这就是动态动态语言超越静态语言之处,由于代码是一直运行着的,与其他代码一直交互,修改,删除应慎重,避免产生副作用。attr_writer:motherlanddef motherland=(value) return @motherland=valueendattr_reader:motherlanddef motherland return @motherlandend封装完成了 隐藏实现class Person def initialize(name,age=18) @name =name @age=age @motherland="China" end def talk puts "my name is "+@name+",age is "+@age.to_s if @motherland=="China" puts "I am a Chinese" else puts "I am a foreigner" end end attr_writer:motherlandend#p1=Person.new("kaichun",20)#p1.talk#p2=Person.new("Ben")#p2.motherland ="ABC"#p2.talk puts "****************"class Student < Person def talk puts "I am a student. my name is "+@name+", age is "+@age.to_s endendp3=Student.new("kaichuan",25);p3.talkp4=Student.new("Ben"); p4.talk 继承、重写方法子类继承父类可以重写方法、也可以增加一些新的方法,以增强父类的方法当你new的时候,一个实例就按照蓝图生成了Ruby灵巧、快速,但并不简单!冗余,不可预知冗余性、缺陷性、动态性由编译内核(或解释内核)在运行时刻来判断变量类型的语言,叫动态语言一切都是对象,但是变量不是对象,变量是引用某个对象时候的代号而已a=5b="hh"puts "a=#{5}"puts "b=#{b}"变量类型常量变量名、变量值、变量类型、变量的作用域(1..9).each {|i| print i if i<7}def one_block yield yield yieldendone_block{puts "This is a block. "}def one_block for num in 1..3 yield(num) endendone_block do |i| puts "This is block#{i}."enddef do_something yieldenddo_something do (1..9).each {|i| print i if i<5} putsenddo_something do 3.times {print "Hi!"} putsendclass Array def one_by_one for i in 0..size yield(self[i]) end puts endendarr=[1,3,5,7,9]arr.one_by_one {|k| print k, ","}arr.one_by_one {|h| print h*h, ", "}class Arraydef one_by_onefor i in 0...sizeyield(self[i] )endputsendendarr = [1,3,5,7,9]arr.one_by_one {|k| print k , ", "} # 1, 3, 5, 7, 9,arr.one_by_one {|h| print h*h, ", "} # 1, 9, 25, 49, 81,class Array def one_by_one for i in 0...size yield(self[i]) end puts endendarr=[1,3,5,7,9]arr.one_by_one {|k| print k, ","}arr.one_by_one {|h| print h*h,","}def method(pr) puts pr.call(7)endoneProc=proc {|k| k *=3}method(oneProc)def method(n) return proc {|i| n +=i}endoneProc=method(3)puts oneProc.call(9)puts oneProc.call(5)puts self.private_methods.sortrice_on_square =164.times do |square| puts "On square #{square+1} are #{raice_on_square} gain(s)" rice_on_quare *=2endrice_on_square = 164.times do |square|puts "On square #{square + 1} are #{rice_on_square} grain(s)"rice_on_square *= 2endrice_on_square = 164.times {|square|puts "On square #{square } are #{rice_on_square} grain(s)"rice_on_square *= 2}#do end 替代{}#相当是一个循环!watir已经搭建好了!require "watir" test_site = "http://www.google.com" ie = Watir::IE.new puts "Beginning of test: Google search." #puts " Step 1: go to the test site: " + test_site ie.goto test_site ie.text_field(:name, "q").set "pickaxe"ie.button(:name, "btnG").clickif ie.text.include? "Programming Ruby" puts " Test Passed. Found the test string: 'Programming Ruby'. Actual Results match Expected Results." else puts " Test Failed! Could not find: 'Programming Ruby'." end puts "End of test: Google search."=end
收藏
举报
TAG: