锁定老帖子 主题:通过15个小例子学习ruby
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2007-10-15
以下都是从国外网站翻译过来,自己学习的时候总结了一下,希望对大家学习ruby有所帮助。
1. Problem: “Display series of numbers (1,2,3,4, 5….etc) in an infinite loop. The program should quit if someone hits a specific key (Say ESCAPE key).”
解决方案
虽然是一个很简单的问题,但我想着想着却觉得这个问题很有意思,原文中也没有给出很完美的答案,不知道谁有好的解决方法。 2. Problem: “Fibonacci series, swapping two variables, finding maximum/minimum among a list of numbers.” 解决方案
语法知识: 1.Hash。Hash在实例话的时候可以在new里边接受一个参数值,或者一个模块,它实际上不是hash对象的一个值,仅当在hash操作的时候找不到这个值对应的项的时候返回。 2.交换两个变量。 3.查询数组里最大最小的值,有专门的API。 3. Problem: “Accepting series of numbers, strings from keyboard and sorting them ascending, descending order.” 解决方案
语法:
4. Problem: “Reynolds number is calculated using formula (D*v*rho)/mu Where D = Diameter, V= velocity, rho = density mu = viscosity Write a program that will accept all values in appropriate units (Don’t worry about unit conversion) If number is < 2100, display Laminar flow, If it’s between 2100 and 4000 display 'Transient flow' and if more than '4000', display 'Turbulent Flow' (If, else, then...)" ruby 代码
语法: 没有搞清楚vars = %w{D V Rho Mu} 这一句是什么意思。 5. Problem: “Modify the above program such that it will ask for ‘Do you want to calculate again (y/n), if you say ‘y’, it’ll again ask the parameters. If ‘n’, it’ll exit. (Do while loop) While running the program give value mu = 0. See what happens. Does it give ‘DIVIDE BY ZERO’ error? Does it give ‘Segmentation fault..core dump?’. How to handle this situation. Is there something built in the language itself? (Exception Handling)” ruby 代码
6.一个计算器的问题,代码太多。 7. Problem: “Printing output in different formats (say rounding up to 5 decimal places, truncating after 4 decimal places, padding zeros to the right and left, right and left justification)(Input output operations)” ruby 代码
语法:
8. Problem: “Open a text file and convert it into HTML file. (File operations/Strings)” 这段代码比较长,其中有些东西还是不太理解,还要看看正则表达式,谁能告诉我下边的代码是怎么执行的吗:
9. Problem: “Time and Date : Get system time and convert it in different formats ‘DD-MON-YYYY’, ‘mm-dd-yyyy’, ‘dd/mm/yy’ etc.” ruby 代码
10. Problem: “Create files with date and time stamp appended to the name” ruby 代码
11. Problem: “Input is HTML table. Remove all tags and put data in a comma/tab separated file.” 12. Problem: “Extract uppercase words from a file, extract unique words.”
语法:(对于第四行还是不太懂,谁能给我讲一下呢) 1.打开文件,读取文件,split(),正则表达式匹配。 13. Problem: “Implement word wrapping feature (Observe how word wrap works in windows ‘notepad’).” ruby 代码
14. Problem: “Adding/removing items in the beginning, middle and end of the array.” ruby 代码
15. Problem: “Are these features supported by your language: Operator overloading, virtual functions, references, pointers etc.” Solution: Well this is not a real problem (not in Ruby, at least). Ruby is a very high level language ant these things are a must . 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2007-10-17
%W 是字符串数组,主要目的是少打几个引号。那个例子是生成动态变量。
|
|
返回顶楼 | |
发表时间:2007-10-17
ruby真的很热门了.
这真是有些奇迹了. |
|
返回顶楼 | |
发表时间:2007-10-18
不知道Ruby这种语法是好是坏,对初学者要求就是记住越多的语法越好吧
|
|
返回顶楼 | |
发表时间:2007-10-19
ruby很好用,越用越觉得很人性化,可能上手容易,但是熟练比肩难
|
|
返回顶楼 | |
浏览 8477 次