Avatar of 4d47
0
0
Genius
0
2

4d47's solution

to Bob in the Crystal Track

0
0
Genius
0
2
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 says 'Fine. Be that way!' if you address him without actually saying anything.

He answers 'Whatever.' to anything else.

Setup

Follow the setup instructions for Crystal here:

http://exercism.io/languages/crystal

More help installing can be found here:

http://crystal-lang.org/docs/installation/index.html

Making the Test Suit Pass

Execute the tests with:

$ crystal spec

In each test suite all but the first test have been skipped.

Once you get a test passing, you can unskip the next one by changing pending to it.

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_spec.cr

require "spec"
require "../src/*"

describe "Bob" do
  describe "#hey" do
    it "responds to stating something" do
      Bob.hey("Tom-ay-to, tom-aaaah-to.").should eq "Whatever."
    end

    pending "responds to shouting something" do
      Bob.hey("WATCH OUT!").should eq "Whoa, chill out!"
    end

    pending "responds to asking a question" do
      Bob.hey("Does this cryogenic chamber make me look fat?").should eq "Sure."
    end

    pending "responds to talking forcefully" do
      Bob.hey("Let\'s go make out behind the gym!").should eq "Whatever."
    end

    pending "responds to using acronyms in regular speech" do
      Bob.hey("It\'s OK if you don\'t want to go to the DMV.").should eq "Whatever."
    end

    pending "responds to forceful questions" do
      Bob.hey("WHAT THE HELL WERE YOU THINKING?").should eq "Whoa, chill out!"
    end

    pending "responds to shouting numbers" do
      Bob.hey("1, 2, 3 GO!").should eq "Whoa, chill out!"
    end

    pending "responds to only numbers" do
      Bob.hey("1, 2, 3").should eq "Whatever."
    end

    pending "responds to question with only numbers" do
      Bob.hey("4?").should eq "Sure."
    end

    pending "responds to shouting with special characters" do
      Bob.hey("ZOMG THE %^*@#$(*^ ZOMBIES ARE COMING!!11!!1!").should eq "Whoa, chill out!"
    end

    pending "responds to shouting with no exclamation mark" do
      Bob.hey("I HATE YOU").should eq "Whoa, chill out!"
    end

    pending "responds to statement containing question mark" do
      Bob.hey("Ending with a ? means a question.").should eq "Whatever."
    end

    pending "responds to prattling on" do
      Bob.hey("Wait! Hang on.  Are you going to be OK?").should eq "Sure."
    end

    pending "responds to silence" do
      Bob.hey("").should eq "Fine. Be that way!"
    end

    pending "responds to prolonged silence" do
      Bob.hey("   ").should eq "Fine. Be that way!"
    end
  end
end
class Bob
  def self.hey(s : String) : String
    if s.strip.empty?
      "Fine. Be that way!"
    elsif s =~ /[[:alpha:]]/ && s.upcase == s
      "Whoa, chill out!"
    elsif s.ends_with? '?'
      "Sure."
    else
      "Whatever."
    end
  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
4d47 says

I wanted to remove the yell? method that was monkey patched, that was my sole motivation really.

about 1 year ago
CarolineChaudey says

Why did you choosed if/elsif/else instead ater case/when ?
How is it more efficient ?