#
coding:utf-8
# Blocks and Iterators(代码块和迭代器)
# times and upto downto是由Integer实现的迭代器
# times {|i| block }:Iterates block int times, passing in values from zero to int - 1.
puts " #times {|i| block }: "
5.times do |i|
print i, " "
print " \n "
end
3.times{ print " Welcome to Ruby ,Jeriffe\n "}
# upto(limit) {|i| block }:Iterates block, passing in integer values from int up to and including limit.
print " \n "
puts " #upto(limit) {|i| block }: "
1.upto(9){|x| print x, " "}
print " \n "
# downto(limit) {|i| block }:Iterates block, passing in integer values from int down to and including limit.
print " \n "
puts " #downto(limit) {|i| block }: "
9.downto(1){|x| print x, " "}
print " \n "
# 数组(及类似的”可枚举的“对象)定义了一个each迭代器
# each {|item| block }:Calls block once for each element in self, passing that element as a parameter.
print " \n "
puts " #each {|item| block }: "
a=[3,2,1]
a.each{|item| print item, ' '}
print " \n "
# 在each迭代器基础上定义的其他迭代器
print " \n "
puts " #在each迭代器基础上定义的其他迭代器: "
a=[1,2,3,4]
b=a.map{|item| item*item}
print b, " \n "
c=a.select{|item| item%2==0}
print c, " \n "
d=a.inject do|sum,item|
sum+item
end
print d, " \n "
# Methods(方法)
# 方法用def关键字来定义,方法的返回值是方法最后一个被执行的表达式
def square(x)
x*x
end
print square(5), " \n "
# Assignment(赋值)
# 除传统的=操作符赋值外,Ruby还支持并行赋值:一个表达式中出现多余一个的值和变量
puts " 并行赋值: "
x,y=1,2
print " x= ",x, ' ,y= ',y, " \n "
x,y=y,x
print " x= ",x, ' ,y= ',y, " \n "
# 方法返回多个值
puts " #方法返回多个值: "
def polar(x,y)
theta=Math.atan2(y,x)
r=Math.hypot(x,y)
[r,theta]
end
distance,angle=polar(2,3)
print " distance= ",distance, ' ,angle= ',angle, " \n "
# Blocks and Iterators(代码块和迭代器)
# times and upto downto是由Integer实现的迭代器
# times {|i| block }:Iterates block int times, passing in values from zero to int - 1.
puts " #times {|i| block }: "
5.times do |i|
print i, " "
print " \n "
end
3.times{ print " Welcome to Ruby ,Jeriffe\n "}
# upto(limit) {|i| block }:Iterates block, passing in integer values from int up to and including limit.
print " \n "
puts " #upto(limit) {|i| block }: "
1.upto(9){|x| print x, " "}
print " \n "
# downto(limit) {|i| block }:Iterates block, passing in integer values from int down to and including limit.
print " \n "
puts " #downto(limit) {|i| block }: "
9.downto(1){|x| print x, " "}
print " \n "
# 数组(及类似的”可枚举的“对象)定义了一个each迭代器
# each {|item| block }:Calls block once for each element in self, passing that element as a parameter.
print " \n "
puts " #each {|item| block }: "
a=[3,2,1]
a.each{|item| print item, ' '}
print " \n "
# 在each迭代器基础上定义的其他迭代器
print " \n "
puts " #在each迭代器基础上定义的其他迭代器: "
a=[1,2,3,4]
b=a.map{|item| item*item}
print b, " \n "
c=a.select{|item| item%2==0}
print c, " \n "
d=a.inject do|sum,item|
sum+item
end
print d, " \n "
# Methods(方法)
# 方法用def关键字来定义,方法的返回值是方法最后一个被执行的表达式
def square(x)
x*x
end
print square(5), " \n "
# Assignment(赋值)
# 除传统的=操作符赋值外,Ruby还支持并行赋值:一个表达式中出现多余一个的值和变量
puts " 并行赋值: "
x,y=1,2
print " x= ",x, ' ,y= ',y, " \n "
x,y=y,x
print " x= ",x, ' ,y= ',y, " \n "
# 方法返回多个值
puts " #方法返回多个值: "
def polar(x,y)
theta=Math.atan2(y,x)
r=Math.hypot(x,y)
[r,theta]
end
distance,angle=polar(2,3)
print " distance= ",distance, ' ,angle= ',angle, " \n "