Ruby中的数组过滤
数组可以算是计算机编程中用到的最多的一个数据结构了,ruby中也是如此,这次我们来看看如何对数组中的项进行过滤,选出自己需要的项。
首先,假如有一个包含人名的数组:
-
>> name = %w{ zhangsan lisi wangwu zhaoliu galeki lyanry liulanger xiaosl tom nick bara}
-
=> ["zhangsan","lisi","wangwu","zhaoliu","lyanry","liulanger","xiaosl","tom","nick","bara" ]
假如我们现在要把4个字母的名字选出来,要怎么做呢?
像其他语言一样,写个for循环,遍历每一项,如果长度为4就选出来?
ruby有更好的办法,只需要一行:
-
>> name.find_all{|n| n.length == 4}
-
=> ["lisi", "nick", "bara"]
find_all 方法配合后面的block,简洁明了的筛选出了符合条件的数组项。如果我们要从数组中剔除某些项,要用到 reject 方法:
-
>> name.reject{|n| n.length == 4}
-
=> ["zhangsan", "wangwu", "zhaoliu", "galeki", "lyanry", "liulanger", "xiaosl", "tom"]
block 中也可以复合多个条件:
-
>> name.find_all{|n| n.length>3 && n.length <8 && n[0,1] == "z"}
-
=> ["zhaoliu"]
以上语句从数组中筛选出长度大于3,小于8,并且首字母为z的名字,依旧简洁明了,忽略效率的因素,我们甚至可以把ruby中的数组当作数据库来查询了。
2021年6月09日 05:58
Thank you so much for such a well-written article. It’s full of insightful information. Your point of view is the best among many without fail.For certain, It is one of the best blogs in my opinion. click here
2021年7月14日 02:57
Olansi Healthcare Co., Ltd is a professional manufacturer of air purifier, hydrogen water, water purifier, etc. healthcare, more than 12 years experience since 2009 in Guangzhou, China. 60,000 m2 of our own injection molding factory, our own filter factory, our own mold factory, our own assembly factory! 600 square meters of professional laboratory, 30 engineers R&D team. We are prfessional in ODM, OEM services! 3,000 PCs per day of production capacity! 100% aging test for mass production! CE, CB, ROHS, SASO, CQC, CCC approval and ISO 9001: 2008 certificate! Olansi luftreiniger
2021年7月14日 04:00
Olansi Healthcare Co., Ltd is a professional manufacturer of air purifier, hydrogen water, water purifier, etc. healthcare, more than 12 years experience since 2009 in Guangzhou, China. 60,000 m2 of our own injection molding factory, our own filter factory, our own mold factory, our own assembly factory! 600 square meters of professional laboratory, 30 engineers R&D team. We are prfessional in ODM, OEM services! 3,000 PCs per day of production capacity! 100% aging test for mass production! CE, CB, ROHS, SASO, CQC, CCC approval and ISO 9001: 2008 certificate! Olansi luftreiniger
2021年7月14日 18:19
This is extremely fascinating substance! I have completely delighted in perusing your focuses and have reached the conclusion that you are right about a hefty portion of them. You are extraordinary. buy weed
2021年7月14日 18:55
Thanks so much for sharing this awesome info! I am looking forward to see more postsby you! delta 8 THC
2021年7月17日 18:13
Công ty OLANSI là một nhà sản xuất OEM máy lọc không khí chuyên nghiệp, các sản phẩm có chứa máy lọc không khí Trung Quốc, máy lọc không khí gia đình, máy lọc không khí HEPA, máy lọc không khí ion âm, máy lọc không khí ion hóa, máy lọc không khí PM2.5, máy lọc không khí xe hơi, máy lọc không khí trong phòng, máy lọc không khí thông minh và như thế. Máy lọc không khí Olansi
2021年7月19日 19:10
Good place, walked into the future together with bookmarked as their favorite your websites. That i can’t procrastinate to read the paper even more with everyone. n95
2021年8月01日 03:30
Winner of Best of Yolo County 2020 and Best of Weedmaps Kind Farma is Davis California's Premier medical and recreational Cannabis dispensary. California weed dispensary menu
2021年8月01日 19:24
Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I'll be subscribing to your feed and I hope you post again soon. Big thanks for the useful info. Bong88
2021年9月02日 18:46
Wow! Such an amazing and helpful post this is. I really really love it. It's so good and so awesome. I am just amazed. I hope that you continue to do your work like this in the future also Autolikes
2021年9月06日 19:50
I know your aptitude on this. I should say we ought to have an online discourse on this. Composing just remarks will close the talk straight away! What's more, will confine the advantages from this data. water heater installation near me
2021年9月09日 19:16
You have outdone yourself this time. It is probably the best, most short step by step guide that I have ever seen. https://www.allfoodmenuprices.org/
2021年9月09日 21:12
Excellent article. Very interesting to read. I really love to read such a nice article. Thanks! keep rocking. restaurants near me
2021年9月16日 20:27
Positive site, where did u come up with the information on this posting?I have read a few of the articles on your website now, and I really like your style. Thanks a million and please keep up the effective work. store holiday hours and opening hours
2021年9月18日 19:25
Pretty good post. I have just stumbled upon your blog and enjoyed reading your blog posts very much. I am looking for new posts to get more precious info. Big thanks for the useful info. minecraft crossplay
2021年9月18日 21:20
I’ve been searching for some decent stuff on the subject and haven't had any luck up until this point, You just got a new biggest fan!.. aka ms remote connect
2021年9月25日 19:27
I like your post. It is good to see you verbalize from the heart and clarity on this important subject can be easily observed... EMF Protection
2021年9月25日 21:18
Only aspire to mention ones content can be as incredible. This clarity with your post is superb and that i may think you’re a guru for this issue. High-quality along with your concur permit me to to seize your current give to keep modified by using approaching blog post. Thanks a lot hundreds of along with you should go on the pleasurable get the job done. EMF Protectors
2022年1月26日 15:45
When your website or blog goes live for the first time, it is exciting. That is until you realize no one but you and your. 토토커뮤니티
2022年5月28日 04:33
This is a great inspiring article.I am pretty much pleased with your good work.You put really very helpful information. Keep it up. Keep blogging. Looking to reading your next post. tangkasnet
2022年5月29日 22:02
i am for the first time here. I found this board and I in finding It truly helpful & it helped me out a lot. I hope to present something back and help others such as you helped me. https://www.parisclub888.net
2022年5月29日 23:15
Thank you for another great article. Where else could anyone get that kind of information in such a perfect way of writing? I have a presentation next week, and I am on the look for such information. royal green
2022年5月31日 18:12
You have a real ability for writing unique content. I like how you think and the way you represent your views in this article. I agree with your way of thinking. Thank you for sharing. https://www.guzaclub88.com
2022年6月03日 06:50
I think this is one of the most significant information for me. And i’m glad reading your article. But should remark on some general things, The web site style is perfect, the articles is really great : D. Good job, cheers dagotogel
2022年6月18日 20:16
That appears to be excellent however i am still not too sure that I like it. At any rate will look far more into it and decide personally! 먹튀검증커뮤니티
2022年7月02日 16:41
Good website! I truly love how it is easy on my eyes it is. I am wondering how I might be notified whenever a new post has been made. I have subscribed to your RSS which may do the trick? Have a great day! lekkie koło zamachowe
2022年7月16日 21:58
Merely a smiling visitant here to share the love (:, btw outstanding style. chateau
2022年7月18日 17:55
Keep up the good work , I read few posts on this web site and I conceive that your blog is very interesting and has sets of fantastic information. https://accountingacademy.bg/index.php?q=infopage&id=460&term=Форекс
2022年7月20日 17:14
i am for the first time here. I found this board and I in finding It truly helpful & it helped me out a lot. I hope to present something back and help others such as you helped me. <a href="https://futuretripexperience.com/
">future trip experience</a>
2022年7月22日 02:48
Good website! I truly love how it is easy on my eyes it is. I am wondering how I might be notified whenever a new post has been made. I have subscribed to your RSS which may do the trick? Have a great day! https://artdaily.com/
2022年7月25日 18:48
Wow, cool post. I’d like to write like this too – taking time and real hard work to make a great article… but I put things off too much and never seem to get started. Thanks though. https://designacademy.bg/index.php?q=question&id=915&ask=Как_се_проектира_малък_апартамент
2022年7月27日 21:53
I really like your writing style, great information, thankyou for posting. credit cards
2022年8月25日 18:13
Wow, cool post. I’d like to write like this too – taking time and real hard work to make a great article… but I put things off too much and never seem to get started. Thanks though. mental health tattoos
2022年9月29日 20:41
I think this is one of the most significant information for me. And i’m glad reading your article. But should remark on some general things, The web site style is perfect, the articles is really great : D. Good job, cheers 토토커뮤니티
2022年10月16日 19:50
Wow, this is really interesting reading. I am glad I found this and got to read it. Great job on this content. I like it. flexon tablet side effects
2022年10月21日 19:03
<p>
Your work is truly appreciated round the clock and the globe. It is incredibly a comprehensive and helpful blog. <a href="https://peakerr.com/">smm panel india</a></p>
2022年10月22日 20:39
I’ve been surfing online more than three hours today, yet I never found any interesting article like yours. It’s pretty worth enough for me. In my opinion, if all webmasters and bloggers made good content as you did, the web will be a lot more useful than ever before. kuşadası escort
2022年11月16日 15:09
Good website! I truly love how it is easy on my eyes it is. I am wondering how I might be notified whenever a new post has been made. I have subscribed to your RSS which may do the trick? Have a great day! proper news time
2022年11月21日 18:26
Positive site, where did u come up with the information on this posting?I have read a few of the articles on your website now, and I really like your style. Thanks a million and please keep up the effective work. spbo terlengkap
2022年11月22日 02:51
Good write-up, I¡¦m normal visitor of one¡¦s blog, maintain up the nice operate, and It is going to be a regular visitor for a long time.
2022年11月24日 18:25
That is really nice to hear. thank you for the update and good luck. spbo live score indonesia
2022年11月29日 03:14
I think this is a really good article. You make this information interesting and engaging. You give readers a lot to think about and I appreciate that kind of writing. newport residences
2022年12月06日 17:36
Wonderful blog! I found it while surfing around on Yahoo News. Do you have any suggestions on how to get listed in Yahoo News? I’ve been trying for a while but I never seem to get there! Appreciate it. 꽁머니 지급
2022年12月14日 19:39
Wonderful blog! I found it while surfing around on Yahoo News. Do you have any suggestions on how to get listed in Yahoo News? I’ve been trying for a while but I never seem to get there! Appreciate it. Dubai Desert Safari
2022年12月29日 18:43
I like your post. It is good to see you verbalize from the heart and clarity on this important subject can be easily observed... livescore
2022年12月31日 04:55
Someone Sometimes with visits your blog regularly and recommended it in my experience to read as well. The way of writing is excellent and also the content is top-notch. Thanks for that insight you provide the readers! Safari in Dubai
2023年1月04日 19:46
Thank you a bunch for sharing this with all of us you actually realize what you are talking about! Bookmarked. Please also seek advice from my site =). We could have a hyperlink change contract between us! Dubai Safari
2023年1月14日 17:29
I ' ve never seen a blog that demand a survey for this actions, very curious... spbo live score
2023年2月02日 19:07
Hi there! Nice post! Please tell us when I will see a follow up! judi slot online
2023年2月04日 18:10
Extraordinary results! And what a changeover for me! I am really glad that I chose them for work! I would suggest them to everyone. They are amazing in their work! bola online
2023年2月14日 17:03
so much wonderful information on here, : D.
2023年2月14日 17:04
<a href="https://www.fiverr.com/share/d8ePwa">seo</a> [url=https://www.fiverr.com/share/d8ePwa]seo[/url]
2023年2月14日 17:04
so much wonderful information on here, : D. seo
2023年3月15日 18:04
Great results! Great compatibility! Super in everything! Using this website have been a splendid experience and have provided amazing results as well! Recommended for all dark web links spbo live score
2023年4月12日 18:18
Nice to read your article! I am looking forward to sharing your adventures and experiences. spbo
2023年5月19日 06:31
Cheap Replica Handbags You guide me by sharing this toolbox i face problem whenever i exploit this promotion reading your blog i am able to easily use.Costco Food Court's special Cafe