1

I am trying to build an array of hashes (I THINK that's the way I phrase it) with a helper method so that I can use it in my view. I am getting the 2 values from columns @other_events.time_start and @other_events.time_end.

helper.rb

 def taken_times()
     @taken_times = []
    @other_events.each do |e|
    @taken_times << { e.time_start.strftime("%l:%M %P") => e.time_end.strftime("%l:%M %P")}
    end
    @taken_times
 end

What I am trying to have is an array of hashes like this:

['10:00am', '10:15am'],
['1:00pm', '2:15pm'],
['5:00pm', '5:15pm'],

which is essentially
['e.time_start', 'e.time_end'],

3
  • 2
    what you're trying to have a simple array. An array of hashes looks [{time_start: '10:00am', time_end: '10:15am'},{time_start: '1:00pm', time_end: '2:15pm'}] Commented Mar 24, 2015 at 15:06
  • 1
    Your example output is not an Array of Hashes, it's an Array of Arrays. You've basically got it, though... what was the problem you were having? Commented Mar 24, 2015 at 15:06
  • It's not even an array of arrays - it's three arrays with commas between them. If it was an array of arrays it would be enclosed in another pair of square brackets. Commented Mar 24, 2015 at 15:12

2 Answers 2

2

I think you should refactor your method to this:

def taken_times(other_events)
  other_events.map { |event| [event.time_start, event.time_end] }
end
  • The helper method is not setting a global variable @taken_times anymore but you can easily call @taken_times = taken_times(other_events).
  • The helper method is using it's argument other_events and not on the global variable @other_events which could be nil in certain views.
  • The helper method returns an array of arrays, not an array of hashes. It is a two-dimensionnal array ("width" of 2, length of x where 0 ≤ x < +infinity).
  • The helper method returns an array of arrays containing DateTime objects, not String. You can easily manipulate the DateTime objects in order to format it in the way you want. "Why not directly transform the DateTime into nice-formatted strings?" you would ask, I would answer with "because you can do that in the view, at the last moment, and maybe someday you will want to do some calculation between the time_start and the time_end before rendering it.

Then in your view:

taken_times(@your_events).each do |taken_time|
  "starts at: #{taken_time.first.strftime("%l:%M %P")}"
  "ends at: #{taken_time.last.strftime("%l:%M %P")}"
end
Sign up to request clarification or add additional context in comments.

2 Comments

is this "taken_times(@your_events).each" meant to be this "taken_times(@other_events).each" or am I to create @your_events? Having trouble getting it to work in the view and trying to debug.
I have used the variable @your_events to show you to replace this variable to your variable representing the events you want to get the start & end time. If in your views you want to get the times of the @other_events, then yes pass this variable to taken_times method.
1

You are asking for an array of hashes ([{}, {}, {}, ...]):

  Array: []
  Hash: {}

But you are expecting an array of array ([[], [], [] ...])

You should do something like this:

def taken_times()
    @taken_times = []
    @other_events.each do |e|
    @taken_times << [e.time_start.strftime("%l:%M %P"), e.time_end.strftime("%l:%M %P")]
    end
    @taken_times
end

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.