-# Stuff you'll learn:
-# method_missing
-# nested closures
-# "builder pattern"
-
-require "xml_document"
-
-describe XmlDocument do
- before do
- @xml = XmlDocument.new
- end
-
- it "renders an empty tag" do
- expect(@xml.hello).to eq("<hello/>")
- end
-
- it "renders a tag with attributes" do
- expect(@xml.hello(:name => 'dolly')).to eq("<hello name='dolly'/>")
- end
-
- it "renders a randomly named tag" do
- tag_name = (1..8).map{|i| ('a'..'z').to_a[rand(26)]}.join
- expect(@xml.send(tag_name)).to eq("<#{tag_name}/>")
- end
-
- it "renders block with text inside" do
- expect(@xml.hello do
- "dolly"
- end).to eq("<hello>dolly</hello>")
- end
-
- it "nests one level" do
- expect(@xml.hello do
- @xml.goodbye
- end).to eq("<hello><goodbye/></hello>")
- end
-
- it "nests several levels" do
- xml = XmlDocument.new
- expect(xml.hello do
- xml.goodbye do
- xml.come_back do
- xml.ok_fine(:be => "that_way")
- end
- end
- end).to eq("<hello><goodbye><come_back><ok_fine be='that_way'/></come_back></goodbye></hello>")
- end
-
- it "indents" do
- @xml = XmlDocument.new(true)
- expect(@xml.hello do
- @xml.goodbye do
- @xml.come_back do
- @xml.ok_fine(:be => "that_way")
- end
- end
- end).to eq(
- "<hello>\n" +
- " <goodbye>\n" +
- " <come_back>\n" +
- " <ok_fine be='that_way'/>\n" +
- " </come_back>\n" +
- " </goodbye>\n" +
- "</hello>\n")
- end
-end
-
-
-
-
-
-
diff --git a/13_xml_document/xml_document_spec.rb b/13_xml_document/xml_document_spec.rb
deleted file mode 100644
index fa1995818..000000000
--- a/13_xml_document/xml_document_spec.rb
+++ /dev/null
@@ -1,77 +0,0 @@
-# # Topics
-#
-# * method_missing
-# * blocks
-# * strings
-# * hashes
-#
-# # Builder
-#
-# Here we will implement the so-called Builder Pattern: we will make an object, XmlDocument, that *builds* an XML file, which can contain arbitrary tags.
-
-# Stuff you'll learn:
-# method_missing
-# nested closures
-# "builder pattern"
-
-require "xml_document"
-
-describe XmlDocument do
- before do
- @xml = XmlDocument.new
- end
-
- it "renders an empty tag" do
- expect(@xml.hello).to eq("")
- end
-
- it "renders a tag with attributes" do
- expect(@xml.hello(:name => 'dolly')).to eq("")
- end
-
- it "renders a randomly named tag" do
- tag_name = (1..8).map{|i| ('a'..'z').to_a[rand(26)]}.join
- expect(@xml.send(tag_name)).to eq("<#{tag_name}/>")
- end
-
- it "renders block with text inside" do
- expect(@xml.hello do
- "dolly"
- end).to eq("dolly")
- end
-
- it "nests one level" do
- expect(@xml.hello do
- @xml.goodbye
- end).to eq("")
- end
-
- it "nests several levels" do
- xml = XmlDocument.new
- expect(xml.hello do
- xml.goodbye do
- xml.come_back do
- xml.ok_fine(:be => "that_way")
- end
- end
- end).to eq("")
- end
-
- it "indents" do
- @xml = XmlDocument.new(true)
- expect(@xml.hello do
- @xml.goodbye do
- @xml.come_back do
- @xml.ok_fine(:be => "that_way")
- end
- end
- end).to eq(
- "\n" +
- " \n" +
- " \n" +
- " \n" +
- " \n" +
- " \n" +
- "\n")
- end
-end
diff --git a/14_array_extensions/array_extensions.rb b/14_array_extensions/array_extensions.rb
new file mode 100644
index 000000000..1c30761ed
--- /dev/null
+++ b/14_array_extensions/array_extensions.rb
@@ -0,0 +1,11 @@
+class Array
+ def sum
+ self.reduce(0) { |a, b| a + b }
+ end
+ def square
+ self.map { |item| item * item }
+ end
+ def square!
+ self.map! { |item| item * item }
+ end
+end
diff --git a/15_in_words/in_words.rb b/15_in_words/in_words.rb
new file mode 100644
index 000000000..326b39bb3
--- /dev/null
+++ b/15_in_words/in_words.rb
@@ -0,0 +1,76 @@
+require 'pry'
+require 'pry-byebug'
+
+class Fixnum
+ def in_words
+ string = self.to_s
+ one_less_digit = string[1..string.length].to_i
+ first_digit = string[0].to_i
+ output = ""
+ orders_of_magnitude = {
+ 1000000000000 => "trillion",
+ 1000000000 => "billion",
+ 1000000 => "million",
+ 1000 => "thousand",
+ 100 => "hundred",
+ }
+ orders_of_magnitude.each do |magnitude, word|
+ if self >= magnitude
+ quantity = self / magnitude
+ remainder = self - quantity * magnitude
+ if remainder > 0
+ output += quantity.in_words + ' ' + word + ' ' + remainder.in_words
+ else
+ output += quantity.in_words + ' ' + word
+ end
+ return output
+ end
+ end
+ dictionary = {
+ 90 => "ninety",
+ 80 => "eighty",
+ 70 => "seventy",
+ 60 => "sixty",
+ 50 => "fifty",
+ 40 => "forty",
+ 30 => "thirty",
+ 20 => "twenty",
+ 19 => "nineteen",
+ 18 => "eighteen",
+ 17 => "seventeen",
+ 16 => "sixteen",
+ 15 => "fifteen",
+ 14 => "fourteen",
+ 13 => "thirteen",
+ 12 => "twelve",
+ 11 => "eleven",
+ 10 => "ten",
+ 9 => "nine",
+ 8 => "eight",
+ 7 => "seven",
+ 6 => "six",
+ 5 => "five",
+ 4 => "four",
+ 3 => "three",
+ 2 => "two",
+ 1 => "one"
+ }
+ dictionary.each do |match, word|
+ if self/match > 0 && self > 20
+ if one_less_digit == 0
+ output += word
+ else
+ output += word + ' ' + one_less_digit.in_words
+ end
+ output.strip!
+ break
+ elsif self == match
+ output = word
+ output.strip!
+ elsif self/10 == 0 && self == 0
+ output = "zero"
+ end
+ end
+ output
+ end
+end