Ruby on Rails 入门之:(9) Ruby中Hash对象与数组
1. Hash对象
Ruby中Hash对象的使用,键值对使用=>分隔开。
如代码所示:
[ruby]
#encoding:gbk
Stus = {"tom"=>"42","Aaron"=>"29","Lucy"=>"32"};
for key,value in Stus
puts key,value;
end
puts "***********************"
Stus.each do |key,value|
puts key,value;
end
puts Stus.size().to_s;
#encoding:gbk
Stus = {"tom"=>"42","Aaron"=>"29","Lucy"=>"32"};
for key,value in Stus
puts key,value;
end
puts "***********************"
Stus.each do |key,value|
puts key,value;
end
puts Stus.size().to_s;
还可以使用Hash.new创建一个新的Hash对象,然后给这个对象添加键值对。
[ruby]
#encoding:gbk
Stus = {"tom"=>"42","Aaron"=>"29","Lucy"=>"32"};
for key,value in Stus
puts key,value;
end
puts "***********************"
Stus.each do |key,value|
puts key,value;
end
puts Stus.size().to_s;
S = Hash.new
S["wang"]="28";
S["phone"]=123456789;
S["易做图"]="male";
puts "************************";
S.each do |key,value|
puts key,value;
end;
#encoding:gbk
Stus = {"tom"=>"42","Aaron"=>"29","Lucy"=>"32"};
for key,value in Stus
puts key,value;
end
puts "***********************"
Stus.each do |key,value|
puts key,value;
end
puts Stus.size().to_s;
S = Hash.new
S["wang"]="28";
S["phone"]=123456789;
S["易做图"]="male";
puts "************************";
S.each do |key,value|
puts key,value;
end;
2. 数组
数组的创建方式和Hash对象差不多,可以直接创建,也可以通过Array.new,然后添加元素。
[ruby]
#encoding:gbk
Foods = ["饺子","面条","馄饨"];
for f in Foods
puts f;
end
puts "********************";
person = Array.new
person[0]="a";
person[1]="b";
person[2]="c";
for p in person
puts p;
end
#encoding:gbk
Foods = ["饺子","面条","馄饨"];
for f in Foods
puts f;
end
puts "********************";
person = Array.new
person[0]="a";
person[1]="b";
person[2]="c";
for p in person
puts p;
end
数组的%表示方式
数组还可以通过%来进行表示
%W: 会对字符进行转义
%w: 不会对字符进行转义
[ruby]
#encoding:gbk
Foods = ["饺子","面条","馄饨"];
for f in Foods
puts f;
end
puts "********************";
person = Array.new
person[0]="a";
person[1]="b";
person[2]="c";
for p in person
puts p;
end
str = %W{上海\s 北京\s 广州\s};
puts str;
str = %w{上海\s 北京\s 广州\s};
puts str;
#encoding:gbk
Foods = ["饺子","面条","馄饨"];
for f in Foods
puts f;
end
puts "********************";
person = Array.new
person[0]="a";
person[1]="b";
person[2]="c";
for p in person
puts p;
end
str = %W{上海\s 北京\s 广州\s};
puts str;
str = %w{上海\s 北京\s 广州\s};
puts str;
上面代码的输出结果:
[ruby]
watkins@watkins:~/temp/workspace/ruby$ ruby array.rb
饺子
面条
馄饨
********************
a
b
c
上海
北京
广州
上海\s
北京\s
广州\s
watkins@watkins:~/temp/workspace/ruby$
watkins@watkins:~/temp/workspace/ruby$ ruby array.rb
饺子
面条
馄饨
********************
a
b
c
上海
北京
广州
上海\s
北京\s
广州\s
watkins@watkins:~/temp/workspace/ruby$
可以使用<<符号向数组中追加元素;
[ruby]
#encoding:gbk
Foods = ["饺子","面条","馄饨"];
Foods << "肉加馍";
for f in Foods
puts f;
end
puts "********************";
person = Array.new
person[0]="a";
person[1]="b";
person[2]="c";
for p in person
puts p;
end
str = %W{上海\s 北京\s 广州\s};
puts str;
str = %w{上海\s 北京\s 广州\s};
puts str;
#encoding:gbk
Foods = ["饺子","面条","馄饨"];
Foods << "肉加馍";
for f in Foods
puts f;
end
puts "********************";
person = Array.new
person[0]="a";
person[1]="b";
person[2]="c";
for p in person
puts p;
end
str = %W{上海\s 北京\s 广州\s};
puts str;
str = %w{上海\s 北京\s 广州\s};
puts str;
补充:Web开发 , 其他 ,