发新话题
打印

Ruby程序快速入门之数据结构

Ruby程序快速入门之数据结构

  就象许多程序语言一样,Ruby也提供了完整的数据结构来存储和管理数据对象。数组是使用方括号和用逗号隔开的单个对象参考列表创建的。

presidents=["John","Richard","Gerald","Ronald","George","William"];

  为了更为容易地创建一个充满单词的数组,Ruby提供了一个特殊的标志来消除双引号和逗号,见下面例子:

presidents= %w[ John Richard Gerald Ronald George William];

  在其它编程语言中,"数组"一词经常意味着一组相同性质的对象的集合。但在Ruby中,不是这样。在Ruby中,一个"数组"可以是由不同性质的对象参考组成的集合。因此,下面是有效的数组表达形式:

order_date=Date.today()
shirt_information=[14.5,"Long",32,order_date]

  在这个数组中,对象参考按顺序存储并索引。象Java一样,索引从0开始,索引可用来从数组中检索对象参考。下面的示例中请求的是,上面创建的shirt_information数组中的第3个元素(索引为2)。注意,你可以使用方括号标志或at方法来检索数组中的对象参考。

irb(main):003:0> shirt_information[2]
=> 32
irb(main):004:0> shirt_information.at(2)
=> 32

  有趣的是,你还可以使用一个负数索引来引用数组中的元素。一个负数索引是从数组尾部开始计数的。

irb(main):005:0> shirt_information[-3]
=> "Long"

  数组是动态的,意味着数组的大小可以根据你的操作而动态地改变。你可以使用[index]=操作符来添加或替换一个数组中的元素。

irb(main):013:0> shirt_information
=> [14.5, "Long", 32, #<Date: 4907585/2,0,2299161>]
irb(main):014:0> shirt_information[1]="Medium" #change shirt length
=> "Medium"
irb(main):015:0> shirt_information[4]=49.99 #add shirt cost
=> 49.99
irb(main):016:0> shirt_information
=> [14.5, "Medium", 32, #<Date: 4907585/2,0,2299161>, 49.99]

  你也可以使用数字对和范围来由数组的一部分创建一个新数组,通过使用一个[开始索引,元素数目]标志或[开始索引...结束索引]标志。

irb(main):019:0> shirt_information
=> [14.5, "Long", 32, #<Date: 4907585/2,0,2299161>, 49.99]
irb(main):020:0> shirt_dimensions = shirt_information[0,3]
=> [14.5, "Long", 32]
irb(main):021:0> shirt_order = shirt_information[2..5]
=> [32, #<Date: 4907585/2,0,2299161>, 49.99]
irb(main):030:0> shirt_information[-3,2]
=> [32, #<Date: 4907585/2,0,2299161>]

  这个结合了赋值运算符([]=)的标志将产生一个很复杂的元素插入或代替操作。一个开始/结束索引或范围可以用在赋值操作符中。具体使用请参考下面的示例:

irb(main):001:0> test_array=["zero", "one", "two", "three", "four"]
=> ["zero", "one", "two", "three", "four"]
irb(main):002:0> #starting at the second element, replace the next
two elements with a single element
irb(main):003:0* test_array[1,2]=1
=> 1
irb(main):004:0> test_array
=> ["zero", 1, "three", "four"]
irb(main):005:0> #insert a new element after the second one
(zero as a second parameter indicates "insert")
irb(main):006:0* test_array[2,0]=2
=> 2
irb(main):007:0> test_array
=> ["zero", 1, 2, "three", "four"]
irb(main):008:0> #add an array of elements after element 5
irb(main):009:0* test_array[5,0]=[5,6,7]
=> [5, 6, 7]
irb(main):010:0> test_array
=> ["zero", 1, 2, "three", "four", 5, 6, 7]
irb(main):011:0> #replace elements in the index range of 3..4 with the array assigned
irb(main):012:0* test_array[3..4]=[3,4]
=> [3, 4]
irb(main):013:0> test_array
=> ["zero", 1, 2, 3, 4, 5, 6, 7]

  最后,也许Ruby数组中最有力量的运算可以从数学操作符中找到答案-它们能够从现在数组创建新的数组。例如,+操作符允许你由两个数组的连接创建一个新数组,而*操作符允许你复制或连接一个数组自身若干次。

irb(main):033:0> shirt_information
=> [14.5, "Long", 32, #<Date: 4907585/2,0,2299161>, 49.99]
irb(main):034:0> pant_information=[34,32,59.99,order_date]
=> [34, 32, 59.99, #<Date: 4907585/2,0,2299161>]
irb(main):035:0> shirt_information + pant_information
=> [14.5, "Long", 32, #<Date: 4907585/2,0,2299161>, 49.99, 34, 32, 59.99,
#<Date: 4907585/2,0,2299161>]
irb(main):036:0> shirt_information * 2
=> [14.5, "Long", 32, #<Date: 4907585/2,0,2299161>, 49.99, 14.5, "Long", 32,
#<Date: 4907585/2,0,2299161>, 49.99]
irb(main):037:0> array1 = [2,4,6,8,10]
=> [2, 4, 6, 8, 10]
irb(main):038:0> array2=[3,6,9]
=> [3, 6, 9]
irb(main):039:0> array1 - array2
=> [2, 4, 8, 10]

  在Ruby中,与数组联系最密切的就是哈希表。在一个数组中,索引是一个整数。在Ruby中Hash类行为上与Array非常相似,但是它允许在集合中出现到参考对象的任何类型的对象"索引"(或键)。在另外的程序语言中,这可能被称为一个字典或地图或哈希地图。一般地,当使用哈希表时,你应该为集合中的每个元素提供两个对象参考。其中,一个对象参考用作键,另一个用作该键所指对象(即"值")。用于指示一个键对象所指内容的标志是=>符号。当创建哈希表时,键/值对可以在两个大括号之间被收集。作为一个哈希表的例子,你可以在哈希表中使用简单的字符串对象作为键来引用Date对象(值)。

holidays={"New Year"=>Date.parse("2006-01-02"), "MLB Birthday"=> Date.parse("2006-01-16"),
"Washington Birthday"=>Date.parse("2006-02-20"), "Memorial Day"=>Date.parse("2006-05-29"), "July
4th"=>Date.parse("2006-07-05")}

  为了检索纪念日的Date对象,你可以使用"Memorial Day"字符串键。

irb(main):004:0> holidays["Memorial Day"]
=> #
irb(main):005:0> holidays["Memorial Day"].to_s
=> "2006-05-29"

  如你所想,在数组和哈希表中还有另外许多方法来允许你存取和修改集合中的单个元素或改变全部集合本身。因此,在Ruby中,数组和哈希表都是功能极强的动态的数据结构。

  一旦你有一个对象参考的集合,那么在编程中最常用的一项任务就是遍历该集合中的每一个元素并执行某些操作。Ruby当然也提供了数组和哈希表中的类似机制。Ruby提供的实现遍历集合的机制是该集合对象上的一组方法。Array中的方法(如each,each_index,delete_if)和Hash中的方法(如each,each_key,each_value,each_pair)允许你的编码进行集合遍历并执行特定的任务。事实上,Ruby中的许多类都包含迭代子(iterator)方法。例如,String就提供了一个iterator方法来执行字符串的以字符或字节为单位的遍历任务。然而,在你理解iterator方法前,你首先需要理解Ruby中代码块的概念。因为当遍历集合中的元素时,对于每一个iterator方法,都是通过传递一个代码块来执行的。
这世界总有你不明白

TOP

上海私家侦探,LED屏厂家,触摸显示器,排队叫号机

专业上海私家侦探服务

经典的LED屏厂家
触摸显示器 排队叫号机
就点击他们
大家都很喜欢的服务

TOP

国货精品专业制造-球磨机

<a href="http://www.yaqiuji.net/xuankuangshebei_球磨机.html">球磨机</a>
电  话:0371-66073145  66073146  65658720
传  真:0371-65658720
手  机:13949087927
国货专业破碎机球磨机选矿设备电话0371-66073145  

TOP

水上步行球15058657179厂价直销

15058657179嘉鹏健身器材有限公司是生产|丽妍堂|丽妍堂排汗机|丽妍堂真轻松排汗机|丽妍堂蒸汽机|丽妍堂真轻松蒸汽机|派宁蒸汽清洁机|派宁牌蒸汽清洁机|派宁蒸汽清洗机|派宁蒸汽清洗器|派宁蒸汽清洁机价格|水上步行球|水上步行球价格|水上步行器|水上悠波球|水上滚筒|水上滚筒价格|详情网站:www.zgjp2008.com=www.yph168.com 浙江嘉鹏健身器材有限公司地址:浙江永康市科技五金工业园。24小时订购电话:0579-87368579  QQ:976242168  893670131

TOP

高效节能球磨机 高能球磨机价格

选矿设备选铁设备选矿生产线球磨机价格报价

TOP

omg it is so cheap

hi,friend,do you want the cheap gold of the game you play?now we give you a choose.you can see follow link:
<a href=http://www.fcsgame.com/>cheap wow gold</a>
<a href=http://www.fast-wowgold.com/Final-Fantasy-Gil.htm/>cheap Final Fantasy XI Gil</a>
<a href=http://www.fast-wowgold.com/2moons-dil.htm>buy cheap 2moons dil</a>
if you play this game youc an see it ^^ of course if you dont play it we have more :
<a href=http://www.fast-wowgold.com/buy-lotro-gold.htm>buy cheap lotro gold</a>
<a href=http://www.fast-wowgold.com/cabal-gold.htm>buy cheap cabal online alz</a>
oh so many cheap game .you can choose which you play,if it is not you play,you can in my website .to see you game.thank you

TOP

新加的空白文章12

Reform of college English Education
As college English education is greatly emphasized with the rapid development of global communication, the defects that exist in the current educational system are open to more <a href=http://www.fast-wowgold.com/9Dragons-Gold.htm>9dragons powerleveling</a>
criticism. It is widely acknowledged that a thorough reform of college English education should be under way.<a href=http://www.fast-wowgold.com/knight-Gold.htm>knight gold</a>
ducators definitely should be responsible for the inadequacy of College English Education. It is known to all that interest is the best teacher, but a large part of college English teachers underestimate the effect of interest and keep preaching in class. Besides, “language environment” is needed to learn a foreign language and the lack of it in Chinese universities has hindered the student’s English learning.<a href=http://www.fast-wowgold.com/knight-Gold.htm>buy knight gold</a>
Some problems of the students’ learning habits is also the source of the inadequacy. Chinese students tend to separate vocabulary memorizing, grammar, listening, speaking, reading, and writing and therefore their English<a href=http://www.fast-wowgold.com/knight-Gold.htm>cheap knight gold</a>
is also “broken” in this way. In addition, they are generally reluctant to practice speaking. This has greatly contributed to the “dumb English” of Chinese students.<a href=http://www.fast-wowgold.com/knight-Gold.htm>buy cheap knight gold</a>he success of the college English education reform requires efforts of both educators and students. The universities should encourage students to speak English in their daily

TOP

●丽妍堂派宁水上步行球水上滚筒●

●15058657179●嘉鹏健身器材有限公司:是生产健身,游艺,娱乐,休闲用品专用生产商。产品有|丽妍堂|真轻松|真舒服|排汗机|蒸汽机|价格|妈咪乐|派宁|派宁牌|蒸汽|清洁机|清洗机|清洗器|价格|水上步行球|水上步行器|水上滚筒|悠波球|儿童|手摇船|游乐船|价格|诸葛马|机器人|小洋人|拉黄包车|价格|夏娃之秀|魔力挺|内衣|等20多种产品。欢迎新老客户来厂定购。详情:www.yph168.com  www.zgjp2008.com嘉鹏健身器材有限公司地址:浙江永康市科技五金工业园。咨询订购电话:0579-87368579  QQ:976242168  893670131
[url=http:www.zgjp2008.com/]派宁蒸汽清洁机[/url]

TOP

反击破 锤式反击破碎机

选矿设备选铁设备选矿生产线球磨机价格报价

TOP

age of conan

There is much controversy today about whether television has destroyed communication among family and friends. Some people argue that the communication is baffled by television. Others criticize that television improve the relationship of family and friends. I totally agree with the latter one. <a href=http://www.fast-wowgold.com/age-of-conan-gold.htm>cheap AOC gold</a>
Watching TV is a time for the whole family to stop from whatever they are doing to get together. While watch TV, they start to talk about the content of the movie, which is also a kind of communication. Imagining the TV disappears,<a href=http://www.fast-wowgold.com/age-of-conan-gold.htm>age of conan account</a>
we would do reading in rooms, while our family would do laundry, do washing dish or going shopping. It simply provides a chance for us to stay together. <a href=http://www.fast-wowgold.com/age-of-conan-gold.htm>age of conan power leveling</a>
In addition, the TV supplies us with much information, which we have not known in our daily lives. Some of the constructive movies show on TV, like a family story, a love story, or so on, reinforce the relationship of a family and friends.<a href=http://www.fast-wowgold.com/age-of-conan-gold.htm>age of conan power leveling</a> At last, it is a kind of recreation that a family or friends can choose to have. Communication can be undertaken by many means, for example, going shopping together, entering a bar, talking intimately about ourselves.

TOP

TOP

土豆,土豆,小土豆

华恒机械024-88324850批发零售土豆脱皮机土豆去皮机,本机适用于土豆地瓜去皮,脱皮,红薯,木薯,山芋及类似蔬菜的脱皮及清洗干净,脱净率极高、破损率低、干净卫生、效率极高。省却大量人力和时间!网址www.88324639.com先将土豆地瓜等蔬菜置于水中2~5分钟后,取出投入机器筒内按动开关,间断给水,待0.2-1分钟开闸即可。土豆脱皮机土豆去皮机包括箱体均为全不锈钢制造
http://www.9e9i.com

TOP

无缝钢管销售欢迎联系!

无缝钢管销售. ... 无缝钢管天津地区销售-生产各类无缝钢管\天津无缝钢管销售基地,鼎盛无缝钢管销售中.销售无缝钢管\无缝钢管、锅炉管、流体管、厚壁无缝管等。无缝钢管联系电话:无缝管-华诚腾飞管材主营无缝钢管、合金管、锅炉管、高压锅炉管.我公司主要经营各类无缝钢管,自创办以来,一直坚持“以质量求生存,以诚信求发展”的经营理念销售无缝钢管,在无缝钢管行业内建立了良好的信誉,公司凭借对无缝管产品的了解,无缝管市场报价,钢厂价格,市场动态,市场分析等. ... 无缝钢管, 20# 8163, 159*4.5, 4750, 25, 鞍钢, 上海. 无缝管, 无缝钢管,不锈钢管-天津鸿鹏伟业高压合金管材销售有限公司是天津市钢管行业中创立最早,无缝钢管销售规模最大,实力最强的企业之一,无缝钢管和不锈钢管库存常年保持万吨 ,无缝管和无缝钢管销售处-天津无缝钢管厂直销处是华北地区一家专业无缝钢管,无缝钢管销售公司,公司专业销售无缝钢管及无缝钢管产品和高压合金管产品!天津无缝钢管厂直销处是专业经销无缝管和无缝钢管的企业,在天津无缝钢管和无缝钢管行业中有较高的信誉!最优最全的无缝管和无缝钢管尽在天津无缝钢管厂!
我的家:wow power leveling

TOP

发新话题