在Ruby Shoes GUI ToolKit中循环

时间:2009-04-09 05:46:17

标签: shoes

我现在只使用鞋子几天,所以也许我错过了一些东西。我给儿子写了一个小程序来帮助他学习乘法表。当他得到十个正确时,他已经完成了。我似乎无法使用SHOES获得while循环。 有些人请告诉我一些声明。当我尝试;它要么擦掉我的流程,要么堆叠语句或鞋子崩溃。

提前谢谢。 山姆

1 个答案:

答案 0 :(得分:5)

我不知道你是如何使用while循环的。很可能你试图通过while循环每次迭代重新创建堆栈,这是一个坏主意。立即想到的两个解决方案是按下按钮处理逻辑并连续跟踪正确的数字,如下所示:

Shoes.app do
  num_correct = 0
  first_num = 1 + rand(10)
  second_num = 1 + rand(10)
  answer = first_num * second_num

  stack do
    @info = para 'Hi, Timmy!  This program will test your ',
           'multiplication tables.  When you get 10 ',
           'correct, you get to stop, and you get your ',
           'pet hamster back!'
    @question = para "What is #{first_num} x #{second_num}?"
    @response = edit_line :width => 100
    btn = button 'OK' do
      if @response.text == ''
        alert('You need to put an answer in the box, Timmy.')
      elsif @response.text.to_i == answer
        num_correct += 1
        if num_correct == 10
          @info.text = "Good job!  That's #{num_correct} in a row!"
          alert('You did it, Timmy!  You can have your ' \
                  'hamster back... for now.')
          exit
        else
          @info.text = "Good job!  That's #{num_correct} in a row!"
          first_num = 1 + rand(10)
          second_num = 1 + rand(10)
          answer = first_num * second_num
          @question.text = "What is #{first_num} x #{second_num}?"
          @response.text = ''
        end
      else
        num_correct = 0
        @info.text = "Wrong, Timmy.  The answer is #{answer}."
        first_num = 1 + rand(10)
        second_num = 1 + rand(10)
        answer = first_num * second_num
        @question.text = "What is #{first_num} x #{second_num}?"
        @response.text = ''
      end
    end
  end
end

或者,我认为使用urlvisit更有趣的解决方案:

class MyTest < Shoes

  url '/', :index
  url '/correct/(\d+)', :correct
  url '/wrong/(\d+)', :wrong
  url '/question', :question
  url '/question/(\d+)', :question
  url '/done', :done

  def index
    stack do
      para 'Hi, Timmy!  This program will test your ' \
             'multiplication tables.  When you get 10 ' \
             'correct, you get to stop, and you get your ' \
             'pet hamster back!'
      button 'OK' do 
        visit '/question'
      end
    end
  end

  def question(num_correct = 0)
    num_correct = num_correct.to_i
    first_num = 1 + rand(10)
    second_num = 1 + rand(10)
    answer = first_num * second_num
    stack do
      para "What is #{first_num} x #{second_num}?"
      flow do
        response = edit_line :width => 100
        button 'Answer' do
          if response.text.to_i == answer
            num_correct += 1
            if num_correct == 10
              visit '/done'
            else
              visit "/correct/#{num_correct}"
            end
          else
            visit "/wrong/#{answer}"
          end
        end
      end
    end
  end

  def correct(num_correct)
    stack do
      para "Good job!  That's #{num_correct} in a row!"
      button "Next Question" do
        visit "/question/#{num_correct}"
      end
    end
  end

  def wrong(answer)
    @num_correct = 0
    stack do
      para "Wrong!  The correct answer is #{answer}."
      button "Next Question" do
        visit '/question'
      end
    end
  end

  def done
    stack do
      para 'You did it, Timmy!  You can have your ' \
      'hamster back... for now.'
      button 'OK' do
        exit
      end
    end
  end
end

Shoes.app