1

I'm trying to generate a variable name based on day of week. In my html.erb file, I have the following code:

<%= "@selected_venue.open_time_"+Time.now.strftime('%a').downcase %>

I'm trying to create a variable such as "@selected_venue.open_time_mon", "@selected_venue.open_time_tue", "@selected_venue.open_time_wed", etc. As you can see, I'm appending the day of week to the generic variable name "@selected_venue.open_time_" so I can get the value of the dynamically generated variable.

Instead of getting the value that the variable represents (which is a time such as 2:00PM), I keep getting the literal result: "@selected_venue.open_time_wed".

Is there a method that I can use to get the value of the dynamically generated variable?

3 Answers 3

3

Cleaner to move the logic to a helper, e.g.:

# in a helper
def open_time
  var = "open_time_" + Time.now.strftime('%a').downcase
  @selected_venue.send(var)
end

# in the template
<%= open_time %>

I agree with @Bubbles that it's better to have a collection like a hash or an array in open_time than having to metaprogram like this.

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

3 Comments

Mori - I ended up putting the open_time method into a helper and it worked great; however, I'm now calling the show action in my controller via AJAX and the application no longer recognizes the open_time method. Do you have any suggestions on how to access helper methods via AJAX?
@Vee, the helper method should have the same scope whether called by AJAX or not. If the AJAX uses a different controller you need to make sure that the helper is visible there as well, e.g. with the helper directive.
You're right, Mori! After modifying a good amount of code, I forgot to rename the instance variable in the helper. Again, works like a charm. Thanks!
2

This is more a general ruby thing, but sure it's possible. You can access a variable like so:

@selected_venue.send(:"open_time_#{Time.now.strftime('%a').downcase}")

That said, in a situation like this I'd first consider if I could replace the dynamic variable with a hash - if open_time could be a hash and if you could set :wed on it, I'd personally find it tidier (of course, I don't know the full situation here - if you have seven variables in the venue table for daily opening times, this solution would make sense).

2 Comments

Thanks, Bubbles. Your answer worked great but I went with Mori's answer below as it allowed me to insert some additional code in the helper. Cheers!
Fair enough; there's a lot to be said for extracting the code.
0

Note that if @selected_venue is an ActiveRecord object (and open_time_mon is an attribute of that object), you can just use it like a hash:

@selected_venue["open_time_#{Time.now.strftime('%a').downcase}"]

Otherwise, as others have said you'll need to use send:

@selected_venue.send("open_time_#{Time.now.strftime('%a').downcase}")

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.