I am trying to iterate through an array in Ruby. I using eachmethod but getting the following error message: NoMethodError: undefined method ``each' for "1, 2":String.
I am using jruby-1.6.7. Not sure if that is the problem.
Here is the code:
#!/usr/bin/ruby
puts "Select one of these.\n1. ABC\n2. DEF\n3. GHI\n"
schemas = gets.chomp
len = schemas.size
puts schemas.split(",")
puts schemas[0..len]
schemas.each {|x| puts x}
I need some guidance with iterating through a simple array in Ruby?
Thanks.
schemas.split(",")butschemasnot changes. You can doschemas = schemas.split(",")then useschemas.each {}schemas = schemas.split(",")and that worked. Thank you for the clear explanation.lenhas the size of string which should be the size of the array. solen = schemas.split(",").sizeshould be what you must have to iterate through the array of the specified length.