Skip to content
Browse files

solutions 2 perfomed in class

  • Loading branch information...
1 parent eea4891 commit d487103c9777df9c17c9c7e2d62a2a5b06ebd842 @burtlo burtlo committed Nov 1, 2011
View
10 lib/display_superheroes.rb
@@ -8,3 +8,13 @@
# @see http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Method_Calls
# @see Programing Ruby, Chapter 6
#
+
+# display_superheroes(hero) => [ hero ]
+# display_superheroes([ hero_1, hero_2 ]) => [ [ hero_1, hero_2 ] ]
+def display_superheroes(*heroes)
+
+ heroes.flatten.uniq.each do |hero|
+ puts hero
+ end
+
+end
View
17 lib/find_quote.rb
@@ -6,4 +6,19 @@
#
# @see http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Method_Calls
# @see Programing Ruby, Chapter 6
-#
+#
+#
+def find_quote(filename,number = 0)
+ quotes = all_quotes(filename)
+ number = number.to_i
+
+ if quotes.empty?
+ "Could not find a quote at this time"
+ else
+ if number > quotes.length - 1
+ quotes.sample
+ else
+ quotes[number]
+ end
+ end
+end
View
28 lib/search_for_quote.rb
@@ -6,4 +6,30 @@
#
# @see http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Method_Calls
# @see Programing Ruby, Chapter 6
-#
+#
+# { :file => 'filename', ... }
+
+def find_all_quotes_include(quotes,value)
+ quotes.find_all {|quote| quote.include?(value) }
+end
+
+def find_all_quotes_end_with(quotes,value)
+ quotes.find_all {|quote| quote.end_with?(value) }
+end
+
+def find_all_quotes_start_with(quotes,value)
+ quotes.find_all {|quote| quote.include?(value) }
+end
+
+# # { :start_with => asdfdf, :include => asdf }
+def search_for_quote(params = {})
+ found_quotes = all_quotes(params.delete(:file))
+
+ params.each do |key,value|
+ found_quotes = found_quotes.find_all do |quote|
+ quote.send("#{key}?",value) if quote.respond_to? "#{key}?"
+ end
+ end
+
+ found_quotes
+end
View
19 lib/url_builder.rb
@@ -1,5 +1,3 @@
-
-
#
# Here is where you will write the #url_builder method that are defined in the
# associated specifications file.
@@ -10,3 +8,20 @@
# @see http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Method_Calls
# @see Programing Ruby, Chapter 6
#
+
+def url_builder(url,parameters = {})
+ parameters = { :results => 10 }.merge(parameters)
+
+ # KEY=VALUE&KEY=VALUE&KEY=VALUE
+ param_string = parameters.map do |k,v|
+
+ unless v == nil
+ "#{k}=#{v}"
+ end
+
+ end.compact.join("&")
+
+
+ "#{url}?#{param_string}"
+
+end
View
1 spec/display_superheroes_spec.rb
@@ -20,6 +20,7 @@
it "should allow parameters to be Arrays of heroes of single heroes" do
should_receive(:puts).exactly(SuperHeroes.length)
+
display_superheroes SuperHeroes.first, SuperHeroes[1..-1]
end
View
20 spec/find_quote_spec.rb
@@ -25,6 +25,17 @@
quote.should == "The dude abides"
end
+ context "as a String" do
+
+ it "should return any quote" do
+
+ quote = find_quote(TEXT_FILE,"a")
+ quote.should_not be_nil
+
+ end
+
+ end
+
context "when the line specified is greater than the number of quotes" do
it "should return any quote" do
@@ -34,6 +45,15 @@
end
+ context "when the line specified is less than 0" do
+
+ it "should return any quote" do
+ quote = find_quote(TEXT_FILE,-1)
+ quote.should_not be_nil
+ end
+
+ end
+
end
end
View
2 spec/url_builder_spec.rb
@@ -31,7 +31,7 @@
context "when a parameter is nil" do
it "should ignore the parameter" do
- url_builder(url,:results => '20',:search => 'burgers',:sort_by => nil).should == "#{url}?results=20&search=burgers"
+ url_builder(url,:search => 'burgers',:sort_by => nil).should == "#{url}?results=10&search=burgers"
end
end

0 comments on commit d487103

Please sign in to comment.
Something went wrong with that request. Please try again.