Skip to content

Instantly share code, notes, and snippets.

@ryanholm
Last active August 29, 2015 14:02
Show Gist options
  • Save ryanholm/93bbc5ea86e9731e74f5 to your computer and use it in GitHub Desktop.
Save ryanholm/93bbc5ea86e9731e74f5 to your computer and use it in GitHub Desktop.
BLOC Arrays
#CP1
def new_array(a,b,c,d)
[a,b,c,d]
end
def first_and_last(a)
[a.first, a.last]
end
#RSpec
describe "new_array" do
it "creates an array of numbers" do
new_array(1,2,3,4).should eq([1,2,3,4])
end
it "creates an array of strings" do
new_array("a", "b", "c", "d").should eq(["a", "b", "c", "d"])
end
it "creates an array of non-sequential objects" do
new_array(1,4,2,3).should eq([1,4,2,3])
end
end
describe "first_and_last" do
it "creates new array with numbers" do
first_and_last([1,2,3]).should eq([1,3])
end
it "creates new array with strings" do
first_and_last(["a", "b", "c", "d"]).should eq(["a", "d"])
end
end
#CP2
def reverse_plus_one(a)
a << a.first
a.reverse
end
def pluses_everywhere(a)
a.join("+")
end
def array_quantity_plus_one(a)
a.count + 1
end
#RSpec
describe "reverse_plus_one" do
it "creates a new array for a short array" do
reverse_plus_one([1,2]).should eq([1,2,1])
end
it "creates a new array for a longer array" do
reverse_plus_one([1,2,3,4]).should eq([1,4,3,2,1])
end
end
describe "pluses_everywhere" do
it "returns a single plus for a short array" do
pluses_everywhere([1,2]).should eq("1+2")
end
it "returns pluses for longer arrays" do
pluses_everywhere([1,2,3,4,5]).should eq("1+2+3+4+5")
end
end
describe "array_quantity_plus_one" do
it "returns the number of items for a short array plus one" do
array_quantity_plus_one([1,2,3]).should eq(4)
end
it "returns the number of items for a long array plus one" do
array_quantity_plus_one([1,532,23,35,52,39]).should eq(7)
end
end
#CP3
def add_item(item, list)
list << item
end
def add_item(item, list)
(list << item).uniq
end
def add_item(item, list)
if !(list.include?(item))
list << item
else
list
end
end
def remove_item(item, list)
list.delete(item)
list
end
def full_list(list) #the list argument is expected to be an array of grocery items
list.uniq.sort
end
#RSpec
describe "add_item" do
it "adds an item to the end of the list" do
add_item("banana", ["orange"]).should eq(["orange", "banana"])
end
it "doesn't add an item if the list already has it" do
add_item("orange", ["orange"]).should eq(["orange"])
end
end
describe "remove_item" do
it "removes an item if it's in the list" do
remove_item("orange", ["banana", "orange"]).should eq(["banana"])
end
it "removes an item if it's in the list" do
remove_item("banana", ["banana", "orange"]).should eq(["orange"])
end
it "doesn't remove an item if it's not in the list" do
remove_item("apple", ["banana", "orange"]).should eq(["banana", "orange"])
end
end
describe "full_list" do
it "returns a list sorted" do
list = %w{3 1 2}
list_sorted = %w{1 2 3}
full_list(list).should eq(list_sorted)
end
it "returns a different list sorted" do
list = %w{banana orange apple}
list_sorted = %w{apple banana orange}
full_list(list).should eq(list_sorted)
end
it "returns a list sorted without duplicates" do
list = %w{banana orange apple orange}
list_sorted = %w{apple banana orange}
full_list(list).should eq(list_sorted)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment