1

Given this ERB code

<% @client.sessionables.ordered.by_program_completion.reverse_each do | program_name_and_completion, sessionables | %>
  <% program_name, program_completion_date = program_name_and_completion %>

  <% # Render stuff... %>
<% end %>

I would like to get rid of the second line, where I use the multiple variable assignment to extract program_name and program_completion_date from the program_name_and_completion array. One would assume it could be done directly in the block assignment, e.g.

sessionables.by_program_completion.each do | [program_name, program_completion_date], sessionables |

but the above snippet doesn't work, so my questions are:

  1. Is this at all possible with Ruby?
  2. If so, what's the correct syntax?

1 Answer 1

2

You can extract the arguments inline as follows:

<% @client.sessionables.ordered.by_program_completion.reverse_each do |(program_name, program_completion_date), sessionables| %>

Or to write the same thing using less verbose syntax (so it's easier to see what's going on!!):

[[[1, 2], 3]].each { |(a, b), c| ... }

Inside the block, we get: a == 1, b == 2, c == 3.

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.