0

I have a problem that I don't know how to resolve:

I have this code that pretty much asks user for date. The problem that I have is that I can't make the array to take var limit as an element whenever user puts a new date. The array resets itself whenever the method 'new_date' ends.

Here is the code:

def new_date 
  puts "Date please: "
  date1 = $stdin.gets.chomp
  d, m, y = date1.split('-').map{|x| x.to_i}
  limit = day_of_week(d, m, y)
  puts days_of_the_week[limit]
  $various_dates = []
  $various_dates.push(limit)
end
3
  • Yes it is ruby but can you please fix your indentation? Commented Oct 10, 2015 at 4:34
  • Why reinitialize $various_dates at the 7th last line? Commented Oct 10, 2015 at 4:37
  • Ok, I edit it. I tried '$various_dates' in several lines just for testing and none of them worked. They are gone now. Commented Oct 10, 2015 at 4:39

1 Answer 1

1

You should not reset your array of dates in:

$various_dates = []

Instead, do something like:

$various_dates ||= []

What this does, is it gets the global variable $various_dates, but if it hasn't been initialized (which means that it is nil), it will be set to an empty array.

Sign up to request clarification or add additional context in comments.

1 Comment

This is it! Thanks so much! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.