<p>In Ruby, trying to print out the individual elements of a String is giving me trouble. Instead of seeing each character, I'm seeing their ASCII values instead: </p>

<pre><code>&gt;&gt; a = "0123"
=&gt; "0123"
&gt;&gt; a[0]
=&gt; 48
</code></pre>

<p>I've looked online but can't find any way to get the original "0" back out of it. I'm a little new to Ruby to I know it has to be something simple but I just can't seem to find it.</p>

[{'answer_id': 29512, 'author': 'SCdF', 'author_id': 1666, 'author_profile': 'https://Stackoverflow.com/users/1666', 'pm_score': 3, 'selected': False, 'text': '<p>You want <code>a[0,1]</code> instead of <code>a[0]</code>.</p>\n'}, {'answer_id': 29517, 'author': 'Stuart', 'author_id': 3162, 'author_profile': 'https://Stackoverflow.com/users/3162', 'pm_score': 3, 'selected': False, 'text': '<p>I believe this is changing in Ruby 1.9 such that "asdf"[2] yields "d" rather than the character code</p>\n'}, {'answer_id': 29518, 'author': 'SCdF', 'author_id': 1666, 'author_profile': 'https://Stackoverflow.com/users/1666', 'pm_score': 1, 'selected': False, 'text': '<p><a href="https://stackoverflow.com/questions/29511/ruby-convert-integer-to-string#29513">@Chris</a>,</p>\n\n<p>That\'s just how [] and [,] are defined for the String class.</p>\n\n<p>Check out the <a href="http://ruby-doc.org/core/classes/String.html#M000786" rel="nofollow noreferrer">String API</a>.</p>\n'}, {'answer_id': 29521, 'author': 'Joseph Pecoraro', 'author_id': 792, 'author_profile': 'https://Stackoverflow.com/users/792', 'pm_score': 5, 'selected': True, 'text': '<p>Or you can convert the integer to its character value:</p>\n\n<pre><code>a[0].chr\n</code></pre>\n'}, {'answer_id': 29572, 'author': 'Ben Childs', 'author_id': 2925, 'author_profile': 'https://Stackoverflow.com/users/2925', 'pm_score': 1, 'selected': False, 'text': '<p>The [,] operator returns a string back to you, it is a substring operator, where as the [] operator returns the character which ruby treats as a number when printing it out.</p>\n'}, {'answer_id': 38710, 'author': 'Nathan Fritz', 'author_id': 4142, 'author_profile': 'https://Stackoverflow.com/users/4142', 'pm_score': 3, 'selected': False, 'text': '<p>To summarize:</p>\n\n<p>This behavior will be going away in version 1.9, in which the character itself is returned, but in previous versions, trying to reference a single character of a string by its character position will return its character value (so "ABC"[2] returns 67) </p>\n\n<p>There are a number of methods that return a range of characters from a string (see the Ruby docs on the String <a href="http://www.ruby-doc.org/core/classes/String.html#M000786" rel="nofollow noreferrer">slice method</a>) All of the following return "C":</p>\n\n<pre><code>"ABC"[2,1] \n"ABC"[2..2]\n"ABC".slice(2,1)\n</code></pre>\n\n<p>I find the range selector to be the easiest to read. Can anyone speak to whether it is less efficient?</p>\n'}, {'answer_id': 31042578, 'author': 'John La Rooy', 'author_id': 174728, 'author_profile': 'https://Stackoverflow.com/users/174728', 'pm_score': 0, 'selected': False, 'text': '<p>I think <code>each_char</code> or <code>chars</code> describes better what you want.</p>\n\n<pre><code>irb(main):001:0&gt; a = "0123"\n=&gt; "0123"\nirb(main):002:0&gt; Array(a.each_char)\n=&gt; ["0", "1", "2", "3"]\nirb(main):003:0&gt; puts Array(a.each_char)\n0\n1\n2\n3\n=&gt; nil\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/29511', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/422/']