ruby笔记

上一篇 / 下一篇  2011-11-02 09:03:36 / 个人分类:Ruby

#puts "hello world!"
#::
=begin
module
class
def,undef
defined?
if,then,else,elsif,case,when,unless
for,in,while,until,next,break,do,redo,
retry,yield
not,and,or
true,false,nil
rescue,ensure
super,self
begin/end
BENGIN END
_FILE_,_LINE_
return
alias
require,include

[] []=
**
!~+-
*/%
+-
>> <<
<= < > >=
<=> == ===
=~ != !~
&&
||
.. ...
defined?
not
or and
if unless while until
begin/end
局部变量、方法参数、方法名用一个小写字母开头
或者用一个下划线开头,全局变量用美元符号作为前缀
¥,实例变量用@开头,类变量用@@开头
类名、模块名、常量用大写字母开头。

词首字母后面可以是字母、数字和下划线的任意组合
@后面不可以直接跟数字


类库

require include将类库程序名包含在已经定义好的
类、方法。从父类继承得到的方法可以直接使用

I/O 输入/输出 Ruby语言Kernel模块的方法,Mix-in在根类的Object中


puts带换行符号,print不带换行符号
printf "Number:%4.3f,string:%s",7.8,"hi!" \n

gets

=end


=begin
数据类型

浮点型数据小数点后必须跟数字1.1e3
0表示八进制,0x表示十六进制,0b表示二进制

字符串是在单引号,双引号之间的代码

1..5    1,2,3,4,5 [1,5]
1...5   1,2,3,4   [1,5)


    
a =1;b=2+3
a,b=b,a#值互换
print a ; print b


x=0
a,b,c=x,(x+1),(x+2)
puts a,b,c
print a,b,c
print a


a=1;b=1.0;print a==b

a=1;b=1.0;
puts a.eql?(b)

a=1;b=1.0;
print a.equal?(b)


a=1.0;b=a
print a.equal?(b)


puts 'aab'<=>'acb'
puts     [5]<=>[4,9]



puts (0..9)===3.14
puts ('a'..'c')==='c'
=~用来比较是否符合一个正则表达式,返回模式在字符串中被匹配到得位置,否则返回nil
!~断言不符合一个正则表达式,返回true,false


if 条件 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=3
case 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 =1
until a>=10
print a," "
a=a+1
end


a=1
while  not a>=10
    print a," "
a=a+1
end



until= while not   
 

for i  in 1..9
    print i, " "
end

break
next
redo
retry



puts "演示break"
c='a'
for i in 1..4
    if i==2 and c=='a'
        print "\n"
        break
    end
    print i,c," "
end
puts "\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," "
end
puts "\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," "
end
puts "\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," "
end
puts "\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 f
end




#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 throw
begin/end rescue ensure raise



谁将被影响 ====来编写代码
以类为模块,以消息来驱动程序的执行


对象===静态的属性 , 动态的方法
子类型从父类型得到属性、方法。我们称之为继承

同一个父类,不同子类有不同的行为和状态,我们称之为多态

对象的属性、方法只有定义代码的人知道,其它的类不知道,这就是封装

封装、继承、多态是面向对象编程的三个本质特征



人们可以决定代码世界中一类事物的属性、方法,当然可以修改代码世界中
一类事物的属性、方法,而且可以委托其它的类来修改,甚至删除。这就是动态
动态语言超越静态语言之处,由于代码是一直运行着的,与其他代码一直交互,修改,删除应慎重,避免产生副作用。




attr_writer:motherland

def motherland=(value)
    return @motherland=value
end




attr_reader:motherland
def motherland
    return @motherland
end

封装完成了 隐藏实现

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:motherland
end

#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
    end
end
p3=Student.new("kaichuan",25);p3.talk
p4=Student.new("Ben"); p4.talk
       
   
   
继承、重写方法
子类继承父类可以重写方法、也可以增加一些新的方法,以增强父类的方法

当你new的时候,一个实例就按照蓝图生成了






Ruby灵巧、快速,但并不简单!
冗余,不可预知
冗余性、缺陷性、动态性


由编译内核(或解释内核)在运行时刻来判断变量类型的语言,叫动态语言

一切都是对象,但是变量不是对象,变量是引用某个对象时候的代号而已




a=5
b="hh"
puts "a=#{5}"
puts "b=#{b}"



变量类型
常量


变量名、变量值、变量类型、变量的作用域



(1..9).each {|i| print i if i<7}


def one_block
    yield
    yield
    yield
end
one_block{puts "This is a block. "}



def one_block
    for num in 1..3
        yield(num)
    end
end
one_block do |i|
    puts "This is block#{i}."
end


def do_something
    yield
end

do_something do
    (1..9).each {|i| print i if i<5}
    puts
end

do_something do
    3.times {print "Hi!"}
    puts
end



class Array
    def one_by_one
        for i in 0..size
            yield(self[i])
        end
        puts
    end
end

arr=[1,3,5,7,9]
arr.one_by_one {|k| print k, ","}
arr.one_by_one {|h| print h*h, ", "}

class Array
def one_by_one
for i in 0...size
yield(self[i] )
end
puts
end
end
arr = [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
    end
end
arr=[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)
end

oneProc=proc {|k|  k *=3}
method(oneProc)





def method(n)
    return proc {|i| n +=i}
end

oneProc=method(3)


puts oneProc.call(9)
puts oneProc.call(5)




puts self.private_methods.sort




rice_on_square =1
64.times do |square|
    puts "On square #{square+1} are #{raice_on_square} gain(s)"
    rice_on_quare *=2
end


rice_on_square = 1
64.times do |square|
puts "On square #{square + 1} are #{rice_on_square} grain(s)"
rice_on_square *= 2
end



rice_on_square = 1
64.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").click

if 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:

 

评分:0

我来说两句

Open Toolbar