iterating over each character of a String in ruby 1.8.6 (each_char)

RubyStringIterator

Ruby Problem Overview


I am new to ruby and currently trying to operate on each character separately from a base String in ruby. I am using ruby 1.8.6 and would like to do something like:

"ABCDEFG".each_char do |i|
  puts i
end

This produces a undefined method `each_char' error.

I was expecting to see a vertical output of:

A
B
C
D
..etc

Is the each_char method defined only for 1.9? I tried using the plain each method, but the block simply ouputs the entire string in one line. The only way I figure how to do this, which is rather inconvenient is to create an array of characters from the begining:

['A','B','C','D','...'].each do|i|
  puts i
end

This outputs the desired:

A
B
C
..etc

Is there perhaps a way to achive this output using an unmodified string to begin with?

I think the Java equivalent is:

for (int i = 0; i < aString.length(); i++){
  char currentChar = aString.charAt(i);
  System.out.println(currentChar);
}

Ruby Solutions


Solution 1 - Ruby

I have the same problem. I usually resort to String#split:

"ABCDEFG".split("").each do |i|
  puts i
end

I guess you could also implement it yourself like this:

class String
  def each_char
    self.split("").each { |i| yield i }
  end
end

Edit: yet another alternative is String#each_byte, available in Ruby 1.8.6, which returns the ASCII value of each char in an ASCII string:

"ABCDEFG".each_byte do |i|
  puts i.chr # Fixnum#chr converts any number to the ASCII char it represents
end

Solution 2 - Ruby

Extending la_f0ka's comment, esp. if you also need the index position in your code, you should be able to do

s = 'ABCDEFG'
for pos in 0...s.length
    puts s[pos].chr
end

The .chr is important as Ruby < 1.9 returns the code of the character at that position instead of a substring of one character at that position.

Solution 3 - Ruby

"ABCDEFG".chars.each do |char|
  puts char
end

also

"ABCDEFG".each_char {|char| p char}

Ruby version >2.5.1

Solution 4 - Ruby

there is really a problem in 1.8.6. and it's ok after this edition

in 1.8.6,you can add this:

requre 'jcode'

Solution 5 - Ruby

But now you can do much more:

a = "cruel world"

a.scan(/\w+/)        #=> ["cruel", "world"]

a.scan(/.../)        #=> ["cru", "el ", "wor"]

a.scan(/(...)/)      #=> [["cru"], ["el "], ["wor"]]

a.scan(/(..)(..)/)   #=> [["cr", "ue"], ["l ", "wo"]]

Solution 6 - Ruby

Returns an array of characters in str. This is a shorthand for str.each_char.to_a. If a block is given, which is a deprecated form, works the same as each_char.

from ruby-doc.org

also now you can do string.chars

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondenchrView Question on Stackoverflow
Solution 1 - RubyPaige RutenView Answer on Stackoverflow
Solution 2 - RubysschuberthView Answer on Stackoverflow
Solution 3 - RubyJulianView Answer on Stackoverflow
Solution 4 - Rubykaka2008View Answer on Stackoverflow
Solution 5 - RubyAbdul BaigView Answer on Stackoverflow
Solution 6 - RubyGeorgiiView Answer on Stackoverflow