Skip to content

Commit 550e68b

Browse files
jairovelasquezactions-user
authored andcommitted
automated commit
0 parents  commit 550e68b

10 files changed

+404
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.idea

.rspec

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
--format documentation
2+
--color

.ruby-version

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
2.6.6

Gemfile

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
source 'http://rubygems.org'
2+
3+
ruby "2.6.6"
4+
5+
gem 'rspec', '~> 3.8'
6+
gem 'cucumber', '2.0.0'
7+
8+
gem 'byebug'

Gemfile.lock

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
GEM
2+
remote: http://rubygems.org/
3+
specs:
4+
builder (3.2.4)
5+
byebug (11.1.3)
6+
cucumber (2.0.0)
7+
builder (>= 2.1.2)
8+
cucumber-core (~> 1.1.3)
9+
diff-lcs (>= 1.1.3)
10+
gherkin (~> 2.12)
11+
multi_json (>= 1.7.5, < 2.0)
12+
multi_test (>= 0.1.2)
13+
cucumber-core (1.1.3)
14+
gherkin (~> 2.12.0)
15+
diff-lcs (1.3)
16+
gherkin (2.12.2)
17+
multi_json (~> 1.3)
18+
multi_json (1.15.0)
19+
multi_test (0.1.2)
20+
rspec (3.8.0)
21+
rspec-core (~> 3.8.0)
22+
rspec-expectations (~> 3.8.0)
23+
rspec-mocks (~> 3.8.0)
24+
rspec-core (3.8.0)
25+
rspec-support (~> 3.8.0)
26+
rspec-expectations (3.8.2)
27+
diff-lcs (>= 1.2.0, < 2.0)
28+
rspec-support (~> 3.8.0)
29+
rspec-mocks (3.8.0)
30+
diff-lcs (>= 1.2.0, < 2.0)
31+
rspec-support (~> 3.8.0)
32+
rspec-support (3.8.0)
33+
34+
PLATFORMS
35+
ruby
36+
37+
DEPENDENCIES
38+
byebug
39+
cucumber (= 2.0.0)
40+
rspec (~> 3.8)
41+
42+
RUBY VERSION
43+
ruby 2.6.6p146
44+
45+
BUNDLED WITH
46+
1.17.3

README.md

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
Ruby Intro
2+
=============
3+
4+
This 3-part homework gives some basic practice in Ruby as well as
5+
getting you accustomed to making testing a regular part of your workflow.
6+
7+
**NOTE: If you are working on a local computer, do not clone this repo to your workspace. Fork it first, then clone your fork.**
8+
9+
10+
Learning Goals
11+
--------------
12+
After completing this assignment, you will know how to:
13+
14+
* Write simple code that uses basic constructs in the Ruby language, including methods and arguments, conditionals, string and array manipulation, regular expressions, and basic object-oriented programming mechanisms
15+
* Understand the Ruby project conventions for where code files and test files are located in a project's directory hierarchy
16+
* Run individual tests or suites of tests using the RSpec unit testing tool
17+
* Understand the basic syntax of RSpec unit tests
18+
19+
Overview
20+
--------
21+
22+
**You may find the [Ruby documentation at ruby-doc.org](https://ruby-doc.org) helpful to have on hand.**
23+
24+
The repo for this assigment follows a fairly standard Ruby convention for codebases: the code
25+
files are stored in `lib/` and the test files are stored in `spec/`.
26+
(We use the RSpec unit-testing framework; if we were using Ruby's default
27+
framework, known as `Test::Unit`, the test files would be under
28+
`test/`.)
29+
30+
**We've placed "starter code" in `lib/ruby_intro.rb`; when you're all done, you
31+
can submit this single file to the autograder.**
32+
33+
However, you can test each of the 3 parts separately. The files
34+
`spec/part[123]_spec.rb` contain RSpec tests for each of the three
35+
parts. For example, to test your answers to Part 1, say `rspec
36+
spec/part1_spec.rb`. `rspec` with no arguments runs the tests in all
37+
the files `spec/*_spec.rb`.
38+
39+
* The line numbers in the RSpec error report will
40+
give you guidance as to which tests failed. (You can check the [RSpec
41+
documentation](http://rspec.info) to see how the `.rspec` file can be
42+
used to customize the output format.)
43+
44+
If you are working in Codio, you are ready to move on to the next part. If you are working on a local computer, proceed with the following steps.
45+
46+
| Local computer setup |
47+
|-----|
48+
Ensure that Ruby 2.6.6 is installed.
49+
50+
To ensure you have the rspec gem installed you need bundler and can then
51+
run bundle install like so:
52+
```sh
53+
$ gem install bundler
54+
$ cd assignment
55+
$ bundle
56+
```
57+
When the above completes successfully you'll have RSpec installed and can
58+
run `rspec` from the command line to test your code.
59+
60+
# 1. Arrays, Hashes, and Enumerables
61+
62+
Check the [Ruby 2.x documentation](http://ruby-doc.org) on `Array`,
63+
`Hash` and `Enumerable` as they could help tremendously with these
64+
exercises. Various Ruby cheatsheets are also helpful as a quick reference! Although Ruby supports looping constructs like 'for' and 'while', consider using block syntax with each for a more idiomatic use of Ruby. :-)
65+
66+
0. Define a method `sum(array)` that takes an array of integers as an argument and returns the sum of its elements. For an empty array it should return zero. Run associated tests via: `$ rspec -e '#sum ' spec/part1_spec.rb` (Make sure you are in the correct directory: `cd assignment` and rspec is installed)
67+
68+
0. Define a method `max_2_sum(array)` which takes an array of integers as an argument and returns the sum of its two largest elements. For an empty array it should return zero. For an array with just one element, it should return that element (Consider if the two largest elements are the same value as well). Run associated tests via: `$ rspec -e '#max_2_sum' spec/part1_spec.rb`
69+
70+
0. Define a method `sum_to_n?(array, n)` that takes an array of integers and an additional integer, n, as arguments and returns true if any two elements in the array of integers sum to n. `sum_to_n?([], n)` should return false for any value of n, by definition. Run associated tests via: `$ rspec -e '#sum_to_n' spec/part1_spec.rb`
71+
72+
You can check your progress on all of the above by running `$ rspec spec/part1_spec.rb`.
73+
74+
75+
# 2. Strings and Regular Expressions
76+
77+
Check the documentation on String and Regexp as they could help tremendously with these exercises. For future reference as well, check out https://rubular.com/ for regex related queries. :-)
78+
79+
0. Define a method `hello(name)` that takes a string representing a name and returns the string "Hello, " concatenated with the name. Run associated tests via: `$ rspec -e '#hello' spec/part2_spec.rb` (Make sure you are in the correct directory: `cd assignment`)
80+
81+
0. Define a method `starts_with_consonant?(s)` that takes a string and returns true if it starts with a consonant and false otherwise. (For our purposes, a consonant is any English letter other than A, E, I, O, U.) Make sure it works for both upper and lower case and for non-letters. Run associated tests via: `$ rspec -e '#starts_with_consonant?' spec/part2_spec.rb`
82+
83+
0. Define a method `binary_multiple_of_4?(s)` that takes a string and returns true if the string represents a binary number that is a multiple of 4, such as '1000'. Make sure it returns false if the string is not a valid binary number. Run associated tests via: `$ rspec -e '#binary_multiple_of_4?' spec/part2_spec.rb`
84+
85+
You can check your progress on all of the above by running `$ rspec spec/part2_spec.rb`.
86+
87+
88+
# 3. Object Oriented Basics
89+
90+
91+
Define a class `BookInStock` which represents a book with an ISBN number, `isbn`, and price of the book as a floating-point number, `price`, as attributes. Run associated tests via: `$ rspec -e 'getters and setters' spec/part3_spec.rb` (Make sure you are in the correct directory: `cd assignment`)
92+
93+
The constructor should accept the ISBN number (a string, since in real life ISBN numbers can begin with zero and can include hyphens) as the first argument and price as second argument, and should raise `ArgumentError` (one of Ruby's built-in exception types) if the ISBN number is the empty string or if the price is less than or equal to zero. Include the proper getters and setters for these attributes. Run associated tests via: `$ rspec -e 'constructor' spec/part3_spec.rb`
94+
95+
Include a method `price_as_string` that returns the price of the book formatted with a leading dollar sign and two decimal places, that is, a price of 20 should format as `$20.00` and a price of 33.8 should format as `$33.80`. Check out formatted string methods in Ruby. Run associated tests via: `$ rspec -e '#price_as_string' spec/part3_spec.rb`
96+
97+
You can check your progress on all of the above by running `rspec spec/part3_spec.rb`.
98+
99+
100+
## More Challenges
101+
102+
* Try getting setup with
103+
an automated test framework such as [guard](http://code.tutsplus.com/tutorials/testing-your-ruby-code-with-guard-rspec-pry--cms-19974) or [autotest](https://rubygems.org/gems/autotest). Guard or AutoTest can be set up so that
104+
they will run all the tests in `spec/`, but every time you edit and save
105+
your code file, the tests are automatically re-run, so you don't have to
106+
run them manually. As we'll see later, this is the "watch the test fail"
107+
part of the TDD or test-driven process of development: write the tests before
108+
you write the code, watch the test fail, fill in the code and save the code file,
109+
then watch the test pass!
110+
111+
* Try pairing using the [one-undermanship pair programming style](http://www.agileventures.org/remote-pair-programming/pair-programming-protocols)

lib/ruby_intro.rb

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# When done, submit this entire file to the autograder.
2+
3+
# Part 1
4+
5+
def sum(arr)
6+
# YOUR CODE HERE
7+
end
8+
9+
def max_2_sum(arr)
10+
# YOUR CODE HERE
11+
end
12+
13+
def sum_to_n?(arr, n)
14+
# YOUR CODE HERE
15+
end
16+
17+
# Part 2
18+
19+
def hello(name)
20+
# YOUR CODE HERE
21+
end
22+
23+
def starts_with_consonant?(s)
24+
# YOUR CODE HERE
25+
end
26+
27+
def binary_multiple_of_4?(s)
28+
# YOUR CODE HERE
29+
end
30+
31+
# Part 3
32+
33+
class BookInStock
34+
# YOUR CODE HERE
35+
end

spec/part1_spec.rb

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../lib/ruby_intro'
4+
5+
describe 'Ruby intro part 1' do
6+
describe '#sum' do
7+
it 'is defined' do
8+
expect { sum([1, 3, 4]) }.not_to raise_error
9+
end
10+
11+
it 'returns correct sum [20 points]', points: 20 do
12+
expect(sum([1, 2, 3, 4, 5])).to be_a Integer
13+
expect(sum([1, 2, 3, 4, 5])).to eq(15)
14+
expect(sum([1, 2, 3, 4, -5])).to eq(5)
15+
expect(sum([1, 2, 3, 4, -5, 5, -100])).to eq(-90)
16+
end
17+
18+
it 'works on the empty array [10 points]', points: 10 do
19+
expect { sum([]) }.not_to raise_error
20+
expect(sum([])).to be_zero
21+
end
22+
end
23+
24+
describe '#max_2_sum' do
25+
it 'is defined' do
26+
expect { max_2_sum([1, 2, 3]) }.not_to raise_error
27+
end
28+
29+
it 'returns the correct sum [7 points]', points: 7 do
30+
expect(max_2_sum([1, 2, 3, 4, 5])).to be_a Integer
31+
expect(max_2_sum([1, 2, 3, 4, 5])).to eq(9)
32+
expect(max_2_sum([1, -2, -3, -4, -5])).to eq(-1)
33+
end
34+
35+
it 'works even if 2 largest values are the same [3 points]', points: 3 do
36+
expect(max_2_sum([1, 2, 3, 3])).to eq(6)
37+
end
38+
39+
it 'returns zero if array is empty [10 points]', points: 10 do
40+
expect(max_2_sum([])).to be_zero
41+
end
42+
43+
it 'returns value of the element if just one element [10 points]', points: 10 do
44+
expect(max_2_sum([3])).to eq(3)
45+
end
46+
end
47+
48+
describe '#sum_to_n' do
49+
it 'is defined' do
50+
expect { sum_to_n?([1, 2, 3], 4) }.not_to raise_error
51+
end
52+
53+
it 'returns true when any two elements sum to the second argument [30 points]', points: 30 do
54+
expect(sum_to_n?([1, 2, 3, 4, 5], 5)).to be true # 2 + 3 = 5
55+
expect(sum_to_n?([3, 0, 5], 5)).to be true # 0 + 5 = 5
56+
expect(sum_to_n?([-1, -2, 3, 4, 5, -8], -3)).to be true # handles negative sum
57+
expect(sum_to_n?([-1, -2, 3, 4, 5, -8], 12)).to be false # 3 + 4 + 5 = 12 (not 3 elements)
58+
expect(sum_to_n?([-1, -2, 3, 4, 6, -8], 12)).to be false # no two elements that sum
59+
end
60+
61+
it 'returns false for any single element array [5 points]', points: 5 do
62+
expect(sum_to_n?([0], 0)).to be false
63+
expect(sum_to_n?([1], 1)).to be false
64+
expect(sum_to_n?([-1], -1)).to be false
65+
expect(sum_to_n?([-3], 0)).to be false
66+
end
67+
68+
it 'returns false for an empty array [5 points]', points: 5 do
69+
expect(sum_to_n?([], 0)).to be false
70+
expect(sum_to_n?([], 7)).to be false
71+
end
72+
end
73+
end

spec/part2_spec.rb

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../lib/ruby_intro'
4+
5+
describe '#hello' do
6+
it 'is defined' do
7+
expect { hello('Testing') }.not_to raise_error # ::NoMethodError)
8+
end
9+
10+
it 'The hello method returns the correct string [30 points]', points: 30 do
11+
expect(hello('Dan').class).to eq(String)
12+
expect(hello('Dan')).to eq('Hello, Dan'), 'Incorrect results for input: "Dan"'
13+
expect(hello('BILL')).to eq('Hello, BILL'), 'Incorrect results for input: "BILL"'
14+
expect(hello('Mr. Ilson')).to eq('Hello, Mr. Ilson'), 'Incorrect results for input: "Mr. Ilson"'
15+
end
16+
end
17+
18+
describe '#starts_with_consonant?' do
19+
it 'is defined' do
20+
expect { starts_with_consonant?('d') }.not_to raise_error # ::NoMethodError)
21+
end
22+
23+
it 'classifies true cases [10 points]', points: 10 do
24+
expect(starts_with_consonant?('v')).to be_truthy, "'v' is a consonant"
25+
%w[v vest Veeee crypt].each do |string|
26+
expect(starts_with_consonant?(string)).to be_truthy, "Incorrect results for input: \"#{string}\""
27+
end
28+
end
29+
30+
it 'classifies false cases [10 points]', points: 10 do
31+
expect(starts_with_consonant?('a')).to be_falsy, "'a' is not a consonant"
32+
%w[asdfgh Unix].each do |string|
33+
expect(starts_with_consonant?(string)).to be_falsy, "Incorrect results for input: \"#{string}\""
34+
end
35+
end
36+
37+
it 'works on the empty string [5 points]', points: 5 do
38+
expect(starts_with_consonant?('')).to be_falsy
39+
end
40+
41+
it 'works on nonletters [5 points]', points: 5 do
42+
expect(starts_with_consonant?('#foo')).to be_falsy
43+
end
44+
end
45+
46+
describe '#binary_multiple_of_4?' do
47+
it 'is defined' do
48+
expect { binary_multiple_of_4?('yes') }.not_to raise_error # ::NoMethodError)
49+
end
50+
51+
it 'classifies valid binary numbers [30 points]', points: 30 do
52+
%w[1010101010100 0101010101010100 100 0].each do |string|
53+
expect(binary_multiple_of_4?(string)).to be_truthy, "Incorrect results for input: \"#{string}\""
54+
end
55+
%w[101 1000000000001].each do |string|
56+
expect(binary_multiple_of_4?(string)).not_to be_truthy, "Incorrect results for input: \"#{string}\""
57+
end
58+
end
59+
60+
it 'rejects invalid binary numbers [10 points]', points: 10 do
61+
expect(binary_multiple_of_4?('a100')).to be_falsy, "'a100' is not a valid binary number!"
62+
expect(binary_multiple_of_4?('')).to be_falsy, 'The empty string is not a valid binary number!'
63+
end
64+
end

0 commit comments

Comments
 (0)