Ruby/Rails - .Each Iterator Is Printing Entire Array at the End of the Loop

Ruby / Rails - .each Iterator is printing entire array at the end of the loop


# Change this line with an =:
<%= @categories.each do |c| %>
# ...to this:
<% @categories.each do |c| %>

You only want the side effects on the block of the #each method, you don't want interpolation of the returned value.

Iterating through array gives full array of items at the end

Replace this line

<%= @portfolio_item.technologies.each do |technology| %>

with this

<% @portfolio_item.technologies.each do |technology| %>

<%= %> will evaluate the expression and returns the array.

Why is this rails view spitting out a raw array at the end of an .each do loop?

Try removing the equals sign in <%= @post.comments.each do |comment| %>. The equals is only necessary if the method itself outputs something. In this case you're just using it to iterate a collection.

What is the right way to iterate through an array in Ruby?

This will iterate through all the elements:

array = [1, 2, 3, 4, 5, 6]
array.each { |x| puts x }

# Output:

1
2
3
4
5
6

This will iterate through all the elements giving you the value and the index:

array = ["A", "B", "C"]
array.each_with_index {|val, index| puts "#{val} => #{index}" }

# Output:

A => 0
B => 1
C => 2

I'm not quite sure from your question which one you are looking for.

Iterating over array without returning the array

Why not create a method for your desired behavior?

def print_each_value(values, format)
values.each do |value|
printf(format, value[0], value[1], value[2], value[3], value[4], value[5], value[6])
end
nil # Here you set what you would like to return to the console.
end

print_each_value(@values, @format)

Edit: Removed annotative variable.

Ruby's each methods not iterating over all items in an array?

I suspect this is happening because you're iterating over the elements of the list while modifying the list.

Try the following:

a = [1,2,3,4]
until a.empty? do
puts "Removing #{a.last}"
a.pop
end

escaping the .each { } iteration early in Ruby

While the break solution works, I think a more functional approach really suits this problem. You want to take the first 10 elements and print them so try

items.take(10).each { |i| puts i.to_s }

Rails ERB iterate over array

First of all this method is so inefficient, you are doing n-queries, to find each record of type Type instead convert those into an array of types by using a single query in the controller, assume that that array is in type_ids

# controller
@channels = Channel.includes(:types) # avoiding n+1 queries

# view
<% @channels.each do |channel| %>
# some channel info output
<% channel.types.each do |type| %>
<%= type.name %>
<% end %> # types loop
<% end %> # channel loop

As @Almaron mentioned, you could render a partial for more simplification, if you have a partial called _type.html.erb you can call render directly

# view
<%= render channel.types %>

Rails will do all the iterating and rendering.

array.each only returns the first value in Ruby


def is_rw_test(mount_info)
mount_info.each do |pt|
Dir.exist? pt.split[0] || @crit_pt_test << "#{ pt.split[0] }"
# Etc
return @crit_pt_test # ohai
end
end

You have a return, so it returns, and no more iteration via each.

If you want to collect values, then do something closer to:

def is_rw_test(mount_info)
mount_info.collect do |pt|
Dir.exist? pt.split[0] || @crit_pt_test << "#{ pt.split[0] }"
# Etc
@crit_pt_test
end
end

But if that's the case then an instance variable doesn't make any sense.

You need to decide what you actually want to do.



Related Topics



Leave a reply



Submit