You are previewing The Ruby Programming Language.

The Ruby Programming Language

Cover of The Ruby Programming Language by Yukihiro Matsumoto... Published by O'Reilly Media, Inc.
  1. The Ruby Programming Language
    1. SPECIAL OFFER: Upgrade this ebook with O’Reilly
    2. A Note Regarding Supplemental Files
    3. Preface
      1. Acknowledgments
      2. Conventions Used in This Book
      3. Using Code Examples
      4. How to Contact Us
      5. Safari® Enabled
    4. 1. Introduction
      1. A Tour of Ruby
      2. Try Ruby
      3. About This Book
      4. A Sudoku Solver in Ruby
    5. 2. The Structure and Execution of Ruby Programs
      1. Lexical Structure
      2. Syntactic Structure
      3. File Structure
      4. Program Encoding
      5. Program Execution
    6. 3. Datatypes and Objects
      1. Numbers
      2. Text
      3. Arrays
      4. Hashes
      5. Ranges
      6. Symbols
      7. True, False, and Nil
      8. Objects
    7. 4. Expressions and Operators
      1. Literals and Keyword Literals
      2. Variable References
      3. Constant References
      4. Method Invocations
      5. Assignments
      6. Operators
    8. 5. Statements and Control Structures
      1. Conditionals
      2. Loops
      3. Iterators and Enumerable Objects
      4. Blocks
      5. Altering Control Flow
      6. Exceptions and Exception Handling
      7. BEGIN and END
      8. Threads, Fibers, and Continuations
    9. 6. Methods, Procs, Lambdas, and Closures
      1. Defining Simple Methods
      2. Method Names
      3. Methods and Parentheses
      4. Method Arguments
      5. Procs and Lambdas
      6. Closures
      7. Method Objects
      8. Functional Programming
    10. 7. Classes and Modules
      1. Defining a Simple Class
      2. Method Visibility: Public, Protected, Private
      3. Subclassing and Inheritance
      4. Object Creation and Initialization
      5. Modules
      6. Loading and Requiring Modules
      7. Singleton Methods and the Eigenclass
      8. Method Lookup
      9. Constant Lookup
    11. 8. Reflection and Metaprogramming
      1. Types, Classes, and Modules
      2. Evaluating Strings and Blocks
      3. Variables and Constants
      4. Methods
      5. Hooks
      6. Tracing
      7. ObjectSpace and GC
      8. Custom Control Structures
      9. Missing Methods and Missing Constants
      10. Dynamically Creating Methods
      11. Alias Chaining
      12. Domain-Specific Languages
    12. 9. The Ruby Platform
      1. Strings
      2. Regular Expressions
      3. Numbers and Math
      4. Dates and Times
      5. Collections
      6. Files and Directories
      7. Input/Output
      8. Networking
      9. Threads and Concurrency
    13. 10. The Ruby Environment
      1. Invoking the Ruby Interpreter
      2. The Top-Level Environment
      3. Practical Extraction and Reporting Shortcuts
      4. Calling the OS
      5. Security
    14. Index
    15. About the Authors
    16. Colophon
    17. SPECIAL OFFER: Upgrade this ebook with O’Reilly
O'Reilly logo

Dates and Times

The Time class represents dates and times. It is a thin layer over the system date and time functionality provided by the operating system. On some platforms, therefore, this class may be unable to represent dates before 1970 or after 2038. The Date and DateTime classes in the standard date library are not constrained in this way, but are not demonstrated here:

# Creating Time objects Time.now # Returns a time object that represents the current time Time.new # A synonym for Time.now Time.local(2007, 7, 8) # July 8, 2007 Time.local(2007, 7, 8, 9, 10) # July 8, 2007, 09:10am, local time Time.utc(2007, 7, 8, 9, 10) # July 8, 2007, 09:10 UTC Time.gm(2007, 7, 8, 9, 10, 11) # July 8, 2007, 09:10:11 GMT (same as UTC) # One microsecond before the new millennium began in London # We'll use this Time object in many examples below. t = Time.utc(2000, 12, 31, 23, 59, 59, 999999) # Components of a Time t.year # => 2000 t.month # => 12: December t.day # => 31 t.wday # => 0: day of week: 0 is Sunday t.yday # => 366: day of year: 2000 was a leap year t.hour # => 23: 24-hour clock t.min # => 59 t.sec # => 59 t.usec # => 999999: microseconds, not milliseconds t.zone # => "UTC": timezone name # Get all components in an array that holds # [sec,min,hour,day,month,year,wday,yday,isdst,zone] # Note that we lose microseconds values = t.to_a # => [59, 59, 23, 31, 12, 2000, 0, 366, false, "UTC"] # Arrays of this form can be passed to Time.local and Time.utc values[5] += 1 # Increment the year ...

The best content for your career. Discover unlimited learning on demand for around $1/day.