While loop in Ruby Shoes GUI ToolKit - shoes

I have only been using Shoes for a few days now so maybe I am missing something. I wrote my son an little program to help him learn his multiplication tables. When he gets ten correct he's done. I can not seem to get the while loop right using SHOES.
Would some please show me a while statement. When I try; it either wipes out my flow and stack statements or Shoe crashes.
thanks in advance.
Sam

I don't know how you're using a while loop. Most likely you're trying to recreate the stack each iteration through the while loop, which is a bad idea. The two solutions that spring immediately to mind are to handle the logic on a button click and keep track of the number correct in a row, like this:
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
Or, the solution which I think is much more interesting, using url and visit:
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

Related

Cannot find where end input is missing

I'm very sorry for the trivial question, i'm very new in Ruby.
It shows me
13-3.rb:32: syntax error, unexpected end-of-input, expecting keyword_then or ';' or '\n'"
But i just can't see where i should put "end" here
if File.exist?(questions) && if File.exist?(answers)
f = File.new(questions, "r:UTF-8")
question_line = f.readlines
f.close
g = File.new(answers, "r:UTF-8")
answer_line = g.readlines
g.close
right_answers = 0
position = 0
questions_amount = question_line.length
while position <= questions_amount
puts question_line[position]
user_input = gets.encode("UTF-8").chomp
if user_input == answer_line[position]
puts "Верный ответ"
right_answers += 1
else
puts "Неправильно. Верный ответ: " + answer_line[position]
end
position += 1
end
puts "У вас #{right_answers} правильных ответов из #{questions_amount}"
else
puts "Файл не найден"
end
Seems like you forgot do in while statement.
while position <= questions_amount do
# ...
end
Also, as mentioned in comments, the first line should be
if File.exist?(questions) && File.exist?(answers)
Hope this helps.

Capybara / Ruby - Trying to return to the beginning of the loop when an error shows up

I'm trying to return to the loop beginnig when an error shows up as the code below.
I'm using the command "next" when a casual error occurs but it is not coming back for the loop beginning.
describe 'Test', :test do
before(:each) do
visit '/admin'
end
it 'Adding new images' do
image = 'barcelona.jpg'
#imagem = Dir.pwd + '/spec/fixtures/' + image
produto = '1'
100.times do
visit '/admin/' + produto
if page.has_no_css?('#mensagem > h1')
within_frame(:xpath, "//*[#id='app-content']/main/div/iframe") do
find('#ctl00_Conteudo_tbxNome_txtId').set 'test_name'
find('#ctl00_Conteudo_BtnSalvar').click
if page.has_no_css?('#mensagem > h1')
find('#ctl00_Conteudo_tbxIdArquivoControle_lnkInserirArquivo').click
attach_file('ctl00_Conteudo_tbxIdArquivoControle_tbxArquivo', #imagem)
find('#ctl00_Conteudo_tbxIdArquivoControle_btnEnviar').click
if page.has_no_css?('#mensagem > h1')
find('#skuTabNavigation a[href="#tabImages"]').click
expect(page).to have_content image
puts 'Test ok'
else
puts 'Error was presented, starting over..'
next
end
else
puts 'Error was presented, starting over..'
next
end
end
else
puts 'Error was presented, starting over..'
next
end
end
end
end
I would like that every time when the system goes to "else" condition, it restart the loop.
I don't think there is a direct way to move back to the initial iteration of a loop. redo exists but it only moves you back to the current iteration.
In this case, you probably want to change the way you're looping so you can more easily control when to start/stop. For example:
i = 0
while i <= 100 do
if page.has_no_css?('#mensagem > h1')
i = 0
puts 'Error'
next
end
i += 1
end
So you don't have to reset the loop index and call puts each time you could rescue an error:
class MyError < StandardError; end
i = 0
while i <= 100 do
begin
if page.has_no_css?('#mensagem > h1')
raise MyError, 'thing was missing'
end
puts i
i += 1
rescue MyError => boom
puts "Error: #{boom.message}"
i = 0
redo
end
end

Reset a counter in Ruby

I have the following code to compile jobs from github jobs API. How do I reset a counter back to 0 every time I call on a new city? I've tried putting it in several different places with no luck.
def ft_count_and_percentage
##url += #city
uri = URI(##url)
response = Net::HTTP.get(uri)
result = JSON.parse(response)
result.each do |job|
if job["type"] == "Full Time"
##fulltime_count += 1
end
end
puts "Total number of jobs in #{#city}: #{result.length}"
if ##fulltime_count > 0
puts ("full time percent ") + "#{(##fulltime_count/result.length) * 100}"
else
puts "No FT Positions"
end
end
##fulltime_count is defined outside this method to start at 0. Currently, as expected the counter just keeps adding jobs every time I add a new city.
boston = Job.new("Boston")
boston.ft_count_and_percentage
sf = Job.new("San Francisco")
sf.ft_count_and_percentage
la = Job.new("Los Angeles")
la.ft_count_and_percentage
denver = Job.new("Denver")
denver.ft_count_and_percentage
boulder = Job.new("Boulder")
boulder.ft_count_and_percentage
chicago = Job.new("Chicago")
chicago.ft_count_and_percentage
ny = Job.new("New York City")
ny.ft_count_and_percentage
You may need to reset it inside Job init
class Job
def initialize
##count = 0
end
def ft_count_and_percentage
#the blah you already have
end
end

Ruby - Create a class with a file as a variable - possible?

I need to create a program in which the user can take different tests. As i dont want to copy paste my code all over for every test, i have tried to setup a class for that purpose - but i have problems with this class.
Error message = undefined variables or method in 'display_test'
I have predefined som test as a .txt file
I want to choose the file in the class depending on what the user answer - is that possible?
Class code:
class Test
#correct_answers = 0
def display_question( question, options, answer )
puts question
options.each_with_index { |option, idx| puts "#{ idx + 1 }: #{ option
}" }
print 'Answer: '
reply = gets.to_i
if answer == reply
puts 'Correct!'
#correct_answers += 1
puts "#{#correct_answers}"
else
puts 'Wrong. The correct answer was: ' + answer.to_s
end
end
def display_test()
f = File.new(userinput, 'r')
while ! (f.eof?) #logikken til at splitte
line = f.gets()
question = line.split("|")
question[1] = question[1].split(";")
display_question question[0], question[1], question[2].to_i
end
end
display_test
puts "________________________________________________________"
puts "Total score:"
puts "You've got" + " #{#correct_answers}" + " correct answers!"
Before hand i have used ("geografitest.txt") instead of username in the File.new so it looked like this:
f = File.new('geografitest.txt','r')
But now i am trying to let the user decide what test to take.
I am very new to ruby, so please bear with me.
I have tried to do it this way, which obviously is not working.
puts "Which test do you want to take?"
select = 0
while (select != 3)
puts "Press 1 to take Geografi test."
puts "Press 2 to take Math test."
puts "Press 3 to take Religion test."
puts "Press 3 to exit"
select = gets.chomp.to_i
if (select == 1)
gets.chomp = userinput
userinput =`geografitest.txt`
echo $userinput
end
if (select == 2)
gets.chomp = userinput
userinput =`matematiktest.txt`
echo $userinput
end
if (select == 3)
gets.chomp = userinput
userinput =`religionstest.txt`
echo $userinput
end
if (select > 4)
puts "Not a correct selection"
elsif (select == 4)
puts "Goodbye"
end
end
abort
So my questions is now;
How can i make the user choose what test to take? Can i make a variable instead of the textfile as i have tried, but in a different way? Or is there a smarter way?
And in what way is my class wrong and how do i fix it? I know its not the way to make it, but i simple cant get my head around how to make it right.
Please help a rookie out.
Cheers!
You can pass file as dependency to you Test class based on user input with object constructor. Something like this
class Test
attr_reader :correct_answers_count
def initialize(file)
#file = file
#correct_answers_count = 0
end
#other code goes here
end
loop do
case user_input = gets.chomp
when '1'
file_name = 'some_file1'
when '2'
file_name = 'some_file1'
when '3'
break
else
puts 'wrong variant'
end
test = Test.new(File.new(file_name, 'r'))
test.display
end

error when creating hash in simple Credit Card class for ruby

I am creating a simple CC class that can create and update a credit card. To do this, I have created cc_bal{} as an instance object so it can update respect credit cards. The hash is to save and update a person and the amount on their cc. I end up getting an output of just the original amount that was created and not the updated amount
Heres the code:
class CC
def initialize(cc_name, cc_bal = {}, open_cc = false)
#cc_name = cc_name
#cc_bal = cc_bal
#open_cc = open_cc
end
def create(initAmount, person)
if initAmount > 0
#open_cc = true
#cc_bal[:person]=initAmount
puts "congrats #{person} on opening your new #{#cc_name} CC! with $#{#cc_bal[:person]}"
else
puts "sorry not enough funds"
end
end
def update(amount, person)
if #open_cc == true
#cc_bal[:person] + amount
else
puts "sorry no account created, #{person}"
end
puts "#{person} has CC balance of #{#cc_bal[:person]}"
end
end
#chase = Bank.new("JP Morgan Chase")
#wells_fargo = Bank.new("Wells Fargo")
me = Person.new("Shehzan", 500)
friend1 = Person.new("John", 1000)
#chase.open_account(me)
#chase.open_account(friend1)
#wells_fargo.open_account(me)
#wells_fargo.open_account(friend1)
#chase.deposit(me, 200)
#chase.deposit(friend1, 300)
#chase.withdraw(me, 50)
#chase.transfer(me, wells_fargo, 100)
#chase.deposit(me, 5000)
#chase.withdraw(me, 5000)
#puts chase.total_cash_in_bank
#puts wells_fargo.total_cash_in_bank
credit_card = CC.new("Visa")
credit_card.create(10,me)
credit_card.update(50,me)
credit_card.create(20,friend1)
credit_card.update(40,friend1)
Please disregard the function calls that are commented out.
Any idea why the CC's are not updatiing?
if #open_cc == true
#cc_bal[:person] + amount
else
You increase the amount, but you don't set the new value anywhere. It should be
if #open_cc == true
#cc_bal[:person] += amount
else
Note. The code needs some serious refactoring and cleanup.

Resources