Skip to content

Instantly share code, notes, and snippets.

@oneawayman
Last active August 29, 2015 13:56
Show Gist options
  • Save oneawayman/8829763 to your computer and use it in GitHub Desktop.
Save oneawayman/8829763 to your computer and use it in GitHub Desktop.
Bloc Web Development Hashes Checkpoint
## Setting Attributes ##
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
## Iterating over a Hash ##
def hash_to_array(h = {})
a = []
h.each do |key, value|
a << "#{key} is #{value}"
end
p a
end
## Hash Methods ##
# Create a method named merge_us that expects two arguments and combines them, assuming they're hashes.
def merge_us(h1, h2)
h1.merge!(h2)
end
# Create a method named my_keys that takes an argument and returns an Array of keys.
def my_keys(h)
h.keys
end
# Create a method named do_i_have? which expects two arguments. The arguments should represent a Hash and Array of keys.
# do_i_have? should return true if the Array has all of the keys in the hash, and false if it does not.
def do_i_have?(hash, array_of_keys)
if hash.keys.sort == array_of_keys.sort
true
else
false
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment