Displaying ASCII values

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vermarajeev
    New Member
    • Aug 2006
    • 180

    Displaying ASCII values

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

    Code:
    def DisplayAscii()
    	for index in 'A'..'Z' do
    		puts ?index
    	end	
    end
    Please help...
  • improvcornartist
    Recognized Expert Contributor
    • May 2007
    • 303

    #2
    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.

    [CODE=ruby]for index in 'A'..'Z' do
    puts index[0]
    end[/CODE]

    Comment

    • vermarajeev
      New Member
      • Aug 2006
      • 180

      #3
      Originally posted by improvcornartis t
      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.

      [CODE=ruby]for index in 'A'..'Z' do
      puts index[0]
      end[/CODE]
      But I need to use '?' to display the ASCII values of the characters.

      Comment

      • improvcornartist
        Recognized Expert Contributor
        • May 2007
        • 303

        #4
        Is this homework? What else are you allowed to use? Eval would work.

        Comment

        • vermarajeev
          New Member
          • Aug 2006
          • 180

          #5
          Originally posted by improvcornartis t
          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?

          Comment

          • improvcornartist
            Recognized Expert Contributor
            • May 2007
            • 303

            #6
            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.

            [CODE=ruby]for index in 'A'..'Z' do
            puts eval("?#{index} ")
            end[/CODE]
            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.

            Comment

            • KOTP
              New Member
              • Jul 2008
              • 2

              #7
              Originally posted by improvcornartis t
              [CODE=ruby]for index in 'A'..'Z' do
              puts eval("?#{index} ")
              end[/CODE]
              Without using eval you can simply do this, if you want to simply puts the ascii for A - Z

              [CODE=ruby]('A'..'Z').each do |x|
              puts x[0]
              end[/CODE]

              Comment

              • improvcornartist
                Recognized Expert Contributor
                • May 2007
                • 303

                #8
                Originally posted by KOTP
                Without using eval you can simply do this, if you want to simply puts the ascii for A - Z

                [CODE=ruby]('A'..'Z').each do |x|
                puts x[0]
                end[/CODE]
                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.

                Comment

                Working...