🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of alexwebgr

alexwebgr's solution

to Bob in the Ruby Track

Published at Jan 11 2021 · 0 comments
Instructions
Test suite
Solution

Bob is a lackadaisical teenager. In conversation, his responses are very limited.

Bob answers 'Sure.' if you ask him a question.

He answers 'Whoa, chill out!' if you yell at him.

He answers 'Calm down, I know what I'm doing!' if you yell a question at him.

He says 'Fine. Be that way!' if you address him without actually saying anything.

He answers 'Whatever.' to anything else.

Bob's conversational partner is a purist when it comes to written communication and always follows normal rules regarding sentence punctuation in English.


For installation and learning resources, refer to the Ruby resources page.

For running the tests provided, you will need the Minitest gem. Open a terminal window and run the following command to install minitest:

gem install minitest

If you would like color output, you can require 'minitest/pride' in the test file, or note the alternative instruction, below, for running the test file.

Run the tests from the exercise directory using the following command:

ruby bob_test.rb

To include color from the command line:

ruby -r minitest/pride bob_test.rb

Source

Inspired by the 'Deaf Grandma' exercise in Chris Pine's Learn to Program tutorial. http://pine.fm/LearnToProgram/?Chapter=06

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

bob_test.rb

require 'minitest/autorun'
require_relative 'bob'

# Common test data version: 1.6.0 42b9d45
class BobTest < Minitest::Test
  def test_stating_something
    # skip
    remark = "Tom-ay-to, tom-aaaah-to."
    assert_equal "Whatever.", Bob.hey(remark), %q{Bob hears "Tom-ay-to, tom-aaaah-to.", and..}
  end

  def test_shouting
    skip
    remark = "WATCH OUT!"
    assert_equal "Whoa, chill out!", Bob.hey(remark), %q{Bob hears "WATCH OUT!", and..}
  end

  def test_shouting_gibberish
    skip
    remark = "FCECDFCAAB"
    assert_equal "Whoa, chill out!", Bob.hey(remark), %q{Bob hears "FCECDFCAAB", and..}
  end

  def test_asking_a_question
    skip
    remark = "Does this cryogenic chamber make me look fat?"
    assert_equal "Sure.", Bob.hey(remark), %q{Bob hears "Does this cryogenic chamber make me look fat?", and..}
  end

  def test_asking_a_numeric_question
    skip
    remark = "You are, what, like 15?"
    assert_equal "Sure.", Bob.hey(remark), %q{Bob hears "You are, what, like 15?", and..}
  end

  def test_asking_gibberish
    skip
    remark = "fffbbcbeab?"
    assert_equal "Sure.", Bob.hey(remark), %q{Bob hears "fffbbcbeab?", and..}
  end

  def test_talking_forcefully
    skip
    remark = "Hi there!"
    assert_equal "Whatever.", Bob.hey(remark), %q{Bob hears "Hi there!", and..}
  end

  def test_using_acronyms_in_regular_speech
    skip
    remark = "It's OK if you don't want to go work for NASA."
    assert_equal "Whatever.", Bob.hey(remark), %q{Bob hears "It's OK if you don't want to go work for NASA.", and..}
  end

  def test_forceful_question
    skip
    remark = "WHAT'S GOING ON?"
    assert_equal "Calm down, I know what I'm doing!", Bob.hey(remark), %q{Bob hears "WHAT'S GOING ON?", and..}
  end

  def test_shouting_numbers
    skip
    remark = "1, 2, 3 GO!"
    assert_equal "Whoa, chill out!", Bob.hey(remark), %q{Bob hears "1, 2, 3 GO!", and..}
  end

  def test_no_letters
    skip
    remark = "1, 2, 3"
    assert_equal "Whatever.", Bob.hey(remark), %q{Bob hears "1, 2, 3", and..}
  end

  def test_question_with_no_letters
    skip
    remark = "4?"
    assert_equal "Sure.", Bob.hey(remark), %q{Bob hears "4?", and..}
  end

  def test_shouting_with_special_characters
    skip
    remark = "ZOMG THE %^*@\#$(*^ ZOMBIES ARE COMING!!11!!1!"
    assert_equal "Whoa, chill out!", Bob.hey(remark), %q{Bob hears "ZOMG THE %^*@\#$(*^ ZOMBIES ARE COMING!!11!!1!", and..}
  end

  def test_shouting_with_no_exclamation_mark
    skip
    remark = "I HATE THE DENTIST"
    assert_equal "Whoa, chill out!", Bob.hey(remark), %q{Bob hears "I HATE THE DENTIST", and..}
  end

  def test_statement_containing_question_mark
    skip
    remark = "Ending with ? means a question."
    assert_equal "Whatever.", Bob.hey(remark), %q{Bob hears "Ending with ? means a question.", and..}
  end

  def test_non_letters_with_question
    skip
    remark = ":) ?"
    assert_equal "Sure.", Bob.hey(remark), %q{Bob hears ":) ?", and..}
  end

  def test_prattling_on
    skip
    remark = "Wait! Hang on. Are you going to be OK?"
    assert_equal "Sure.", Bob.hey(remark), %q{Bob hears "Wait! Hang on. Are you going to be OK?", and..}
  end

  def test_silence
    skip
    remark = ""
    assert_equal "Fine. Be that way!", Bob.hey(remark), %q{Bob hears "", and..}
  end

  def test_prolonged_silence
    skip
    remark = "          "
    assert_equal "Fine. Be that way!", Bob.hey(remark), %q{Bob hears "          ", and..}
  end

  def test_alternate_silence
    skip
    remark = "\t\t\t\t\t\t\t\t\t\t"
    assert_equal "Fine. Be that way!", Bob.hey(remark), %q{Bob hears "\t\t\t\t\t\t\t\t\t\t", and..}
  end

  def test_multiple_line_question
    skip
    remark = "\nDoes this cryogenic chamber make me look fat?\nNo."
    assert_equal "Whatever.", Bob.hey(remark), %q{Bob hears "\nDoes this cryogenic chamber make me look fat?\nNo.", and..}
  end

  def test_starting_with_whitespace
    skip
    remark = "         hmmmmmmm..."
    assert_equal "Whatever.", Bob.hey(remark), %q{Bob hears "         hmmmmmmm...", and..}
  end

  def test_ending_with_whitespace
    skip
    remark = "Okay if like my  spacebar  quite a bit?   "
    assert_equal "Sure.", Bob.hey(remark), %q{Bob hears "Okay if like my  spacebar  quite a bit?   ", and..}
  end

  def test_other_whitespace
    skip
    remark = "\n\r \t"
    assert_equal "Fine. Be that way!", Bob.hey(remark), %q{Bob hears "\n\r \t", and..}
  end

  def test_non_question_ending_with_whitespace
    skip
    remark = "This is a statement ending with whitespace      "
    assert_equal "Whatever.", Bob.hey(remark), %q{Bob hears "This is a statement ending with whitespace      ", and..}
  end
end
class Bob
  RESPONSES = {
    question: 'Sure.',
    yelling: 'Whoa, chill out!',
    yelling_question: 'Calm down, I know what I\'m doing!',
    silence: 'Fine. Be that way!',
    whatever: 'Whatever.'
  }

  attr_reader :remark

  def initialize(remark)
    @remark = remark.gsub(/[^a-zA-Z0-9?]/, '')
  end

  def self.hey(remark)
    new(remark).hey
  end

  def hey
    return RESPONSES[:yelling_question] if yelling_question?
    return RESPONSES[:question] if question?
    return RESPONSES[:yelling] if yelling?
    return RESPONSES[:silence] if silence?

    RESPONSES[:whatever]
  end

  private

  def question?
    remark.end_with?('?')
  end

  def yelling?
    # return false if it is a digit and upcase? since numbers and other characters return true when tested if they are upcase
    # but we need to cater for the scenario where there are digits and characters which should return true of yelling
    # using chopped_remark to remove the ? from the comparison so the digit check returns true when there is just digit and ?
    return false if chopped_remark !~ /\D/ && upcase?

    upcase?
  end

  def upcase?
    chopped_remark == chopped_remark.upcase
  end

  def yelling_question?
    question? && yelling?
  end

  def silence?
    remark.empty?
  end

  def chopped_remark
    remark.end_with?('?') ? remark.chop : remark
  end
end

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?