0

I'm getting an array back from redis (trhough a controller for my projects) which I need to destructure in my product view.

Array (showing 2 results, but it can many more): ["project-5", "project-4"]

The numbers (5, 4) are my project id's (@project.id) which I need to subtract to use in a .each do function.

All that I can find for it, are destructure solutions for multiple variables. But here I only have 1 variable.

Can anyone help me out with how I can isolate the product id out of this array?

1 Answers1

0

You can use match method to get the ids with the help of regex. Assuming array consists of your projetcs with id-s 3 and 44 then a one-liner would be:

2.0.0-p353 :091 > array.map{|el| el.match(/\d+$/)[0]}
  => ["3", "44"] 

If you sure need to use .each do then

array.each do |el|
  el.match(/\d+$/)[0]
end

You can also use method gsub with regex to replace everything except the id inside string with an empty string, so only id-part of the string will remain. One-liner for this (array = ["proj3ct-3", "project-1567"]):

2.0.0-p353 :040 > array.map {|el| el.gsub(/(.*\-)[^\d]*/, "")}
  => ["3", "1567"]

.each do similarly to previous example with match.

EDIT: If you want the do-block to return the array already in formatted form then you should use bang-method from gsub like this: el.gsub!(/(.*\-)[^\d]*/, "") This will also make changes in elements. Non-bang methods only return changed values without altering them.

Just in case you don't know and would like to test/analyze your regex then this is a good place to do it- I've been using it for quite a while. Also a good place to recheck some basics is here

Andres
  • 2,099
  • 3
  • 22
  • 39
  • Thanks @Andres, that does the job! I'm just experiencing 1 small hick-up: it prints the full array as well after the destructured one. `5 1 ["project-5", "project-1"]` – Sebastian Plasschaert Oct 26 '15 at 10:32
  • I guess you have the problem with `.each do` block not with one-liner? In that case you can use the bang-method from gsub `el.gsub!(/(.*\-)[^\d]*/, "")` . In that case it will return only formatted elements inside array. Bang method will also make changes inside the element not only return the formatted form. – Andres Oct 26 '15 at 10:37
  • Then probably something else is going wrong, because even with this method i still have the same outcome: `
    <%= @project_rec.each do |el| %> <%= el.gsub!(/(.*\-)[^\d]*/, "") %>
    <% end %>
    `
    – Sebastian Plasschaert Oct 26 '15 at 10:51
  • But I'll probably be able to figure that out. Thanks so far, @Andres – Sebastian Plasschaert Oct 26 '15 at 10:53
  • Aha- you should remove `=` sign before do-block inside your view. It displays also the return-value from block. :) But I suggest making a proper method in you corresponding helper file that returns already formatted array. For ex `formatted_project_ids` or sth like that. Then you can print out only id-s by iterating over the method's return value `<%= formatted_project_ids.each do |el|` inside your view. You'll spare your view-files with excessive ruby-logic (for ex describing regex in this example a.s.o.) like that and your app is more structured* – Andres Oct 26 '15 at 11:02
  • I know what I did wrong: I made it local. After removing the `@` it only returns each value ones. – Sebastian Plasschaert Oct 26 '15 at 12:54