This will iterate through all the elements:
array = [1, 2, 3, 4, 5, 6] array.each { |x| puts x }
Prints:
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}" }
Prints:
A => 0 B => 1 C => 2
I’m not quite sure from your question which one you are looking for.
Related Posts:
- What does %w(array) mean?
- Array to Hash Ruby
- Ruby: undefined method `[]’ for nil:NilClass when trying to get Enumerator on an Array of Hashes
- How to map with index in Ruby?
- How to sum array of numbers in Ruby?
- Ruby array to string conversion
- How to check if a value exists in an array in Ruby
- Difference between map and collect in Ruby?
- Creating and iterating a 2d array in Ruby
- What is the best way to convert an array to a hash in Ruby
- How can I delete one element from an array by value
- For-each over an array in JavaScript
- Loop through an array in JavaScript
- Loop through an array in JavaScript
- Get underlined text with Markdown
- What is the meaning of *nix?
- Get underlined text with Markdown
- What does the “map” method do in Ruby?
- Loop through an array php
- How to write a switch statement in Ruby
- Rails 5 ActionController::InvalidAuthenticityToken error
- what does ? ? mean in ruby
- Why is using “for…in” for array iteration a bad idea?
- How to update Ruby Version 2.0.0 to the latest version in Mac OSX Yosemite?
- Correct way of looping through C++ arrays
- How to use “gets” and “gets.chomp” in Ruby
- Getting a substring in Ruby by x number of chars
- How to check whether a string contains a substring in Ruby
- Fill array with random numbers within a specified range (C++)
- Ruby: How to install a specific version of a ruby gem?
- ruby operator “=~” [duplicate]
- What does << mean in Ruby?
- What is attr_accessor in Ruby?
- ActionController::UnknownFormat
- What does ||= (or-equals) mean in Ruby?
- How to read lines of a file in Ruby
- Bubble sort algorithm in MIPS
- extended initializer lists only available with
- Bank Account Java Program
- Ruby equivalent for Python’s “try”?
- What does “+=” (plus equals) mean?
- Rails 4 RoutingError: No Route Matches
- undefined method (NoMethodError) ruby
- Multi-Line Comments in Ruby?
- How to uninstall ruby installed by ruby-install
- PG::ConnectionBad – could not connect to server: Connection refused
- Rails syntax error : unexpected keyword_ensure, expecting end-of-input
- Removing all installed Gems and starting over
- Ruby `send` vs `call` method
- What is the meaning of “do | |” in Ruby?
- RVM is not a function, selecting rubies with ‘rvm use …’ will not work
- How to delete specific characters from a string in Ruby?
- Ruby: kind_of? vs. instance_of? vs. is_a?
- Rails: I installed Ruby, now “bundle install” doesn’t work
- How to do a newline in output
- Ruby: What is the easiest way to remove the first element from an array?
- Ruby String to Date Conversion
- p vs puts in Ruby
- Determining type of an object in ruby
- Create a two dimensional string array anArray[2][2]
- Fizz Buzz in Ruby for dummies
- How to Uninstall RVM? [duplicate]
- How to iterate through table in Lua?
- What is the difference between a Process’ pid, ppid, uid, euid, gid and egid?
- Equivalent of “continue” in Ruby
- PHP array printing using a loop
- What does the Ruby method ‘to_sym’ do?
- Array to Hash Ruby
- Ruby 2.0.0p0 IRB warning: “DL is deprecated, please use Fiddle”
- Printing from a declared array in MIPS
- Getting error: dyld: Symbol not found: _clock_gettime
- Uninstall old versions of Ruby gems
- Uploading a file in Rails
- What is java interface equivalent in Ruby?
- Sort hash by key, return hash in Ruby
- Gem installation error: You have to install development tools first
- How to understand strptime vs. strftime
- How to break out from a ruby block?
- ERROR: While executing gem … (Gem::FilePermissionError)
- No such file or directory @ rb_sysopen ruby
- How to add to an existing hash in Ruby
- Ruby ‘require’ error: cannot load such file
- How to convert a string to lower or upper case in Ruby
- What is the canonical way to trim a string in Ruby without creating a new string?
- Difference between $stdout and STDOUT in Ruby
- No implicit conversion of String into Integer (TypeError)?
- Multi-Line Comments in Ruby?
- char *array and char array[]
- How to print elements in a vector c++
- How do you check if a variable is an array in JavaScript?
- YAML equivalent of array of objects in JSON
- Javascript Uncaught TypeError: Cannot read property ‘0’ of undefined
- What does ** do in C language?
- Are arrays passed by value or passed by reference in Java?
- warning: constant ::Fixnum is deprecated When generating new model
- java.lang.ArrayIndexOutOfBoundsException: 4 Error
- Printing array elements with a for loop
- How to get the first element of an array?
- C char array initialization
- C: warning: excess elements in array initializer; near initialization for ‘xxx’ ; expects ‘char *’, but has type ‘int’