diff options
| -rw-r--r-- | Rakefile | 9 | ||||
| -rw-r--r-- | exercises.rb | 82 | ||||
| -rw-r--r-- | spec/functions_spec.rb | 85 |
3 files changed, 176 insertions, 0 deletions
diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..f1ab6b7 --- /dev/null +++ b/Rakefile @@ -0,0 +1,9 @@ +require 'rake' +require 'rake/testtask' + +Rake::TestTask.new do |t| + t.libs << '.' + t.pattern = 'spec/**/*_spec.rb' +end + +task :default => :test diff --git a/exercises.rb b/exercises.rb new file mode 100644 index 0000000..c857a82 --- /dev/null +++ b/exercises.rb @@ -0,0 +1,82 @@ +# Write a function called `divide` that takes 2 arguments. +# This function will divide the first argument by the second argument and return +# the result. +def divide(a, b) + a / b +end + + +# Write a function called `hello5` that will print the string "hello" 5 times, +# with each "hello" on a separate line +def hello5 + 5.times do + puts 'hello' + end +end + + +# Write a function called `hello_x_times` that takes 1 argument which is a +# number. If the argument is 4, "hello" will be printed 4 times. Each "hello" +# must be printed on the same line (e.g. "hellohellohellohello"). +def hello_x_times(count) + count.times do + print 'hello' + end +end + + +# Write a function called `string_plus` that takes 2 string arguments. It will +# return a string with the second argument concatenated to the first ('working' +# and 'example' become 'workingexample'). +def string_plus(str1, str2) + str1 + str2 +end + + +# Write a function called `join_reverse_array` that takes 1 argument, an array +# of strings. It will return a string with each element of the array +# concatenated in reverse order (['az', 'by', 'cx', 'dw'] becomes 'dwcxbyaz'). +def join_reverse_array(arr) + arr.reverse.join +end + + +# Write a function called `array_tack_join` that takes 2 arguments, an array of +# strings, and a string. It will concatenate the second argument to each string +# in the array and return a string with one string in the array per line (e.g. +# ['banana', 'orange', 'apple'], ' cat' would become: +# "banana cat +# orange cat +# apple cat" +def array_tack_join(arr, tack_str) + arr.map do |s| + s + tack_str + end.join "\n" +end + + +# Write a class called `Table`. When initialising `Table`, a decimal height must +# be passed to the initialiser. The table instance's height can then be read and +# set using `my_table.height` and `my_table.height = 20.9` +class Table + attr_accessor :height + + def initialize(height) + @height = height + end +end + + +# Copy your `Table` class and change the copy's name to `Table2`. Add a method +# `height_times2` that returns the table's height * 2. +class Table2 + attr_accessor :height + + def initialize(height) + @height = height + end + + def height_times2 + @height * 2 + end +end diff --git a/spec/functions_spec.rb b/spec/functions_spec.rb new file mode 100644 index 0000000..ad62429 --- /dev/null +++ b/spec/functions_spec.rb @@ -0,0 +1,85 @@ +require 'minitest/spec' +require 'minitest/autorun' + +require 'exercises' + + +describe 'exercises' do + describe '#divide' do + it 'must divide first number by second number' do + divide(12, 3).must_equal 4 + divide(28, 2).must_equal 14 + end + end + + describe '#hello5' do + it 'must print "hello" 5 times' do + -> { hello5 }.must_output <<EOS +hello +hello +hello +hello +hello +EOS + end + end + + describe '#hello_x_times' do + it 'must print "hello" the same number of times specified in the ' \ + 'argument' do + -> { hello_x_times(3) }.must_output 'hellohellohello' + end + end + + describe '#string_plus' do + it 'must return its arguments concatenated together' do + string_plus('Does ', 'this work?').must_equal 'Does this work?' + end + end + + describe '#join_reverse_array' do + it 'must return a string containing array elements concatenated in ' \ + 'reverse order' do + join_reverse_array(['test', 'hello', 'thing', 'example']).must_equal \ + 'examplethinghellotest' + end + end + + describe '#array_tack_join' do + it 'must return a string with each element from the array passed in ' \ + 'concatenated with its string parameter and joined with newlines' do + array_tack_join(['banana', 'orange', 'apple'], ' cat').must_equal \ +'banana cat +orange cat +apple cat' + end + end + + describe 'Table' do + it 'must be initialised with a decimal height' do + t = Table.new(50.4) + t.must_be_instance_of Table + end + + it 'must allow reading of `height`' do + height = 30.21 + t = Table.new(height) + t.height.must_equal height + end + + it 'must allow writing of `height`' do + height = 70.3 + t = Table.new(5) + t.height = height + t.instance_variable_get(:@height).must_equal height + end + end + + describe 'Table2#height_times2' do + it 'must return 2 times the table height' do + height = 67.3 + t = Table2.new(height) + t.height_times2.must_equal height * 2 + end + end +end |
