0
0
0
3

BigChief45's solution

to Bob in the Ruby Track

Instructions
Test Suite
Solution

Bob

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.


For installation and learning resources, refer to the exercism help 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.2.0 6dc2014
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 = "Let's go make out behind the gym!"
    assert_equal "Whatever.", Bob.hey(remark), %q{Bob hears "Let's go make out behind the gym!", and..}
  end

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

  def test_forceful_question
    skip
    remark = "WHAT THE HELL WERE YOU THINKING?"
    assert_equal "Calm down, I know what I'm doing!", Bob.hey(remark), %q{Bob hears "WHAT THE HELL WERE YOU THINKING?", 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_only_numbers
    skip
    remark = "1, 2, 3"
    assert_equal "Whatever.", Bob.hey(remark), %q{Bob hears "1, 2, 3", and..}
  end

  def test_question_with_only_numbers
    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 YOU"
    assert_equal "Whoa, chill out!", Bob.hey(remark), %q{Bob hears "I HATE YOU", 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

  # Problems in exercism evolve over time, as we find better ways to ask
  # questions.
  # The version number refers to the version of the problem you solved,
  # not your solution.
  #
  # Define a constant named VERSION inside of the top level BookKeeping
  # module, which may be placed near the end of your file.
  #
  # In your file, it will look like this:
  #
  # module BookKeeping
  #   VERSION = 1 # Where the version number matches the one in the test.
  # end
  #
  # If you are curious, read more about constants on RubyDoc:
  # http://ruby-doc.org/docs/ruby-doc-bundle/UsersGuide/rg/constants.html

  def test_bookkeeping
    skip
    assert_equal 2, BookKeeping::VERSION
  end
end
class Bob

  def self.hey(remark)
    case remark
    when nothing?
      'Fine. Be that way!'
    when yell?
      'Whoa, chill out!'
    when question?
      'Sure.'
    else
      'Whatever.'
    end
  end

  private

  def self.question?
    -> (r) { r[-1] == '?' }
  end

  def self.yell?
    lambda do |r|
      r = r.delete('^A-Za-z')
      return false if r.empty?

      r.split('').all? { |c| /[[:upper:]]/.match(c) }
    end
  end

  def self.nothing?
    -> (r) { r.strip.size.zero? }
  end

end

What can you learn from this solution?

A huge amount can be learnt 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 I could read more about to develop my understanding?

Community Comments

See what others have said about this solution
about 1 year ago
BigChief45 says

Did some experimenting using Lambdas in when statements.

I really like this kind of approach since I can call the method without passing remark as an argument. Otherwise, calling a class method and passing the remark it as an argument while there is a case remark at the top seems very redundant.

I would love to know if there is a better approach for this.

about 1 year ago
Insti says

This looks good. The use of lambdas is neat.

That said, some things to think about:

The cure (complicating the categorisation methods) might be worse
than the disease (a little bit of redundancy.)

I can call the method without passing remark as an argument. Otherwise, calling a class method and passing the remark it as an argument while there is a case remark at the top seems very redundant.

Might that mean that the case statement is not formulated ideally?
Is the code you're trying to write:
case remark.type
when :nothing
when :yell
when :question

?

What does OO design have to say about switching on the type of an object?

What happens when you want to add a new condition?

It would be nice if we could follow the Open/Closed principle and be able to do it without needing to modify the hey method. (This may require a different approach.)

about 1 year ago
kotp says

I have at least one solution using lambda or Procs myself (I haven't looked lately), but I know I did for Bob.