Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## lib/iteration.rb
- # To not overload namespaces
- module Iteration
- # Add 2 to each element and return the new array.
- def add_2(array)
- array.map { |e| e+2 }
- end
- # Delete all smaller than 4 and sort it from lowest to highest
- def del_sort(array)
- array.reject { |e| e<4 }.sort
- end
- # To be able to call the methods via Iteration.method
- module_function :add_2, :del_sort
- end
- ## test/suite/lib/interation.rb
- Baretest.suite "Hands-on" do
- suite 'Iteration' do
- setup :input => 1, "first example" do # input, variante 1 (kann irgend ein objekt sein, z.B. auch ein String)
- @input = [*0..10]
- end
- setup :input => 2, "second example" do
- @input = [10, 2, 1, -2, 14, -1, 13, -4, 6, 8, -5, 3, 7, 12, 9, -3, 0, 15, 11, 5, 4]
- end
- suite "#add2" do
- setup :input => 1 do # ergänze :input variante 1
- @result = [*2..12]
- end
- setup :input => 2 do # ergänze :input variante 2
- @result = [12, 4, 3, 0, 16, 1, 15, -2, 8, 10, -3, 5, 9, 14, 11, -1, 2, 17, 13, 7, 6] # mit sort_by { rand } wüsst ich nicht wirklich wie...
- end
- assert ":input" do
- @result == Iteration.add_2(@input)
- end
- end
- suite "#del_sort" do
- setup :input => 1 do # ergänze :input variante 1
- @result = [*4..10]
- end
- setup :input => 2 do # ergänze :input variante 2
- @result = [*4..15]
- end
- assert "del_sort with :input" do
- @result_del_sort == Iteration.del_sort(@input)
- end
- end
- end
- end
Add Comment
Please, Sign In to add comment