Skip to content

Instantly share code, notes, and snippets.

@JoseJRVazquez
Created April 3, 2014 22:36
Show Gist options
  • Save JoseJRVazquez/9964262 to your computer and use it in GitHub Desktop.
Save JoseJRVazquez/9964262 to your computer and use it in GitHub Desktop.
To get this started, i opened the terminal and entered IRB for the interactive ruby shell. I entered the requried methods
Joses-MacBook-Air:first-app JRV$ irb
2.0.0-p451 :001 > def add(a,b)
2.0.0-p451 :002?> a-b
2.0.0-p451 :003?> end
=> nil
2.0.0-p451 :004 > p add(5,4)
1
=> 1
2.0.0-p451 :005 >
next i ran the special test to make sue the result equaled what it was supposed to
2.0.0-p451 :006 > def hello #This defines the method
2.0.0-p451 :007?> "hello world" #this was the input
2.0.0-p451 :008?> end #this ends the method
=> nil
2.0.0-p451 :009 > p hello == "hello world" #This function checked is the defined function equaled "hello world".
true
=> true #Since it did match, it answered true
2.0.0-p451 :010 >
rspec is the most popular testing framework, and the two most common terms are describe and it
The folowing is a test in rspec
describe "hello" do
it "says hello to someone" do
hello("Steve", "Jobs").should eq("Hello Steve Jobs.")
end
end
Now this method has two arguments. The first is the name of whats being tested, or "hello". The second is called a block, and its in between the do and end statements. Within those arguments is the methods like IT. the it method is like the describe method, because it takes both string and block arguments. It describes on behavior the block will perfom. It is for each behavior of the HELLO method defined before it. IT can be used multiple times within a single block.
So when an argument is passed to IT, the method is called, and the IT line validates its returns. Then the words SHOULD and EQ are used to validate the comparison. If I read the above line right, it is supposed to tell me that if using the HELLO method, if you are given Steve and Jobs, the string SHOULD EQUAL "Hello Steve Jobs".
So the test to see if it I can do it involved writing an arguement that could pass the follownig tests
describe "link_to" do
it "should return a valid link for Bloc" do
link_to("Bloc", "http://www.bloc.io").should eq("<a href='http://www.bloc.io'>Bloc</a>")
end
it "should return a valid link for Google" do
link_to("Google", "http://www.google.com").should eq("<a href='http://www.google.com'>Google</a>")
end
end
To build the test, it required two arguments and a 'p' argument for each site:
def link_to (link, site) #This sets up the dual arguments of the site and the link
"<a href='#{site}'>#{link}</a>"#this forumla set up how it should output the arguments
end #this closes the method
#these are the variables defined for each one
p link_to("Google", "http://www.google.com")
p link_to("Bloc", "http://www.bloc.io")
At first I assumed it couldbe done in two methods, not realizing one method can hold more than one argument. Much like the compound interest question, it can have many inputs that can be output.
this was hard, but I had to go back in notes and lessons to find the answer. I think the key is remeber what you can. and know where to find the answer for the rest. Onto the next one!
@JoseJRVazquez
Copy link
Author

Also, the site solution was as follows:

def link_to(text, address)
  "<a href='#{address}'>#{text}</a>"
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment