惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Blog — PlanetScale
Blog — PlanetScale
B
Blog
A
About on SuperTechFans
大猫的无限游戏
大猫的无限游戏
爱范儿
爱范儿
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
H
Help Net Security
H
Hackread – Cybersecurity News, Data Breaches, AI and More
博客园 - 三生石上(FineUI控件)
有赞技术团队
有赞技术团队
酷 壳 – CoolShell
酷 壳 – CoolShell
WordPress大学
WordPress大学
IT之家
IT之家
D
Docker
Google DeepMind News
Google DeepMind News
罗磊的独立博客
T
The Blog of Author Tim Ferriss
aimingoo的专栏
aimingoo的专栏
博客园 - 叶小钗
Recent Announcements
Recent Announcements
阮一峰的网络日志
阮一峰的网络日志
D
DataBreaches.Net
博客园 - 司徒正美
Engineering at Meta
Engineering at Meta

博客园 - 小熊bryan

坐视——做事——做势 InfoQ中文站正式升级为1.5版 编辑絮语 今年…… 书店的风景 要敏捷一点,再敏捷一点 对Ruby中Kernel的callcc方法的研究 理解ruby核心概念:Object, Class, Module, Kernel 再试试看 越来越觉得自己的博客像个包子 翻译作品:为敏捷团队设计协作空间 用元编程的方式向Ruby添加properties 【翻译】在ruby中实现attributes的lazily initialization(惰性初始化) 《Programming Ruby 中文版第二版》P577页singleton_method_undefined方法说明有点问题 是没有好音乐,还是我们没有好心境? Best of Ruby Quiz 笔记之三:GEDCOM Parser 使用REXML在ruby中处理xml 软件的业务价值来自哪里?——business value generally comes from using software 要问五次为什么--业务需求挖掘之道(摘自Wikipedia)
Best of Ruby Quiz 之四 Animal Quiz之我的答案
小熊bryan · 2007-07-24 · via 博客园 - 小熊bryan

终于完全靠我自己的思考,做出来一个quiz,就是第四个Animal Quiz,代码贴一下,笔记稍后放出。

 1class AnimalQuiz
 2    @@animals = Array.new                                        # 用来保存问题和答案的数组,每个数组元素又是一个数组,
 3                                                                                    # [["cat", "Is it cute?", "y"], ["dog", "does it bark?","y"] ]
 4        
 5    def AnimalQuiz.add_animal(animal)            
 6      @@animals.push(animal)
 7    end
 8
 9    
10    def self.do_quiz
11        match_animal = ""
12        @@animals.each do |animal|
13            puts animal[1]                                            # 提问题
14            s = gets
15            if  s.chomp! == animal[2]                        # 答案是否与数组中给定的答案匹配
16                puts "is it " + animal[0+ "?"     # 匹配的情况,询问是否是当前数组元素中的动物
17                if get_yes_or_no=="y"                            # 读取输入,是当前数组元素中的动物
18                    match_animal = animal[0]                 # 取出动物
19                    break                                                        
20                end
21            else                                                                 # 答案不匹配,向下一个数组元素中的问题进发
22                next
23            end
24        end
25        if match_animal.empty?                                # 循环完毕,没有找到答案
26            puts "you win, what animal do u think of?"         # 向数组中累积问题
27            user_animal = Array.new
28            user_animal << gets.chomp!
29            puts "give me a question"
30            user_animal << gets.chomp!
31            puts "what is your answer to this question?(y or n)"
32            user_animal << gets.chomp!
33            add_animal(user_animal)
34            puts "thanks"
35        else
36            puts "i win,"                                                # 找到动物
37        end        
38    end
39
40    def self.go
41        puts "Think of an animal"
42        puts "is it an elephant?"                            # 对大象特殊处理
43        if get_yes_or_no != "y"                                # 不是大象,开始处理
44            do_quiz
45        end
46        while wanna_play_again? =="y"                    # 是大象,询问是否想继续
47            puts "Think of an animal"              # 继续,进行后续quiz
48            do_quiz
49        end
50    end
51
52    private
53        
54    def self.wanna_play_again?
55        puts "wanna play again?"
56        get_yes_or_no
57    end
58    
59    def self.get_yes_or_no                                    # 获取用户输入
60        s =gets         
61        while ((s=~/^\s*y\s*$/).nil? && (s=~/^\s*n\s*$/).nil? )
62            puts "please input 'y' or 'n'"
63             s = gets
64         end
65         s.chomp!
66    end
67    
68end
69
70AnimalQuiz.go