- XML Reference Guide
- Overview
- What Is XML?
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Table of Contents
- The Document Object Model
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- DOM and Java
- Informit Articles and Sample Chapters
- Books and e-Books
- Implementations
- DOM and JavaScript
- Using a Repeater
- Repeaters and XML
- Repeater Resources
- DOM and .NET
- Informit Articles and Sample Chapters
- Books and e-Books
- Documentation and Downloads
- DOM and C++
- DOM and C++ Resources
- DOM and Perl
- DOM and Perl Resources
- DOM and PHP
- DOM and PHP Resources
- DOM Level 3
- DOM Level 3 Core
- DOM Level 3 Load and Save
- DOM Level 3 XPath
- DOM Level 3 Validation
- Informit Articles and Sample Chapters
- Books and e-Books
- Documentation and Implementations
- The Simple API for XML (SAX)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- SAX and Java
- Informit Articles and Sample Chapters
- Books and e-Books
- SAX and .NET
- Informit Articles and Sample Chapters
- SAX and Perl
- SAX and Perl Resources
- SAX and PHP
- SAX and PHP Resources
- Validation
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Document Type Definitions (DTDs)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XML Schemas
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- RELAX NG
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Schematron
- Official Documentation and Implementations
- Validation in Applications
- Informit Articles and Sample Chapters
- Books and e-Books
- XSL Transformations (XSLT)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XSLT in Java
- Java in XSLT Resources
- XSLT and RSS in .NET
- XSLT and RSS in .NET Resources
- XSL-FO
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XPath
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XML Base
- Informit Articles and Sample Chapters
- Official Documentation
- XHTML
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XHTML 2.0
- Documentation
- Cascading Style Sheets
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XUL
- XUL References
- XML Events
- XML Events Resources
- XML Data Binding
- Informit Articles and Sample Chapters
- Books and e-Books
- Specifications
- Implementations
- XML and Databases
- Informit Articles and Sample Chapters
- Books and e-Books
- Online Resources
- Official Documentation
- SQL Server and FOR XML
- Informit Articles and Sample Chapters
- Books and e-Books
- Documentation and Implementations
- Service Oriented Architecture
- Web Services
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Creating a Perl Web Service Client
- SOAP::Lite
- Amazon Web Services
- Creating the Movable Type Plug-in
- Perl, Amazon, and Movable Type Resources
- Apache Axis2
- REST
- REST Resources
- SOAP
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- SOAP and Java
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- WSDL
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- UDDI
- UDDI Resources
- XML-RPC
- XML-RPC in PHP
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Ajax
- Asynchronous Javascript
- Client-side XSLT
- SAJAX and PHP
- Ajax Resources
- JSON
- Ruby on Rails
- Creating Objects
- Ruby Basics: Arrays and Other Sundry Bits
- Ruby Basics: Iterators and Persistence
- Starting on the Rails
- Rails and Databases
- Rails: Ajax and Partials
- Rails Resources
- Web Services Security
- Web Services Security Resources
- SAML
- Informit Articles and Sample Chapters
- Books and e-Books
- Specification and Implementation
- XML Digital Signatures
- XML Digital Signatures Resources
- XML Key Management Services
- Resources for XML Key Management Services
- Internationalization
- Resources
- Grid Computing
- Grid Resources
- Web Services Resource Framework
- Web Services Resource Framework Resources
- WS-Addressing
- WS-Addressing Resources
- WS-Notifications
- New Languages: XML in Use
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Google Web Toolkit
- GWT Basic Interactivity
- Google Sitemaps
- Google Sitemaps Resources
- Accessibility
- Web Accessibility
- XML Accessibility
- Accessibility Resources
- The Semantic Web
- Defining a New Ontology
- OWL: Web Ontology Language
- Semantic Web Resources
- Google Base
- Microformats
- StructuredBlogging
- Live Clipboard
- WML
- XHTML-MP
- WML Resources
- Google Web Services
- Google Web Services API
- Google Web Services Resources
- The Yahoo! Web Services Interface
- Yahoo! Web Services and PHP
- Yahoo! Web Services Resources
- eBay REST API
- WordML
- WordML Part 2: Lists
- WordML Part 3: Tables
- WordML Resources
- DocBook
- Articles
- Books and e-Books
- Official Documentation and Implementations
- XML Query
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XForms
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Resource Description Framework (RDF)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Topic Maps
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation, Implementations, and Other Resources
- Rich Site Summary (RSS)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Simple Sharing Extensions (SSE)
- Atom
- Podcasting
- Podcasting Resources
- Scalable Vector Graphics (SVG)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- OPML
- OPML Resources
- Summary
- Projects
- JavaScript TimeTracker: JSON and PHP
- The Javascript Timetracker
- Refactoring to Javascript Objects
- Creating the Yahoo! Widget
- Web Mashup
- Google Maps
- Indeed Mashup
- Mashup Part 3: Putting It All Together
- Additional Resources
- Frequently Asked Questions About XML
- What's XML, and why should I use it?
- What's a well-formed document?
- What's the difference between XML and HTML?
- What's the difference between HTML and XHTML?
- Can I use XML in a browser?
- Should I use elements or attributes for my document?
- What's a namespace?
- Where can I get an XML parser?
- What's the difference between a well-formed document and a valid document?
- What's a validating parser?
- Should I use DOM or SAX for my application?
- How can I stop a SAX parser before it has parsed the entire document?
- 2005 Predictions
- 2006 Predictions
- Nick's Book Picks
So far we've created two simple classes and their objects, and looked at getter and setter methods. We've also touched on input and output, requesting input from and printing strings to the command line.
Now let's up the ante a little bit.
Printing Objects
So far, we've just printed simple strings. But what happens if we have other types of data to print, such as numbers or objects? For example, let's create an array:
... puts "Survey is called #{survey.title}" a = Answer.new("yes") b = Answer.new("no") answers = [a, b] puts "So far we have " + answers.length + " answers."
Here I've created two answers, and added them to an array called
answers
. This array is like the arrays you work with than any other language,
in that in addition to adding and removing items, you can also find out
how many items it has, in this case by getting the length
.
There is, however, a problem here:
Survey is called Retitled Survey demo.rb:40:in `+': cannot convert Fixnum into String (TypeError) from demo.rb:40
Ruby does not like the fact that we are attempting to put a number into a string.
Unlike some other languages, it does not implicitly perform this conversion.
So instead, we'll have to do it ourselves. Fortunately, numbers are also objects,
so we have access to the to_s
method:
... puts "Survey is called #{survey.title}" a = Answer.new("yes") b = Answer.new("no") answers = [a, b] puts "So far we have " + answers.length.to_s + " answers."
Now we can run it:
Survey is called Retitled Survey So far we have 2 answers.
What if we try to print other objects? For example, we can print the
survey
:
... puts survey
This prints the object all right, but it's not very pretty:
Survey is called Retitled Survey So far we have 2 answers. #<Survey:0x2aa6f08>
But is there anything we can do about this? Yes, there is. All classes
are extended from a single root object type, and it is from this ultimate superclass
that they get their implementation of to_s
, which is how they are output to the commandline.
As in other object-oriented languages, however, we can override that method
with our own implementation:
class Survey attr_reader :title attr_writer :title def initialize (givenTitle) @title = givenTitle end def to_s return "Survey: " + @title end end ...
In fact, we can do pretty much anything with this method, such as adding other information, recording how many times it's been printed, and so on. It is a method like any other.
Completing the Structure
The only piece we have left to add is the Question
class:
... def to_s return "Survey: " + @title end end class Question attr_reader :questionText, :answers attr_writer :questionText def initialize (theQuestion, theAnswers) @questionText = theQuestion @answers = theAnswers end end class Answer attr_reader :answerText, :responses ... a = Answer.new("yes") b = Answer.new("no") answers = [a, b] firstQuestion = Question.new("Do you like movie sequels?", answers) a = Answer.new("Star Wars") b = Answer.new("Lord of the Rings") c = Answer.new("Halloween") d = Answer.new("None of the above") sequelAnswers = [a, b, c, d] secondQuestion = Question.new("What is your favorite movie series?", sequelAnswers)
Starting with the Question
definition, we have a class with two
attributes: the actual text of the question, and an array of answers. Moving down to the
actual script, we see that we are creating two arrays of Answer
objects, and then creating Question
objects that accept them
as parameters.
You may be wondering about the reuse of the a
and b
variables. We can do this because in Ruby, all variables are actually references.
When we changed the references to point to new objects, it was the new objects that were added
to the array.
Now we need to add the questions to the actual survey:
class Survey attr_reader :title attr_writer :title def initialize (givenTitle) @title = givenTitle @questions = Array.new end def to_s return "Survey: " + @title end def addQuestion(question) @questions.push(question) end end class Question attr_reader :questionText, :answers ... secondQuestion = Question.new("What is your favorite movie series?", sequelAnswers) survey.addQuestion(firstQuestion) survey.addQuestion(secondQuestion)
Starting at the bottom, we can use the addQuestion
method
to add the question to the survey, and within that method definition, we see
the standard push
method so common in languages dealing with arrays.
This method adds the item to the array. Ruby also has support for the shift
and pop
methods, which remove items from the front and back of the array,
respectively.
Of course, in order for Ruby to know that this method is available, it needs to know that
@questions
is an array, so we'll declare it in the constructor.
But how do we know this is really working? We can check things by requesting specific items:
... survey.addQuestion(firstQuestion) survey.addQuestion(secondQuestion) puts "The second question is: " puts survey.questions[1].questionText puts "The third answer of the second question is: " puts survey.questions[1].answers[2].answerText
In the first case, we are requesting the second item in the
@questions
attribute. (Ruby arrays are zero-based.)
Once we have a reference to that object, which is, remember, a
Question
object, we can request its
questionText
attribute.
In the second case, we request a reference to that same Question
object, but then we are requesting a reference to the third item in its
answers
array. We can then retrieve that object's
the answerText
attribute.
Running the script gives us:
Survey is called Retitled Survey The second question is: What is your favorite movie series? The third answer of the second question is: Halloween
So now we have the basic structure for our application, and we know to how to create and manipulate arrays and their individual items. Next, we'll look at automating things a bit more using arrays.