Chapter2. Simple Data Tasks

 
   

Ruby Way
By Hal Fulton
Slots : 1.0
Table of Contents
 


Chapter 2. Simple Data Tasks

IN THIS CHAPTER

  • Strings

  • Regular Expressions

  • Numbers

  • Times and Dates

  • Summary

Theory attracts practice as the magnet attracts iron.

Karl Friedrich Gauss

One measure of the sophistication of a programming language is: What kinds of data will it directly support? The earliest computers were programmed strictly in machine language with purely numeric data. Soon after, the concept of character data and strings of characters was invented, which was crucial to the development of general-purpose languages.

As time goes by, we find ourselves dealing with data of increasing complexity. Modern languages frequently include support for many kinds of data. Note that we don't say types here because the usual notion of a type might be somewhat different. For example, a regular expression might be stored essentially in the form of a character string, but we don't really consider them to be strings because of their special uses.

We could easily add things like arrays and hashes to this list because these are, for Ruby, fairly low-level entities. In fact, there are some incidental uses of these in this chapter. But arrays and hashes (and more complex data structures) deserve a chapter of their own.

This chapter, then, is devoted to four of the most common kinds of data in Ruby. These are strings, regular expressions, numbers, and times and dates.

A string, as in other languages, is simply a sequence of characters. Similar to most entities in Ruby, strings are first-class objects.

Regular expressions form a very condensed notation for describing patterns within text. These have been around for decades and have become even more commonly used in the last 10 years.

Numbers need little explanation; they comprise both integers and floating-point numbers. In Ruby, integers can be of class Fixnum or Bignum, depending on their magnitude.

Times and dates are problematic in any language. Ruby strives to sort through the confusion with an object-oriented interface to the traditional time and date routines.

The alert reader might notice that we don't include the Range class in this discussion. This isn't because ranges aren't useful, but because they aren't that complex; that class is far less rich and interesting than the others covered here. But ranges are certainly covered in incidental code throughout the entire book.

Let's look at some sample code now. We'll begin with strings.


   

 

 



The Ruby Way
The Ruby Way, Second Edition: Solutions and Techniques in Ruby Programming (2nd Edition)
ISBN: 0672328844
EAN: 2147483647
Year: 2000
Pages: 119
Authors: Hal Fulton

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net