Connecting Tech Pros Worldwide Forums | Help | Site Map

Displaying ASCII values

Familiar Sight
 
Join Date: Aug 2006
Posts: 180
#1: Oct 12 '07
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...

Expert
 
Join Date: May 2007
Posts: 213
#2: Oct 12 '07

re: Displaying ASCII values


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
Familiar Sight
 
Join Date: Aug 2006
Posts: 180
#3: Oct 16 '07

re: Displaying ASCII values


Quote:

Originally Posted by improvcornartist

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.
Expert
 
Join Date: May 2007
Posts: 213
#4: Oct 16 '07

re: Displaying ASCII values


Is this homework? What else are you allowed to use? Eval would work.
Familiar Sight
 
Join Date: Aug 2006
Posts: 180
#5: Oct 17 '07

re: Displaying ASCII values


Quote:

Originally Posted by improvcornartist

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?
Expert
 
Join Date: May 2007
Posts: 213
#6: Oct 17 '07

re: Displaying ASCII values


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.
Newbie
 
Join Date: Jul 2008
Posts: 2
#7: Jul 31 '08

re: Displaying ASCII values


Quote:

Originally Posted by improvcornartist

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
Expert
 
Join Date: May 2007
Posts: 213
#8: Jul 31 '08

re: Displaying ASCII values


Quote:

Originally Posted by KOTP

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.
Reply