标签:而不是 函数的参数 st3 pre nbsp sleep ruby thread 必须
1、实现ruby中的多线程
# def test1
# n = 1
# if n > 10
# puts "test1结束"
# else
# while true
# sleep 2
# puts n
# n = n + 1
# end
# end
# end
#
#
# def test2
# n = 100
# if n > 100
# puts "test2结束"
# else
# while true
# sleep 2
# puts n
# n = n + 10
# end
# end
# end
#
# t1 = Thread.new{test1()}
# t2 = Thread.new{test2()}
#
# t1.join
# t2.join
#
2、实现ruby中的关键字传参,这里要用冒号,而不是等号
def test(a:"a1",b:"b1",c:"c1") puts a puts b puts c end test(a:"a2",c:"c2")
3、ruby中普通参数和关键字参数混合使用
def test1(d,a:"a1",b:"b1",c:"c1") # 这里的普通参数必须要放在前面,放在后面会报错的 puts a puts b puts c puts d end test1(1) test1(1,c:"c2")
4、ruby函数关键字和普通参数混用,传递一个Hash,函数的参数使用传递的值和hash中的值
args = {"a":"a11","b":"b11","c":"c11"}
def test2(d,a:"a1",b:"b1",c:"c1")
# 这里的普通参数必须要放在前面,放在后面会报错的
puts "test22222222222222"
puts a
puts b
puts c
puts d
end
5、ruby函数关键字参数和普通参数混用,函数使用默认值和hash两种
args = {"a":"a11","c":"c11"}
def test3(d,a:"a1",b:"b1",c:"c1")
# 这里的普通参数必须要放在前面,放在后面会报错的
puts "test333333333333"
puts a
puts b
puts c
puts d
end
test3(2,args)
标签:而不是 函数的参数 st3 pre nbsp sleep ruby thread 必须
原文地址:https://www.cnblogs.com/bainianminguo/p/11283909.html