iswg iswg - 1 year ago 98
Ruby Question

Ruby: file output blank

So I'm trying to build a program which tells me how many days are left till someone's birthday. I have a text file that I'm drawing data from. The problem is the save method is not working for some reason, and nothing is being printed to the output file. Thank you so much in advance!

require 'date'

Person =, :bday)

module Family

Member =

File.readlines("bday_info.txt").each do |line|
name, bday = line.split(',')
person =, bday)
Member[name] = bday

def self.next_bday(name)
birthday = Date.parse(Family::Member[name])
this_year =, birthday.month,
next_year = + 1, birthday.month,
if this_year >
puts "\n#{(this_year -} days to #{name}'s birthday"
puts "\n#{(next_year -} days to #{name}'s birthday"

def"days_left.txt", "w") do |file|
file.puts "#{next_bday(name)}"


loop do
puts "\nName:"
response = gets.chomp.capitalize
if response.downcase == "quit"
elsif Family::Member.has_key?("#{response}") == false
puts "\nYou ain't on the list"
Family.next_bday(response) #WHY IS THIS LINE NOT WORKING???

Answer Source

Your Family.next_bday does not return anything (to be more precise - it's returning nil that last puts returns), thus nothing is written to the file.

Other notes(not directly related):

  1. No reason in calling Family.next_bday(response) twice, just save return value from the first call
  2. Family::Member constant looks more like a variable, more clean design will be to make the whole thing a class that takes input file name, reads data in initializer and then stores into a instance variable
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download