您的位置:首页 > 编程语言 > Ruby

ruby入门示例2

2008-05-20 09:05 447 查看
#E5.5-1.rb

def talk name
"Hi! " + name
end

puts talk "kaichuan"
puts talk("kaichuan")
puts (talk "kaichuan")
puts (talk("kaichuan"))
#E5.5-2.rb

a=5
b=3
puts a>b ? "a>b" : "a<=b"
puts (a>b)? ("a>b") : ("a<=b")
#puts a>b? "a>b" : "a<=b"
#E5.5-3.rb

a = 3
b = 1; a += b if 3>5
print "a = ", a, "/n"
print "b = ", b, "/n"

c = 3
(d = 1; c += d) if 3>5
print "c = ", c, "/n"
print "d = ", d, "/n"
# E6.1-1.rb

def sum( a, b=5 )
a+b
end
puts sum(3,6)
puts sum(3)
# E6.1-2.rb

def sum(*num)
numSum = 0
num.each { |i| numSum+=i }
return numSum
end

puts sum()
puts sum(3,6)
puts sum(1,2,3,4,5,6,7,8,9)
# E6.1-3.rb

def talk(a)
puts "This is talk version 1."
end

def talk(a,b=1)
puts "This is talk version 2."
end

talk(2)
talk(2,7)
#E6.2-1.rb

class Person
def talk(name)
print "my name is #{name}."
end
end

class Student < Person
def talk(name)
super
print "and I'm a student./n"
end
end

aPerson=Person.new
aPerson.talk("kaichuan")
print "/n/n"

aStudent=Student.new
aStudent.talk("kaichuan")
#E6.3-1.rb

class StudentClass
@@count=0
def initialize( name )
@name = name
@@count+=1
end

def talk
puts "I am #@name, This class have #@@count students."
end

end

p1=StudentClass.new("Student 1 ")
p2=StudentClass.new("Student 2 ")
p3=StudentClass.new("Student 3 ")
p4=StudentClass.new("Student 4 ")
p3.talk
p4.talk
#E6.3-2.rb

class StudentClass
@@count=0

def initialize
@@count+=1
end

def StudentClass.student_count
puts "This class have #@@count students."
end

end

p1=StudentClass.new
p2=StudentClass.new
StudentClass.student_count

p3=StudentClass.new
p4=StudentClass.new
StudentClass.student_count
#E6.4-1.rb

class Person
def talk
puts "Hello! "
end
end

p1=Person.new
p2=Person.new

def p2.talk
puts "Here is p2. "
end

def p2.laugh
puts "ha,ha,ha... "
end

p1.talk
p2.talk
p2.laugh
#E6.5-1.rb

class Person
def talk
puts " public :talk, 将调用speak"
speak
end

def speak
puts "protected:speak,将调用laugh"
laugh
end

def laugh
puts " private:laugh"
end

protected :speak
private :laugh
end

p1=Person.new
p1.talk
#p1.speak
#p1.laugh
#E6.5-2.rb

class Person
def speak
"protected:speak "
end

def laugh
" private:laugh"
end

protected :speak
private :laugh
end

class Student < Person

def useLaugh
puts laugh
end

def useSpeak
puts speak
end

end

p2=Student.new
p2.useLaugh
p2.useSpeak
#E6.5-3.rb

class Person
def speak
"protected:speak "
end

def laugh
" private:laugh"
end

protected :speak
private :laugh

def useLaugh(another)
puts another.laugh
end

def useSpeak(another)
puts another.speak
end

end

p1=Person.new
p2=Person.new

p2.useSpeak(p1)
#p2.useLaugh(p1)
#E6.5-4.rb

class Person
private #后面的方法设定为private
def talk
puts " already talk "
end
end

p1=Person.new
#p1.talk private方法不能访问

class Person
public :talk
end

p1.talk
#E7.1-1.rb

puts Math.sqrt(2)
puts Math::PI
#E7.2-1.rb

module Me

def sqrt(num, rx=1, e=1e-10)
num*=1.0
(num - rx*rx).abs <e ? rx : sqrt(num, (num/rx + rx)/2, e)
end

end

include Math
puts sqrt(293)
#puts sqrt(293, 5, 0.01)

include Me
puts sqrt(293)
puts sqrt(293, 5, 0.01)
#E7.2-2.rb

module Me
def sqrt(num, rx=1, e=1e-10)
num*=1.0
(num - rx*rx).abs <e ? rx : sqrt(num, (num/rx + rx)/2, e)
end
end

module Me2
def Me2.sqrt(*num)
"This is text sqrt. "
end
PI=3.14
end

puts Math.sqrt(1.23)
puts Math::PI
puts Me2.sqrt(55, 66, 77, 88, 99)
puts Me2::PI

include Me
puts sqrt(456, 7, 0.01)
#E7.3-1.rb

module Me
def sqrt(num, rx=1, e=1e-10)
num*=1.0
(num - rx*rx).abs <e ? rx : sqrt(num, (num/rx + rx)/2, e)
end
end

class Person
def talk
puts "I'm talking."
end
end

class Student < Person
include Me
end

aStudent=Student.new
aStudent.talk
puts aStudent.sqrt(20.7,3.3)
#E7.3-2.rb

module Me
def sqrt(num, rx=1, e=1e-10)
num*=1.0
(num - rx*rx).abs <e ? rx : sqrt(num, (num/rx + rx)/2, e)
end
end

class Student
end

aStudent=Student.new
aStudent.extend(Me)
puts aStudent.sqrt(93.1, 25)
#E7.4-1.rb

module Me
def sqrt(num, rx=1, e=1e-10)
num*=1.0
(num - rx*rx).abs <e ? rx : sqrt(num, (num/rx + rx)/2, e)
end
end
#E7.4-2.rb

class Person
def talk
puts "I'm talking."
end
end
#E7.4-3.rb
require "E7.4-1"
require "E7.4-2"

class Student < Person
include Me
end

aStudent=Student.new
aStudent.talk
puts aStudent.sqrt(77,2)
#E7.4-4.rb
load "E7.4-1.rb"

class Student
end

aStudent=Student.new
aStudent.extend(Me)
puts aStudent.sqrt(100.1, 12)
#E8.1-1.rb

arr1=[]
arr2=Array.new
arr3=['4 ','5 ','6 ']

print arr1, "/n"
print arr2, "/n"
print arr3, "/n"#E8.1-2.rb

arr=[3,4,5,6,7,8,9]

puts arr[0]
puts arr.first
puts arr[arr.length-1]
puts arr[arr.size-1]
puts arr.last
puts arr[-1]
puts arr[-2]
print arr[1..3] ,"/n"
print arr[-3,2] ,"/n"
#E8.1-3.rb

arr=[4,5,6]
print arr.join(", "),"/n"

arr[4] = "m"
print arr.join(", "),"/n"
print arr[3] ,"/n"

arr.delete_at(3)
print arr.join(", "),"/n"

arr[2] = ["a","b","c"]
print arr.join(", "),"/n"
print arr[2] ,"/n"

arr[0..1] = [7,"h","b"]
print arr.join(", "),"/n"

arr.push("b" )
print arr.join(", "),"/n"

arr.delete(["a","b","c"] )
print arr.join(", "),"/n"
arr.delete("b")
print arr.join(", "),"/n"

arr.insert(3,"d")
print arr.join(", "),"/n"

arr<<"f"<<2
print arr.join(", "),"/n"
arr.pop
print arr.join(", "),"/n"
arr.shift
print arr.join(", "),"/n"

arr.clear
print arr.join(", "),"/n"#E8.1-4.rb

aaaa=[" aa ",4,5," bb "]
bbbb=[4,1,3,2,5]

print aaaa + bbbb ,"/n"
print aaaa * 2 ,"/n"
print bbbb - aaaa ,"/n"

print aaaa | bbbb ,"/n"
print aaaa & bbbb ,"/n"

print bbbb.sort ,"/n"
print aaaa.reverse ,"/n"
#E8.2-1.rb

str1='this is str1'
str2="this is str2"
str3=%q[this is str3]
str4=%Q{this is str4}
str5=<<OK_str
Here is string document, str5
line one;
line two;
line three.
OK
OK_str

puts str3
puts str4
puts str5#E8.2-2.rb

str = ' this' + " is"
str += " you"
str << " string" << " ."

puts str*2
puts str[-12,12]#E8.2-3.rb

str = " this is you string ."
puts str*2

str = " this is you string ./n"
puts str*2

str = " /tthis is you string ."
puts str

str = ' this/'s you string ./n'
puts str#E8.2-4.rb

def hello(name)
" Welcome, #{name} !"
end

puts hello("kaichuan")
puts hello("Ben")#E8.3-1.rb

str="Hello,kaichuan,Welcome!"

puts str =~ /kaichuan/
puts str =~ /a/
puts str =~ /ABC/
#E8.3-2.rb

str="Hello,kaichuan,Welcome!"

puts str !~ /kaichuan/
puts str !~ /a/
puts str !~ /ABC/
#E8.3-3.rb

strdoc=<<DOC_EOF
This is windows2000 or windows98 system.
Windows system is BEST?
Windows2000 running in 12-31-2006,……
DOC_EOF

re = /[w|W]indows(?:98|2000) /
strdoc.gsub!(re, "Windows XP ")
re = /[1-9][0-9]/-[1-9][0-9]/-/d/d/d/d/
time = Time.now.strftime("%m-%d-%Y")
strdoc.gsub!(re, time)
puts strdoc
#E8.4-1.rb

def one_block
yield
yield
yield
end

one_block { puts "This is a block. " }#E8.4-2.rb

def one_block
for num in 1..3
yield(num)
end
end

one_block do |i|
puts "This is block #{i}. "
end#E8.4-3.rb

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
#E8.4-4.rb

def method(pr)
puts pr.call(7)
end

oneProc=proc{|k| k *=3 }
method(oneProc)
#E8.4-5.rb

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

oneProc=method(3)
puts oneProc.call(9)
puts oneProc.call(5)
#E8.4-6.rb

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, ", "}
#E9-1.rb

class MetaPerson

def MetaPerson.method_missing(methodName, *args)
name = methodName.to_s
begin
class_eval(%Q[
def #{name}
puts '#{name}, #{name}, #{name}...'
end
])
rescue
super(methodName, *args)
end
end

def method_missing(methodName, *args)
MetaPerson.method_missing(methodName, *args)
send(methodName)
end

def MetaPerson.modify_method(methodName, methodBody)
class_eval(%Q[
def #{methodName}
#{methodBody}
end
])
end

def modify_method(methodName, methodBody)
MetaPerson.modify_method(methodName, methodBody)
end

end
#E9-2.rb

require "E9-1"

class Person < MetaPerson

end

person1 = Person.new
person2 = Person.new

person1.sleep
person1.running

person1.modify_method("sleep", "puts 'ZZZ...'")
person1.sleep
person2.sleep

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: