1

I have a helper method that returns around 10 values/variables to a view. Calling this method in the view is a mess if i need to make use of local variables and not the instance variables returned by the helper.

I also don't want to make use of multiple helper methods for all these different values as it will duplicate my code.

For eg: example_helper.rb

  def multiple_return_values
    return a,b,c,d,e,f,g,h,i,j
  end

example_view.html.erb

  <% a,b,c,d,e,f,g,h,i,j = multiple_return_values %>

OR

  <% values = multiple_return_values %>
  <% values[0] %>
  <% values[1] %>
  .
  .
  .
  <% values[9] %>

I don't want to use these above blocks of code(for example_view.html.erb). Neither i want to use instance variables coming from the helper. I want to use only local variables in views.

What is the best way to use these multiple values returned from the helper in the view using only local variables?

1
  • 2
    the other way to do this is you can return open struct instance and get the value from that by my_struct = multiple_return_values then my_struct.x my_struct.y my_struct.z... Commented Mar 5, 2018 at 6:22

1 Answer 1

1

TLDR: My preference is Solution 1.1 if only few returned values, and Solution 1.2 if loooooads of returned values

Solution 1.1

def multiple_return_values
  # ...
  return {
    a: a,
    b: b,
    c: c,
    d: d,
    e: e,
    f: f,
    g: g,
    h: h,
    i: i,
    j: j
  }
end

Solution 1.2

OR if you prefer non-repetition of "keys" and "values" from Solution 1.1, then:

def multiple_return_values
  # ...
  local_variables_to_be_returned = [:a, :b, :c, :d, :e, :f, :g, :h, :i, :j]
  return Hash[local_variables_to_be_returned.map{|v| [v, binding.local_variable_get(v)]}]
end

Usage

Then in your views,

<% returned_values = multiple_returned_values -%>

<%= returned_values[:a] %>
<%= returned_values[:b] %>
<%= returned_values[:g] %>

Solution 2

See @HankPhung's comment: to return an OpenStruct instance instead of a Hash, then you'll just simply use OpenStruct.new(HASH):

def multiple_return_values
  # ...
  local_variables_to_be_returned = [:a, :b, :c, :d, :e, :f, :g, :h, :i, :j]
  return OpenStruct.new(
    Hash[local_variables_to_be_returned.map{|v| [v, binding.local_variable_get(v)]}]
  )
end

Usage

Then in your views,

<% returned_values = multiple_return_values -%>

<%= returned_values.a %>
<%= returned_values.b %>
<%= returned_values.g %>

Solution 3

NOT-recommended; only very case-specific usage

You can directly pass in the binding as the return value

def multiple_return_values_binding
  # ...
  return binding
end

Usage

Then in your views,

<% somebinding = multiple_return_values_binding -%>

<%= somebinding.local_variable_get(:a) %>
<%= somebinding.local_variable_get(:b) %>
<%= somebinding.local_variable_get(:g) %>
Sign up to request clarification or add additional context in comments.

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.