Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 486 Vote(s) - 3.48 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What is the "right" way to iterate through an array in Ruby?

#11
I think there is no one *right* way. There are a lot of different ways to iterate, and each has its own niche.

- `each` is sufficient for many usages, since I don't often care about the indexes.
- `each_ with _index` acts like Hash#each - you get the value and the index.
- `each_index` - just the indexes. I don't use this one often. Equivalent to "length.times".
- `map` is another way to iterate, useful when you want to transform one array into another.
- `select` is the iterator to use when you want to choose a subset.
- `inject` is useful for generating sums or products, or collecting a single result.

It may seem like a lot to remember, but don't worry, you can get by without knowing all of them. But as you start to learn and use the different methods, your code will become cleaner and clearer, and you'll be on your way to Ruby mastery.

Reply

#12
Using the same method for iterating through both arrays and hashes makes sense, for example to process nested hash-and-array structures often resulting from parsers, from reading JSON files etc..

One clever way that has not yet been mentioned is how it's done in the [Ruby Facets][1] library of standard library extensions. From [here][2]:

```
class Array

# Iterate over index and value. The intention of this
# method is to provide polymorphism with Hash.
#
def each_pair #:yield:
each_with_index {|e, i| yield(i,e) }
end

end
```

There is already [`Hash#each_pair`][3], an alias of `Hash#each`. So after this patch, we also have `Array#each_pair` and can use it interchangeably to iterate through both Hashes and Arrays. This fixes the OP's observed insanity that `Array#each_with_index` has the block arguments reversed compared to `Hash#each`. Example usage:

```
my_array = ['Hello', 'World', '!']
my_array.each_pair { |key, value| pp "#{key}, #{value}" }

# result:
"0, Hello"
"1, World"
"2, !"

my_hash = { '0' => 'Hello', '1' => 'World', '2' => '!' }
my_hash.each_pair { |key, value| pp "#{key}, #{value}" }

# result:
"0, Hello"
"1, World"
"2, !"
```

[1]:

[To see links please register here]

[2]:

[To see links please register here]

[3]:

[To see links please register here]

Reply

#13
This will iterate through all the elements:

```rb
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:

```rb
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.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through