0

I am a fairly new to programming (3 months in), and am attempting to learn via TDD.

Obviously the point to TDD is write the test cases first, this particular piece I wasn't sure how to.

The code snippet is:

class PhraseFactory
  def initialize
    @sentence = ''
  end
  def make_sentences_from
    for i in 0 ... self.length
      @sentence += self[i] + ' '
    end
  end  

How I was thinking to test it was using:

describe "When sent a message(<< is that proper terminology?) from an array of strings"
  it "Builds a sentence"
    my_word_array.should_have (here is where I am unclear)sent_a_message_to(make_sentences_from)

Thanks for any help.

2 Answers 2

3

i like TDD but i would not recommend anyone to use TDD to learn something new! it's always a good idea to use the REPL (irb) to experiment with code.

your example is full of WTFs for any ruby developer:

  • you are missing all the ENDs (looks kinda like python?!)
  • you are naming something Factory (are you a java guy?)
  • you use for instead of each
  • you are doing stuff in a class that should be a oneliner
  • you reinvent the wheel by rebuilding core functionality

besides that, i don't really understand your question and code...

what should the result of your code be? what is the input to your "factory"

$ irb
> %(you can just use join to build a sentence from an array of words).join
"you can just use join to build a sentence from an array of words"
Sign up to request clarification or add additional context in comments.

2 Comments

Sorry about the missing ends, noob omission. The class name is a fluke, I just called it that cause of its function. The for instead of each was me playing with code I found in the ruby pickaxe book. I realize the code is all redundant, I am just trying to cement the basics. The input for factory is (supposed to be) something like sentence = this_array_of_strings.make_sentence_from
It also just occurred to me... in order for this to work I suppose it would be necessary to make my method an extension of the Array class, not a class of its own? Otherwise I would have to new it up everytime?
1

Learning both a new language (Ruby) and technique (TDD) at once may be a bit too much. On the other hand, I find unit tests a great way to clarify code behavior, and as such a good learning tool. One suggestion here would be to look into something like the Ruby Koans: http://rubykoans.com/

I am not a Rubyist, so I can't comment on their quality, but I used the F# Koans, which were adapted from the Ruby ones, and were pretty good. This should both give you a good entry point into the language, as well as a familiarity with unit testing, which should serve you well once you start working on your own project and get into TDD.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.