Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Help solve ruby (Rspec) task

So I got this task from a "School" I'm applying to, and I don't really understand it, so what i this for is someone who knows what they are doing to solve it en then tell me why they did the different things they did!

I just want to get an idea of what a person does with this, when they really knows what to do.

It's using Rspec

# Topics

* Hash

* Array

* instance variables

* regular expressions

require 'spec_helper'

describe Dictionary do
  before do
    @d = Dictionary.new
  end

  it 'is empty when created' do
    @d.entries.should == {}
  end

  it 'can add whole entries with keyword and definition' do
    @d.add('fish' => 'aquatic animal')
    @d.entries.should == {'fish' => 'aquatic animal'}
    @d.keywords.should == ['fish']
  end

  it 'add keywords (without definition)' do
    @d.add('fish')
    @d.entries.should == {'fish' => nil}
    @d.keywords.should == ['fish']
  end

  it 'can check whether a given keyword exists' do
    @d.include?('fish').should be false
  end

  it "doesn't cheat when checking whether a given keyword exists" do
    @d.include?('fish').should be false # if the method is empty, this test passes with nil returned
    @d.add('fish')
    @d.include?('fish').should be true # confirms that it actually checks
    @d.include?('bird').should be false # confirms not always returning true after add
  end

  it "doesn't include a prefix that wasn't added as a word in and of itself" do
    @d.add('fish')
    @d.include?('fi').should be false
  end

  it "doesn't find a word in empty dictionary" do
    @d.find('fi').should be_empty # {}
  end

  it 'finds nothing if the prefix matches nothing' do
    @d.add('fiend')
    @d.add('great')
    @d.find('nothing').should be_empty
  end

  it "finds an entry" do
    @d.add('fish' => 'aquatic animal')
    @d.find('fish').should == {'fish' => 'aquatic animal'}
  end

  it 'finds multiple matches from a prefix and returns the entire entry (keyword + definition)' do
    @d.add('fish' => 'aquatic animal')
    @d.add('fiend' => 'wicked person')
    @d.add('great' => 'remarkable')
    @d.find('fi').should == {'fish' => 'aquatic animal', 'fiend' => 'wicked person'}
  end

  it 'lists keywords alphabetically' do
    @d.add('zebra' => 'African land animal with stripes')
    @d.add('fish' => 'aquatic animal')
    @d.add('apple' => 'fruit')
    @d.keywords.should == %w(apple fish zebra)
  end

  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')
    @d.printable.should == %Q{[apple] "fruit"\n[fish] "aquatic animal"\n[zebra] "African land animal with stripes"}
  end
end

1 Answer

They've given you the tests and now you need to write the code to make them pass. I didn't read through them all as you should attempt to solve these on your own. If you get stuck on something particular, then come back and let us know.

Here's an example of how to solve the first couple of tests. Keep in mind, you should write the least amount of code to make the test pass.

First, you need to create a class named Dictionary. You can tell this because the tests are describing Dictionary and there is a before block that creates a new instance of the Dictionary class.

The first test indicates that when a Dictionary instance is created it should have an entries attribute that is an empty hash. So, you can define an initialize method (which gets executed when you call Dictionary.new) with an instance variable named @entries and set it equal to an empty hash. This test also indicates that you need a getter method for the entries attribute. There are a couple of ways to accomplish this, but the easiest is to use the attr_reader method.

The second test indicates that an instance of Dictionary can respond to an add method that takes a key/value pair as its argument. The add method should add the key/value pair to the entries hash. There are multiple ways to accomplish this, but here I chose to use #merge!.

class Dictionary
  attr_reader :entries

  def initialize
    @entries = {}
  end

  def add(hash)
    @entries.merge!(hash)
  end
end

Good luck with your schooling! Again, if you need help with a specific challenge, let me know.

Thank you so much! :)

Hey, would u know any good tutorials or anything like that where I could learn this, I've gotten some from the school, but if u know any other good sources, that would be great!

Thanks!

Hey, would u know any good tutorials or anything like that where I could learn this, I've gotten some from the school, but if u know any other good sources, that would be great!

Thanks!

Here are a couple I have for RSpec:

TutsPlus

Better Specs

Cheers!