Skip to content

adrianeyre/fizzbuzz-Ruby

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 

Repository files navigation

FizzBuzz Pair Programming Challenge

Technologies: Ruby, Rspec

Pair Partner - Joe Marriott

The Challenge

The challenge was to make a simple “FizzBuzz” program that accepted one integer value into a method called fizzbuzz. The method checks for the following conditions: if the integer is divisible by 3 it returns ‘fizz’, if the integer is divisible by 5 it returns ‘buzz’, if the integer is divisible by 3 and 5 it returns ‘fizzbuzz’ and if none of the conditions are met it returns the integer. Example below

1 --> 1
2 --> 2
3 --> fizz
4 --> 4
5 --> buzz
6 --> fizz
7 --> 7
8 --> 8
9 --> fizz
10 --> buzz
11 --> 11
12 --> fizz
13 --> 13
14 --> 14
15 --> fizzbuzz
16 --> 16
17 --> 17
18 --> fizz
19 --> 19
20 --> buzz

As part of the challenge we took it in turns writing a failing test while the second partner had to write some code to make the test pass. We continued this process until all test were passed and the challenge was complete. This process is commonly known as the "Ping Pong" method

We split the task into several steps

Firstly we created a local repository on our computers

> mkdir fizzbuzz                      # Make the fizzbuzz directory
> cd fizzbuzz                         # Move to the fizzbuzz directory
> git init                            # Initialise the local git repository
> touch README.md                     # Create a blank README file
> git add README.md                   # Added the README file from the staging area to the repository
> git commit -m "Initial Commit"      # Commited the changes

We logged into our Github account and created a repository called 'fizzbuzz' without initialising the "README.md" file because we had already created one

We then linked our local repository to the Github repository

> git remote add origin https://github.com/adrianeyre/fizzbuzz.git    # Link repositories
> git push -u origin master                                           # Push local to remote

So that we were able to collaborate our projects we had to give each other permission to our repositories.

> git remote add <pair-partner> <URL-to-pair-partners-repo>     # Link my repository with Joes's

> git fetch                                                     # Fetch Joe's repository
> git pull <pair-partner> master                                # Pull it to my local repository

After we complete a section of code the partner who was coding had to add their file, commit them and then push the file up to their Github repository.

> git add <filename>            # Add file from staging area to repository
> git commit -m "Description"   # Commit changes
> git push                      # Push local to remote

We used the RSPEC framework to test our code to make sure it worked 100%. RSPEC once initialised creates two folders 'spec' where it stores the testing files and ‘lib’ where it stores the application code. Initially we had to install the RSPEC Gem then run the command to set up the directory structure.

> gem install rspec
> rspec --init

The first partner created a file called ‘fizzbuzz_spec.rb” in the ./spec directory. We named it in this way because the application file was going to be called ‘fizzbuzz.rb’ and was going to be saved in the ./lib directory.

The first test was nice and simple to check if we pass the integer 3 into the method ‘fizzbuzz’ it should return the string ‘fizz’

require 'fizzbuzz'                      # Let RSPEC know what file to test

describe 'fizzbuzz' do                  # The method we're testing
  it '3 equals to fizz' do              # Description of test
    expect(fizzbuzz(3)).to eq 'fizz'    # Condition for test
  end
end

We then ran the RSPEC command to check if our tested passed or failed

> rspec ./spec/fizzbuzz_spec.rb

As the file at this point was not created RSPEC gave some error messages, so it was down to the second partner to create the file and write the code that passed the test.

We continued creating tests and code to pass the tests until we met all the requirements of the application.

RSPEC tests

require 'fizzbuzz'

describe 'fizzbuzz method' do
  it '3 equals to fizz' do
    expect(fizzbuzz(3)).to eq 'fizz'
  end
  it '5 equals to buzz' do
    expect(fizzbuzz(5)).to eq 'buzz'
  end
  it '15 equals to fizzbuzz' do
    expect(fizzbuzz(15)).to eq 'fizzbuzz'
  end
  it '8 equals to 8' do
    expect(fizzbuzz(8)).to eq 8
  end
end

describe 'fizzbuzz Fixnum' do
  it '3 equals to fizz' do
    expect(3.fizzbuzz).to eq 'fizz'
  end
  it '5 equals to buzz' do
    expect(5.fizzbuzz).to eq 'buzz'
  end
  it '15 equals to fizzbuzz' do
    expect(15.fizzbuzz).to eq 'fizzbuzz'
  end
  it '8 equals to 8' do
    expect(8.fizzbuzz).to eq 8
  end
end

Ruby code

def fizzbuzz(num)
  result = ""
  result += "fizz" if num % 3 == 0
  result += "buzz" if num % 5 == 0
  result == "" ? num : result
end

class Integer
  def fizzbuzz
    result = ""
    result += "fizz" if self % 3 == 0
    result += "buzz" if self % 5 == 0
    result == "" ? self : result
  end
end

About

Pre-Course - Pair Programming Project

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages