Forked from stevebartholomew/gist:1feab9d6fa5128f618c4
Last active
June 22, 2017 12:47
-
-
Save robertomiranda/90be9269fde72024e288c3fa5914c234 to your computer and use it in GitHub Desktop.
Ruby refactoring exercise
This file contains hidden or 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
class Array | |
def to_annotated_xml(root) | |
output = "<#{root}>" | |
each do |i| | |
if i.is_a?(Fixnum) | |
output << "<number>#{i}</number>" | |
elsif i.is_a?(String) | |
if i.match(/@/) | |
output << "<email>#{i}</email>" | |
else | |
output << "<string>#{i}</string>" | |
end | |
else | |
output << "<value>#{i}</value>" | |
end | |
end | |
output << "</#{root}>" | |
end | |
end | |
p [1, "Stephen", "[email protected]"].to_annotated_xml("person") |
This file contains hidden or 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
class Object | |
def to_annotated_xml | |
"<value>#{self}</value>" | |
end | |
end | |
class String | |
def to_annotated_xml | |
tag = self.match(/@/) ? 'email' : 'string' | |
"<#{tag}>#{self}</#{tag}>" | |
end | |
end | |
class Fixnum | |
def to_annotated_xml | |
"<number>#{self}</number>" | |
end | |
end | |
class Array | |
def to_annotated_xml(root) | |
output = "<#{root}>" | |
each do |i| | |
output << i.to_annotated_xml | |
end | |
output << "</#{root}>" | |
end | |
end | |
p [1, "Stephen", "[email protected]"].to_annotated_xml("person") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment