user6526412
user6526412

Reputation:

Ruby code Skips A certain part of Program

I am trying to run a calculator program but for some reason it is skipping some parts of the code. Here's my whole ruby codes:

# require yaml file

require 'yaml'
MESSAGES = YAML.load_file('calculator_messages.yml')

LANGUAGE = 'en'

def messages(message, lang='en')
  MESSAGES[lang][message]
end

def prompt(key)
  message = messages(key, LANGUAGE)  # make sure the "messages" method is declared above this line
  Kernel.puts("=> #{message}")
end

def integer?(input)
  input.to_i.to_s == input
end

def float?(input)
  input.to_f.to_s == input
end

def valid_number?(input)
  integer?(input) || float?(input)
end

def operation_to_message(op)
  word = case op
         when '1'
           'Adding'
         when '2'
           'Subtracting'
         when '3'
           'Multiplying'
         when '4'
           'Dividing'
         end
  word
end

prompt('welcome')

name = ''
loop do
  name = Kernel.gets().chomp()

  if name.empty?()
    prompt('valid_name')
  else
    break
  end
end

prompt("Hi #{name}!")

loop do # main loop
  number1 = ''
  loop do
    prompt('first_number')
    number1 = Kernel.gets().chomp()

    if valid_number?(number1)
      break
    else
      prompt('not_a_valid_number')
    end
  end

  number2 = ''
  loop do
    prompt('second_number')
    number2 = Kernel.gets().chomp()

    if valid_number?(number2)
      break
    else
      prompt('not_a_valid_number')
    end
  end

  operator_prompt = <<-MSG
  What operation would you like to perform?
  1) add
  2) subtract
  3) multiply
  4) divide
  MSG

  prompt(operator_prompt)

  operator = ''
  loop do
    operator = Kernel.gets().chomp()
    if %w(1 2 3 4).include?(operator)
      break
    else
      prompt('choose_number_range')
    end
  end

  prompt("#{operation_to_message(operator)} the two numbers")

  result = case operator
           when '1'
             number1.to_i() + number2.to_i()
           when '2'
             number1.to_i() - number2.to_i()
           when '3'
             number1.to_i() * number2.to_i()
           else
             number1.to_f() / number2.to_f()
           end

  prompt("The answer is: #{result}")
  prompt('next_step')
  answer = Kernel.gets().chomp()
  break unless answer.downcase().start_with?('y')
end

prompt('goodbye_msg')

Now here are the specific codes that it skips:
1. Displaying the name prompt("Hi #{name}!")
2. Displaying the adding, subtracting etc. message

prompt("#{operation_to_message(operator)} the two numbers")

3. Finally the part where it prints the actual answer inside the result variable.

result = case operator
           when '1'
             number1.to_i() + number2.to_i()
           when '2'
             number1.to_i() - number2.to_i()
           when '3'
             number1.to_i() * number2.to_i()
           else
             number1.to_f() / number2.to_f()
           end

  prompt("The answer is: #{result}")

Do you have any idea why it's skipping this codes?

Upvotes: 1

Views: 106

Answers (1)

Felix
Felix

Reputation: 4716

Problem: As far as I understand, your prompt message tries to translate and probably doesnt find a translation for e.g. "Hi, Felix".

You could check on this theory quickly by changing the message function:

def messages(message, lang='en')
  MESSAGES[lang][message] || "NO TRANSLATION FOUND"
end

and observe the output again.

Update As OP confirmed the theory and requested a 'solution' in the comments, the cheapest one might be this one:

def messages(message, lang='en')
  # Look up translation, default to untranslated message.
  MESSAGES[lang][message] || message
end

Upvotes: 1

Related Questions