Home > Enterprise >  How can I extract all repeated code to a function for the if else statements and begin end
How can I extract all repeated code to a function for the if else statements and begin end

Time:10-02

this is the code that I wanted to make use of the repeated code by placing it at another function and storing the variables to that other function. I am not really new at this function type so I really need some help, thank you so much for helping me

 def main
    score = 0

    puts "1. What does the == operator do?"
    puts "(a) calculates an arithmetic solution."
    puts "(b) assigns a value to a variable."
    puts "(c) checks for equality\n(d) draws the '=' character"
    puts "Your answer: "

    begin
        answer = gets.chomp
    end while (answer != 'a' && answer != 'b' && answer != 'c' && answer != 'd')
    
    if (answer == 'c')
        score  = 1
        puts "Correct \tYour score is now "   score.to_s
    else
        puts "Wrong \tYour score is still "   score.to_s
    end
    
    puts "2. Which is NOT a C keyword?"
    puts "(a) when"
    puts "(b) const"
    puts "(c) unsigned"
    puts "(d) do"
    puts "Your answer: "
    
    begin
        answer = gets.chomp
    end while (answer != 'a' && answer != 'b' && answer != 'c' && answer != 'd')
    
    if (answer == 'a')
        score  = 1
        puts "Correct \tYour score is now "   score.to_s
    else
        puts "Wrong \tYour score is still "   score.to_s
    end

    
    puts "3. In function call, the actual parameters are separated by"
    puts "(a) semicolons"
    puts "(b) colons"
    puts "(c) commas"
    puts "(d) space"
    puts "Your answer: "
    note
    begin
        answer = gets.chomp
    end while (answer != 'a' && answer != 'b' && answer != 'c' && answer != 'd')
    
    if (answer == 'c')
        score  = 1
        puts "Correct \tYour score is now "   score.to_s
    else
        puts "Wrong \tYour score is still "   score.to_s
    end
    
end  

main

CodePudding user response:

this isn't an easy step but, as per @BroiSatse's comments, creating the different aspects of the object as classes:

Defining an object to hold possible answers

class Answer
  attr_accessor :text, :correct
  def initialize text, correct
    @text = text
    @correct = correct
  end
end

Defining an object to hold questions, which will contain an array of possible answers

class Question
  attr_accessor :text, :answers
  def initialize text
    @text = text
    @answers = []
  end
end

Defining the "Quiz" class, which contains the score, and an array of questions. it also includes a routine to loop through the questions and answers stored in the quiz

class Quiz
  attr_accessor :questions, :score
  def initialize
    @questions = []
    @score = 0
  end

  def ask_questions
    @questions.each_with_index do |question, i|
      puts "#{i 1}. #{question.text}"

      question.answers.each_with_index do |answer,i|
        a_letter = (i   'a'.ord).chr
        puts "(#{a_letter}) #{answer.text}"
      end

      while true
        puts "Your answer: "
        user_answer = gets.chomp
        break if (user_answer&.ord - 'a'.ord)&.between?(0, question.answers.count-1)
      end

      if (question.answers[(user_answer.ord - 'a'.ord)]&.correct)
        @score  = 1
        puts "Correct \tYour score is now "   @score.to_s
      else
        puts "Wrong \tYour score is still "   @score.to_s
      end
    end
  end
end

Use the new classes/objects

Now that the classes are set up, we can create our "Quiz":

quiz=Quiz.new

question = Question.new("What does the == operator do?")
question.answers << Answer.new('calculates an arithmetic solution.', false)
question.answers << Answer.new('assigns a value to a variable.', false)
question.answers << Answer.new("checks for equality", true)
question.answers << Answer.new("draws the '=' character", false)
quiz.questions << question

question = Question.new("Which is NOT a C keyword?")
question.answers << Answer.new('when', true)
question.answers << Answer.new('const', false)
question.answers << Answer.new('unsigned', false)
question.answers << Answer.new('do', false)
quiz.questions << question

question = Question.new("In function call, the actual parameters are separated by")
question.answers << Answer.new('semicolons', false)
question.answers << Answer.new('colons', false)
question.answers << Answer.new('commas', true)
question.answers << Answer.new('space', false)
quiz.questions << question

and finally run the "ask_questions" routine

quiz.ask_questions

demo output

2.7.2 :124 > quiz.ask_questions
1. What does the == operator do?
(a) calculates an arithmetic solution.
(b) assigns a value to a variable.
(c) checks for equality
(d) draws the '=' character
Your answer:
c
Correct         Your score is now 1
2. Which is NOT a C keyword?
(a) when
(b) const
(c) unsigned
(d) do
Your answer:
c
Wrong   Your score is still 1
3. In function call, the actual parameters are separated by
(a) semicolons
(b) colons
(c) commas
(d) space
Your answer:
c
Correct         Your score is now 2

CodePudding user response:

this is my code is it alright or too sloppy

class Question
  attr_accessor :prompt, :answer
  def initialize(prompt, answer)
    @prompt = prompt
    @answer = answer
  end
end

p1 =
 "1. What does the == operator do?\n
 (a) calculates an arithmetic solution.\n
 (b) assigns a value to a variable.\n
 (c) checks for equality\n
 (d) draws the '=' character\n
 Your answer: \n"
p2 =
 "1. What does the == operator do?\n
 (a) calculates an arithmetic solution.\n
 (b) assigns a value to a variable.\n
 (c) checks for equality\n
 (d) draws the '=' character\n
 Your answer: "
p3 =
"3. In function call, the actual parameters are separated by\n
(a) semicolons\n
(b) colons\n
(c) commas\n
(d) space\n

Your answer: "

questions = [
  Question.new(p1, "c"),
  Question.new(p2, "a"),
  Question.new(p3, "c"),
]

def run_test(questions)
answer = ""
score = 0
  for question in questions
    puts question.prompt
    begin
        answer = gets.chomp
    end while (answer != 'a' && answer != 'b' && answer != 'c' && answer != 'd')
    if answer == question.answer
      score  = 1
      puts "Correct \tYour score is now "   score.to_s
    else
      puts "Wrong \tYour score is still "   score.to_s
    end
  end
end

run_test(questions)
  • Related