0

I am trying to create a calendar list in Ruby on Rails

I created a date range:

date_range = [['Feb 10', 'Feb 11'], ['Feb 11', 'Feb 12'], ['Feb 10', 'Feb 11', 'Feb 12']]

I ran this code:

date_range.each do |day|
  for daytime in day
    puts daytime
  end
end

The output is:

Feb 10 Feb 11 Feb 11 Feb 12 Feb 10 Feb 11 Feb 12

How can I display just:

Feb 10 Feb 11 Feb 12
4
  • 5
    Your date_range is not a valid object. Commented Feb 14, 2016 at 9:32
  • 2
    Apart from the syntax, your date_range seems to be an array of date pairs (i.e. a 2D array), whereas your output shows single dates. Could you explain that? Commented Feb 14, 2016 at 9:34
  • What are date bar and day bar? Commented Feb 14, 2016 at 9:46
  • Sorry,just date bar,and which I have done in here Commented Feb 14, 2016 at 9:49

2 Answers 2

5

To specifically get the result you're after, how about

date_range.flatten.uniq.zip
Sign up to request clarification or add additional context in comments.

6 Comments

I don't know if sort was necessary.
@sawa Yeah, in his example it wasn't. I just wanted to give something a bit more robust.
@sawa I don't know too much about Rails and its use cases. Would you recommend removing the sort?
It depends on his intention (it may already be sorted). But since it is not asked, I think it is safer not to add your interpretation.
This code can let date_range = ["Feb 10","Feb 11","Feb 12"],but it can't use for loop to print every date bar. I got an error undefined method each for #<Post:0x007fe7ef0b0d18>
|
2

Here is one more way to do this (with straight face):

date_range.reduce(:|)
#=> ["Feb 10", "Feb 11", "Feb 12"]

Comments

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.