Ruby编程之数组、多维数组和遍历
【摘要】 笔者主要在博客中介绍了Ruby编程数组和多维数组的小例子,并写出它们对应的遍历方法
def hello
return "hello world"
end
x = [1+2, hello, `dir`]
y = %w( this is an array of strings )
for i in x
puts(i.inspect)
end
for i in y
puts(i.inspect)
end
3
"hello world"
" \xC7\xFD\xB6\xAF\xC6\xF7 C \xD6еľ\xED\xCA\xC7 windows\n \xBE\xED\xB5\xC4\xD0\xF2\xC1к\xC5\xCA\xC7 EEAC-26C7\n\n C:\\Users\\Administrator\\RubymineProjects\\untitled \xB5\xC4Ŀ¼\n\n2022/11/01 08:18 <DIR>
"this"
"is"
"an"
"array"
"of"
"strings"
a = Array.new # an empty array
for i in a
puts(i.inspect)
end
a = Array.new(2) # [nil,nil]
for i in a
puts(i.inspect)
end
a = Array.new(2,"hello world") # ["hello world","hello world"]
for i in a
puts(i.inspect)
end
a = Array.new(2)
for i in a
puts(i.inspect)
end
a[0]= Array.new(2,'hello')
for i in a[0]
puts(i.inspect)
end
a[1]= Array.new(2,'world')
for i in a[1]
puts(i.inspect)
end
a = [ [1,2,3,4],
[5,6,7,8],
[9,10,11,12],
[13,14,15,16] ]
for i in a
puts(i.inspect)
end
multiarr = [['one','two','three','four'],[1,2,3,4]]
for i in multiarr
puts(i.inspect)
end
nil
nil
"hello world"
"hello world"
nil
nil
"hello"
"hello"
"world"
"world"
[1, 2, 3, 4]
[5, 6, 7, 8]
[9, 10, 11, 12]
[13, 14, 15, 16]
["one", "two", "three", "four"]
[1, 2, 3, 4]
开发环境:Rubymine
The book of Ruby----A hands-on guide for the Adventurous---[美]Huw Collingbourne---no starch press
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区),文章链接,文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件至:cloudbbs@huaweicloud.com进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容。
- 点赞
- 收藏
- 关注作者
评论(0)