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
advertisement
Rate this item | 0 users have rated this item.
Email this articleEmail this article
 
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
Microsoft Article: Will Hyper-V Make VMware This Decade's Netscape?
Microsoft Article: BitLocker Encryption on Windows Server 2008
Go Parallel Article: Intel Thread Checker, Meet 20 Million LOC
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
Avaya Article: Call Control XML - Powerful, Standards-Based Call Control
Tripwire Whitepaper: Seven Practical Steps to Mitigate Virtualization Security Risks
Internet.com eBook: The Pros and Cons of Outsourcing
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
Go Parallel Article: James Reinders on the Intel Parallel Studio Beta Program
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
Go Parallel Article: Getting Started with TBB on Windows
HP eBook: Storage Networking , Part 1
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Go Parallel Video: Intel(R) Threading Building Blocks: A New Method for Threading in C++
HP Video: Is Your Data Center Ready for a Real World Disaster?
Microsoft Partner Portal Video: Microsoft Gold Certified Partners Build Successful Practices
HP On Demand Webcast: Virtualization in Action
Go Parallel Video: Performance and Threading Tools for Game Developers
Rackspace Hosting Center: Customer Videos
Intel vPro Developer Virtual Bootcamp
HP Disaster-Proof Solutions eSeminar
HP On Demand Webcast: Discover the Benefits of Virtualization
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Microsoft Download: Silverlight 2 Software Development Kit Beta 2
30-Day Trial: SPAMfighter Exchange Module
Red Gate Download: SQL Toolbelt
Iron Speed Designer Application Generator
Microsoft Download: Silverlight 2 Beta 2 Runtime
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
IBM IT Innovation Article: Green Servers Provide a Competitive Advantage
Microsoft Article: Expression Web 2 for PHP Developers--Simplify Your PHP Applications
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES