0

Below are 2 methods I've tried to implement in a Ruby program to pass an rspec being passed into it.

it is part of a larger class Dictionary

I've handled about 12 other specs for this program, but keep getting an error when it gets to the following rspec (Admittedly, part of the problem that I might be having is that when i've tried running these, i keep getting errors that the "should" answers should be rewritten with "expect" and the way I've rewritten could be incorrect:

it 'can produce printable output like so: [keyword] "definition"' do
    @d.add('zebra' => 'African land animal with stripes')
    @d.add('fish' => 'aquatic animal')
    @d.add('apple' => 'fruit')
    expect(@d.printable).to eql(%Q{[apple] "fruit"\n[fish] "aquatic animal"\n[zebra] "African land animal with stripes"})

class Dictionary
  # TODO: your code goes here!
  attr_accessor :word, :define, :words

  def initialize
    @d = Hash.new(0)
  end

  def entries
    @d
  end

  def keywords
    @d.keys.sort
  end  

  def printable
    opts_sorted = @d.sort_by { |word, defin| word}
    opts_sorted.map{ |word, defin| "[#{word}] \"#{defin}\"\n" }.join
   end

end

the output on the screen when i try it seems to be correct, but I keep getting the following error 1) Dictionary can produce printable output like so: [keyword] "value" each on a new line

Failure/error: ****the expect line from the rspec above NoMethodError: undefined method "printable" for #(Dictionary: (some numbers) .spec/11_dictionary_spec.rb:81 in 'block (2 levels) in

thanks in advance if you can help

7
  • are those methods in your test file or in your dictionary class?
    – Anthony
    Oct 13, 2014 at 18:05
  • Hi Anthony, thanks for taking a look at this and helping.. the def "printable" methods are in the Dictionary class.. I posted both, though i know i can only run 1 at a time. thought i'd post both just for the sake of someone seeing both methods i've tried
    – 2iron
    Oct 13, 2014 at 18:28
  • 1
    Ok, we need a little more context here. Where are you initializing @d? Can we see that block? Also can you separate the code snips into a test_file and a lib_file for clarity sake?
    – Anthony
    Oct 13, 2014 at 18:52
  • Anthony - edited original entry with more of code.. hope it helps.. don't have both printable methods active at same time, but put in to easily copy. comment out one if you want to test. also, didn't copy whole program, but what i believe is enough to get the gist. ps... where is best place to better understand rspec terminology and about classes, method writing or ruby syntax in general...thanks
    – 2iron
    Oct 13, 2014 at 22:01
  • Anthony - okay, the printable matches the output requested by the rspec except that there is an additional /n at the end (after the last item in the array... any way to rewrite to eliminate the final /n. do i need to rewrite as an if statement that if it's the last item in the array, the last /n is not included in the string, or once mapped, can i go to the last item in the array and strip off the last couple of items (/n) in the string? thanks again for help.... if you couldn't guess, a noob in Ruby...
    – 2iron
    Oct 13, 2014 at 22:41

0

Your Answer

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

Browse other questions tagged or ask your own question.