advertisement
Premier Club Log In/Registration
  Include Code  Search Tips
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   SKILLBUILDING  |   TIP BANK  |   SOURCEBANK  |   FORUMS  |   NEWSLETTERS
Browse DevX
Ruby Sample Application
Partners & Affiliates
advertisement
advertisement
Rate this item | 0 users have rated this item.
 Print Print
 
Ruby—A Diamond of a Programming Language, Part 2
Get ready to dive deeper into the power and elegance of the language lauded as a possible contender to replace current programming languages. 

advertisement
uby is an object-oriented, metaprogramming language that has garnered a lot of attention in software development circles as of late. Metaprogramming is a means of writing software programs that write or manipulate other programs, thereby making coding faster and more reliable. Part 1 examined Ruby setup, general syntax, and object-oriented aspects. This article, part 2, looks at some other powerful features built into the Ruby programming language. Again, Ruby is based on and has adopted features from many languages including Lisp, Smalltalk, Python and Perl. So in many ways, Ruby is a "best of breed" language that has been assembled from the best and most powerful programming features found in its predecessors.


Numbers, Strings, and other "Standard" Types in Ruby
Everything in Ruby is an object. Maybe more precisely, everything in Ruby is treated as a full fledged object. So, the number 4, the fixed point number 3.14 and the string "Hi" are all objects in Ruby. Obviously, they are somewhat "special" objects since you don't have to use the new method to create one. Instead, you use the literal 4, for example, to create an instance of the object representing the number 4.

However, when most people learn a new programming language it is helpful to understand "standard" types. So in this section, numbers, strings, Booleans, and a few other base Ruby types are explored.

Numerics
Essentially, numbers in Ruby are classified as either integers or floats. Integers are further subdivided. There are "regular sized" integers and really big integers. Because everything is an object, floats and integers (whether big or small) are defined by classes (see Figure 1). Numeric is the super class for all numbers. Float and Integer classes are subclasses of Numeric. Fixnum and Bignum are subtypes of Integer that define the "regular size" and bigger integer numbers.

Figure 1. Ruby Numbers: The Numeric class is the super class for all Ruby numbers. Concrete instances of numbers are either of type Bignum or Fixnum integers or Float.

Literals are used to represent instances of these classes. The code below in the interactive Ruby shell (irb) shows literal instances of Float, Fixnum and Bignum. Notice that methods (in this case the class method) can be called on the literal. Again, everything in Ruby is an object so you will find all sorts of methods for numeric instances.


irb(main):001:0> 3.class
=> Fixnum
irb(main):002:0> 3.4.class
=> Float
irb(main):003:0> 10000000000000000000.class
=> Bignum
There is some additional syntax for creating numbers as shown in the code listing below. The letter 'E' can be used to represent numbers in exponential notation. Numbers can be proceeded with 0 to represent a number in octal, 0x to represent a number in hex, and 0b to represent a number in binary form. For clarity, underscores can be used in numbers as separators. Be careful not to use a comma as a separator when writing literals. In some cases, this can actually produce an array, which will be discussed below. Finally, a question mark in front of a character (or control or meta character combination) creates a Fixnum instance corresponding to the ASCII character/escape sequence value for the character(s).
What's the real difference between Fixnum and Bignum instances? Fixnum integers can be stored in the machines word minus a bit (usually 16, 32, or 64 bits) while Bignum instances are integers that exceed that sized storage space. The good news for developers is that you don’t have to worry about the size of the integer (see the code sample below). Ruby automatically takes care of any conversion across the Fixnum/Bignum barrier for you!

irb(main):001:0> i=4
=> 4
irb(main):002:0> i.class
=> Fixnum
irb(main):003:0> i=i+100000000000000
=> 100000000000004
irb(main):004:0> i.class
=> Bignum
irb(main):005:0> i=i-100000000000000
=> 4
irb(main):006:0> i.class
=> Fixnum
  Next Page: Strings


Page 1: IntroductionPage 5: Data Structures
Page 2: StringsPage 6: Code Blocks, Iterators, and Procedure Objects
Page 3: Regular ExpressionsPage 7: Input/Output
Page 4: RangesPage 8: A Real-world Ruby Application
advertisement
Advertising Info  |   Member Services  |   Permissions  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About


JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
IBM eBook: Planning a Service Oriented Architecture
IBM eBook: Choosing the Right Architecture--What It Means for You and Your Business
Microsoft Article: Will Hyper-V Make VMware This Decade's Netscape?
Avaya Article: Using Intelligent Presence to Create Smarter Business Applications
Intel Go Parallel Article: Getting Started with TBB on Windows
Microsoft Article: 7.0, Microsoft's Lucky Version?
Avaya Article: How to Feed Data into the Avaya Event Processor
IBM Article: Developing a Software Policy for Your Organization
Microsoft Article: Managing Virtual Machines with Microsoft System Center
Intel Go Parallel Article: Intel Threading Tools and OpenMP
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
HP Video: StorageWorks EVA4400 and Oracle
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Red Gate Download: SQL Toolbelt and free High-Performance SQL Code eBook
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
Silverlight 2 App and Walkthrough: Leverage Silverlight 2 with SQL Server and XML
IBM Article: Enterprise Search--Do You Know What's Out There?
HP Demo: StorageWorks EVA4400
Microsoft Article: The Progress and Promise of Deep Zoom
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES