Created
June 14, 2013 12:43
-
-
Save stevenabrooks/5781515 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Testing / Assertion | |
# Define a method that takes two values and compares them, printing pass or fail | |
class Person | |
attr_accessor :name, :birthday :age | |
def name(string) | |
@name = string | |
end | |
def birthday(string) | |
@birthday = string | |
end | |
def age | |
@age = ((time.now.year.to_i) - (@birthday.slice #need to fill this in.to_i)) | |
end | |
end | |
# assert_equal(1,1) #=> pass | |
# assert_equal(2,1) #=> fail | |
def assert_equal(actual, expected) | |
if actualy== expected | |
puts "pass" | |
else | |
puts "fail" | |
end | |
end | |
# Use assert to test the following: | |
# define a method that creates an Array of Greetings for every person's name | |
# greetings(["Bob", "Tim", "Jake"]) #=> ["Hello Bob!", "Hello Tim!", "Hello Jake!"] | |
new_array = [] | |
def greetings(names) | |
names.each do |name| | |
new_array = "Hello #{name}!" | |
end | |
names = ["Brad", "Tim", "Jake"] | |
assert_equal( | |
greetings(names), | |
["Hello Brad!", "Hello Tim!", "Hello Jake!"] | |
) | |
# define a method to sum the values of an array. Make this method defend against nils and | |
# other errors | |
def sum(numbers) | |
end | |
########UNSURE | |
assert_equal sum([]), 0 | |
assert_equal sum([1,2]), 3 | |
assert_equal sum([1,nil,2]), 3 | |
assert_equal sum([1, "2", 2]), 3 | |
# define a method that returns comfortable for temps between 60-80, cold for below and hot | |
# for above. | |
def temperature_bot(temp) | |
if temp == (60..80) | |
puts "comfortable" | |
elsif temp < 60 | |
puts "cold" | |
elsif temp > 80 | |
puts "hot" | |
end | |
assert_equal temperature_bot(65), "comfortable" | |
assert_equal temperature_bot(70), "comfortable" | |
assert_equal temperature_bot(85), "hot" | |
assert_equal temperature_bot(30), "cold" | |
# define an object, Person, that has a birthdate and a name. Define a method for a person | |
# that returns their age. | |
begin | |
person = Person.new | |
person.name = "Tim Berners-Lee" | |
person.birthday = "06/08/1955" | |
assert_equal person.name, "Tim Berners-Lee" | |
assert_equal person.birthday, "06/08/1955" | |
assert_equal person.age, 58 | |
rescue => e | |
puts "Fail: #{e}" | |
end | |
class Person | |
attr_accessor :name :birthday | |
def age | |
year = self.birthday.split("/").last | |
2013 - year.to_i | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment