Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7874045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:41:36+00:00 2026-06-03T02:41:36+00:00

I am working through Chris Pine’s Ruby book, and I am slightly confused why

  • 0

I am working through Chris Pine’s Ruby book, and I am slightly confused why my code doesn’t quite work.

I have a file called birthdays.txt which has around 10 lines of text which resembles:

Andy Rogers, 1987, 02, 03

etc.

My code as follows:

hash = {}

File.open('birthdays.txt', "r+").each_line do |line|
  name, date = line.chomp.split( /, */, 2 )
  hash[name] = date
end

puts 'whose birthday would you like to know?'

name = gets.chomp
puts hash[name]                                    
puts Time.local(hash[name])

My question is, why does the last line of code, Time.local(hash[name]) produce this output?:

1987-01-01 00:00:00 +0000 

instead of:

1987-02-03 00:00:00 +0000
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T02:41:38+00:00Added an answer on June 3, 2026 at 2:41 am
    line = "Andy Rogers, 1987, 02, 03\n"
    name, date = line.chomp.split( /, */, 2 ) #split (', ', 2) is less complex.
    #(Skipping the hash stuff; it's fine)
    p date #=>          "1987, 02, 03"
    # Time class can't handle one string, it wants: "1987", "02", "03"
    # so: 
    year, month, day = date.split(', ')
    p Time.local(year, month, day)
    # or do it all at once (google "ruby splat operator"):
    p Time.local(*date.split(', '))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am just working through some examples from my text book, writing the code
I am working through the Agile Web Development with Rails book but I have
I am working through a REST services book dealing with WCF. I have been
Working through Pro ASP.NET MVC book and I got the following code snippet that
I am working through the Ruby-on-Rails3 Tutorial Chapter 8. I have created a form
I am working through an MVVM tutorial, and I have the following code, written
I am working through the Ruby on Rails 3 tutorial book and typed the
I've been working through the book Scripted GUI Testing with Ruby, and I'm really
I am working through a programming book and I want to branch off of
I am working through the Ruby on Rails Tutorial by Michael Hartl and I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.