472,133 Members | 1,443 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,133 software developers and data experts.

Displaying ASCII values

180 100+
Hello All,
How do I display ASCII values from A..Z?
I tried the following but get some compiler error:

Expand|Select|Wrap|Line Numbers
  1. def DisplayAscii()
  2.     for index in 'A'..'Z' do
  3.         puts ?index
  4.     end    
  5. end
Please help...
Oct 12 '07 #1
7 7009
improvcornartist
303 Expert 100+
The ? only looks at the next character, i, so the remaining 'ndex' causes an error. I'm not sure how to use ? to get what you want, but using a string element reference would work.

Expand|Select|Wrap|Line Numbers
  1. for index in 'A'..'Z' do
  2.   puts index[0]
  3. end
Oct 12 '07 #2
vermarajeev
180 100+
The ? only looks at the next character, i, so the remaining 'ndex' causes an error. I'm not sure how to use ? to get what you want, but using a string element reference would work.

Expand|Select|Wrap|Line Numbers
  1. for index in 'A'..'Z' do
  2.   puts index[0]
  3. end
But I need to use '?' to display the ASCII values of the characters.
Oct 16 '07 #3
improvcornartist
303 Expert 100+
Is this homework? What else are you allowed to use? Eval would work.
Oct 16 '07 #4
vermarajeev
180 100+
Is this homework? What else are you allowed to use? Eval would work.
Homework, absolutely no. Why do you think that could be a homework? I come from a C++ background and learning Ruby. The best way to learn any language is to play with it. I want to know how do I display the ASCII value of a char using the '?'. By the way how do you declare a character in Ruby?
Oct 17 '07 #5
improvcornartist
303 Expert 100+
I only thought it could possibly be homework because you said you "need to use '?'". The loop I gave you would do the same thing - it would list the ASCII values. Since you rejected it and said you needed to use '?' I thought it may be a homework requirement or something. In any case, iterating a loop like you tried doesn't work because of the way '?' is defined. However, if you use eval, you can use an index and then evaluate the expression.

Expand|Select|Wrap|Line Numbers
  1. for index in 'A'..'Z' do
  2.   puts eval("?#{index}")
  3. end
This will fill in the index, then get the ? value of that index. For defining a character, I don't think Ruby actually has a character class. So you would probably use the same method as you would for a string, char = 'A'. I'm still fairly new to Ruby myself, so I don't know if there's a better way to do characters. If so, I haven't found it.
Oct 17 '07 #6
KOTP
2
Expand|Select|Wrap|Line Numbers
  1. for index in 'A'..'Z' do
  2.   puts eval("?#{index}")
  3. end
Without using eval you can simply do this, if you want to simply puts the ascii for A - Z

Expand|Select|Wrap|Line Numbers
  1. ('A'..'Z').each do |x|
  2.   puts x[0]
  3. end
Jul 31 '08 #7
improvcornartist
303 Expert 100+
Without using eval you can simply do this, if you want to simply puts the ascii for A - Z

Expand|Select|Wrap|Line Numbers
  1. ('A'..'Z').each do |x|
  2.   puts x[0]
  3. end
You are correct, using x[0] works. That was actually my solution in post #2. But the original poster wanted to use the '?', which is why I used eval.
Jul 31 '08 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

12 posts views Thread by chunhui_true | last post: by
1 post views Thread by Kermit Piper | last post: by
12 posts views Thread by korund | last post: by
7 posts views Thread by Jeffrey Spoon | last post: by
4 posts views Thread by meendar | last post: by
5 posts views Thread by tushar.saxena | last post: by
9 posts views Thread by =?Utf-8?B?RGFu?= | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.