#“从#开始到行尾是单行注释”
puts 3/5
puts 3/5.0
=begin
puts 6/5
puts 6/5.0
多行注释可以用=begin 和 =end
实际上,这是Ruby的内嵌文档Rdoc,类似javadoc ,
可以用命令 ri 从源文件生产文档。
=end#演示分行
puts 3/5 ; puts 3/5.0
puts "这里演示" /
"连行"#循环
a=1
while a <10
print a," "
a=a+1
end
# E3.10-2.rba=1
until a >=10
print a," "
a=a+1
end# E3.10-3.rb
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'
c = 'b'
print "/n"
break
end
print i,c," "
end
puts "/n/n"
puts "演示next"
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 "演示redo"
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 "演示retry"
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"#求50以内的素数
for i in 2..50 #50以内
f=true
for p in 2...i
if i%p==0
f=!f
break
end
end
print i," " if f
end#演示times, upto, downto, each, step
3.times { print "Hi!" }
puts
1.upto(9) {|i| print i if i<7 }
puts
9.downto(1){|i| print i if i<7 }
puts
(1..9).each {|i| print i if i<7}
puts
0.step(11,3) {|i| print i } #求50以内的素数
$arr=[ ] #建立一个全局数组 $arr
$arr[0]=2 def add_prime(n) #定义方法 将 n以内的奇素数加入$arr
3.step(n,2){|num|$arr <<num if is_prime?num }
end
def is_prime?(number) #定义方法 判断一个数是否是素数
j=0 #数组下标
while $arr[j] * $arr[j] <=number
return false if number % $arr[j] ==0
j +=1
end
return true
endadd_prime(50)
print $arr.join(", "),"/n" #转换成字符串输出
#赋值和关系运算符
print "赋值/n"
a = 1 ;b = 2 + 3 ;print "a=",a," b=",b,"/n"
a ,b = b ,a ;print "a=",a," b=",b,"/n"
a = b = 1 + 2 + 3 ;print "a=",a," b=",b,"/n"
a = (b = 1 + 2) + 3 ;print "a=",a," b=",b,"/n"
x = 0 ;print "/n"
a,b,c = x, (x+1), (x+2) ;print "a=",a," b=",b," c=",c,"/n" print "关系运算符/n"
a=1; b=1.0; puts a==b
a=1; b=1.0; puts a.eql?(b)
a=1.0; b=1.0; puts a.equal?(b)
a=1.0; b=a ; puts a.equal?(b)
puts "aab" <=> "acb"
puts [5] <=> [4,9]
puts (0..9)=== 3.14
puts ('a'..'f')=== 'c'
# E3.9-1.rb
x=3
case x
when 1...2
print "x=",x,";在 1...2中"
when 4..9,0
print "x=",x,";在 4..9,0中,或是0"
else
print "x=",x,";其它可能"
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/'m Chinese."
else
puts "I/'m foreigner."
end
end
attr_writer :motherlandend
p1=Person.new("kaichuan",20)
p1.talkp2=Person.new("Ben")
p2.motherland="ABC"
p2.talk#E4.4-1.rb 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
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#E4.5-1.rb
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
endclass Worker < Person
def talk
puts "I am a worker. my name is "+@name+", age is "+@age.to_s end
endp5=Worker.new("kaichuan",30);p5.talk
p6=Worker.new("Ben");p6.talk# E5.3-1.rb
a=5
b="hh"
puts "a = #{a}"
puts "b = #{b}"
# E5.3-2.rb
a=5
print "a = ",a," ", a.class, "/n"a="hh" # a: 5 --> "hh"
print "a = ",a," ", a.class, "/n"
# E5.4-1.rb
class Person
def talk
puts "Today is Saturday. "
end
endp1=Person.new
p1.talkclass Person
def talk
puts "Today is #@date. "
end
attr_accessor :date
end
p1.date="Sunday"
p1.talk# E5.4-2.rb
class Person
def talk
puts "Today is Saturday. "
end
endp1=Person.new
p1.talkclass Person
undef :talk
end
#p1.talk
ruby命令的执行方法 ruby 实例
转载本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章