Today you come to work as usual and start your programming work as always.
The project manager tells you that today you want to add a new feature to the server, and you want to write a method that handles the book object, wrapping all the fields of the book object in XML format so that you can easily interact with the client later. and print the log before and after the start of the package so that it is convenient to debug and problem-Orient.
No problem! You think this function is a piece of cake, very confidently began to write code.
The book object code is as follows:
Class book
Attr_accessor:book_name,:p ages,:p Rice,: Author, ISBN end
Then write a class that is designed to wrap the book object in XML format:
Class Formatter
def format_book (book)
puts ' format begins ' result
= ' <book_name>#{book.book_name} </book_name>\n "Result
+ +" <pages>#{book.pages}</pages>\n "result
+ =" <price>#{ Book.price}</price>\n "Result
+ +" <author>#{book.author}</author>\n "result
+ +" < isbn>#{book.isbn}</isbn>\n "
puts" format finished "result End"
The calling code is as follows:
Book = book.new
book.book_name = "Programming Ruby"
book.pages = 830 Book.price = \ book.author =
"Dave T Homas "
book.isbn =" 9787121038150 "
formatter = formatter.new result
= Formatter.format_book (book)
puts result
After you've written it, you can't wait to start running and the results are exactly what you expected.
The project manager after reading, is very satisfied with you, the boy efficiency is very high! You are also very proud.
However, two days later, the project manager found you, he said before the need to consider the interaction of the client also includes mobile devices, and mobile devices are compared to eat traffic, in XML format to transmit too much traffic, think the best to change to use the JSON format transmission. However, the previous XML format should also be preserved, preferably by the client specifying which format to use.