Skip to content

Instantly share code, notes, and snippets.

@ryanholm
Last active August 29, 2015 14:02
Show Gist options
  • Save ryanholm/616d6daac4f2e61297f3 to your computer and use it in GitHub Desktop.
Save ryanholm/616d6daac4f2e61297f3 to your computer and use it in GitHub Desktop.
BLOC Hashes
#CP1
class User
attr_accessor :name, :email, :bio, :age, :sex
def initialize(config = {})
@name = config[:name] || "n/a"
@email = config[:email] || "n/a"
@bio = config[:bio] || "n/a"
@age = config[:age] || "n/a"
@sex = config[:sex] || "n/a"
end
end
#RSpec
describe "User" do
describe "initialize" do
it "can set just the name" do
u = User.new(name: "Bob")
u.name.should eq("Bob")
u.email.should eq("n/a")
end
it "can set all values" do
u = User.new(name: "Joe", email: "[email protected]", bio: "Cool dude", age: 34, sex: :male)
u.name.should eq("Joe")
u.email.should eq("[email protected]")
u.bio.should eq("Cool dude")
u.age.should eq(34)
u.sex.should eq(:male)
end
it "can set no values" do
u = User.new
u.name.should eq("n/a")
u.email.should eq("n/a")
u.bio.should eq("n/a")
u.age.should eq("n/a")
u.sex.should eq("n/a")
end
end
end
#CP2
def hash_to_array(h = {})
a = []
h.each do |key, value|
a << "#{key} is #{value}"
end
a
end
describe "hash_to_array" do
it "returns array for short hash" do
hash = { name: "Bob", age: 34 }
array = [ "name is Bob", "age is 34" ]
hash_to_array(hash).should eq(array)
end
it "returns array for longer hash" do
hash = { name: "Joe", age: 34, sex: :male }
array = [ "name is Joe", "age is 34", "sex is male" ]
hash_to_array(hash).should eq(array)
end
end
#CP3 (merge)
def merge_us(h1, h2)
h1.merge(h2)
end
#RSpec
describe "merge_us" do
it "merges two hashes that are unique" do
h1 = { name: "Computer", cost: "$1,000" }
h2 = { first_name: "Bob", age: 34 }
new_hash = { name: "Computer", cost: "$1,000", first_name: "Bob", age: 34 }
merge_us(h1, h2).should eq(new_hash)
end
it "merges two hashes that are have some things in common" do
h1 = { name: "Computer", cost: "$1,000" }
h2 = { name: "Mouse", uuid: "1234" }
new_hash = { name: "Mouse", cost: "$1,000", uuid: "1234" }
merge_us(h1, h2).should eq(new_hash)
end
end
#CP3 2 (Keys)
def my_keys(h)
h.keys
end
#RSpec
describe "my_keys" do
it "returns keys for a small hash" do
h = { name: "Computer", cost: "$1,000" }
keys = [:name, :cost]
my_keys(h).should eq(keys)
end
it "returns keys for a larger hash" do
h = { name: "Mouse", cost: "$5", uuid: "1234" }
keys = [:name, :cost, :uuid]
my_keys(h).should eq(keys)
end
end
#CP3 3
def do_i_have?(hash, array_of_keys)
hash.keys.sort == array_of_keys.sort
end
#RSpec
describe "do_i_have?" do
it "returns false if it doesn't have any of the keys" do
h = { name: "Computer", cost: "$1,000" }
keys = [:age, :bio]
do_i_have?(h, keys).should eq(false)
end
it "returns false if one or more of the keys isn't in the hash" do
h = { name: "Computer", cost: "$1,000" }
keys = [:name, :bio, :cost]
do_i_have?(h, keys).should eq(false)
end
it "returns false if the hash has a different number of keys than the array" do
h = { name: "Computer", cost: "$1,000" }
keys = [:name]
do_i_have?(h, keys).should eq(false)
end
it "returns true if all keys are in the hash" do
h = { name: "Computer", cost: "$1,000", uuid: "1234" }
keys = [:name, :cost, :uuid]
do_i_have?(h, keys).should eq(true)
end
it "returns true if all keys are in the hash, regardless of order" do
h = { name: "Computer", cost: "$1,000", uuid: "1234" }
keys = [:name, :uuid, :cost]
do_i_have?(h, keys).should eq(true)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment