O'Reilly logo

Ruby by Example by Kevin C. Baird

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

#39 Numbers by Name (to_lang.rb)

In previous scripts, notably #16 Adding Commas to Numbers (commify.rb) on page 75 and #17 Roman Numerals (roman_numeral.rb) on page 81, we talked about how numbers can be represented in a variety of ways. Both of those scripts showed meaningful ways of representing Integers as Strings, other than the handy but trivially different to_s method. This script, to_lang.rb, extends that discussion by representing Integers as Strings consisting of how those numbers are spoken in two real-world languages: English and Spanish.

The Code

This code is broken into three separate files, for reasons that I will make clear in How It Works on page 198.

representable_in_english.rb

  =begin rdoc
  This is intended for use with to_lang.rb ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required