[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: newbie question re: variable assignment

Simon.Mullis

4/13/2005 6:06:00 PM

Thanks Patrick.

If I want to convert in the other direction - i.e get the ASCII value
for the characters (1..9)...

1.upto(9) { |x| puts ?x }

Does not work - it give me the ASCII code for x (which is 120) nine
times

How can I force ruby to treat the "x" as a variable and not as "x". ($x
from perlland)

Thanks

SM


-----Original Message-----
From: Patrick Hurley [mailto:phurley@gmail.com]
Sent: 13 April 2005 19:00
To: ruby-talk ML
Subject: Re: newbie question re: variable assignment


On 4/13/05, Simon.Mullis@equinoxsolutions.com
<Simon.Mullis@equinoxsolutions.com> wrote:
> Shall we find out?

We want to be the friendly news group, so starting out questions are
welcome :-)

> Why doesn't this work?
>
> 1.upto(9) do |x|
> puts ?#{x}
> end

The #{ } escape is for with quoted strings. You could do something
similar to what you write using and eval; however, I think you would be
better off with:

65.upto(75) { |x| puts x.chr }

Patrick


------------------------------------------------------------------------------------------
Equinox Converged Solutions
Tel: +44 (0)1252 405 600
http://www.equinoxsol...
Equinox Converged Solutions is a trading name of Synetrix Holdings Limited.

IMPORTANT NOTICE:
This message is intended solely for the use of the Individual or organisation to whom it is addressed. It may contain privileged or confidential information. If you have received this message in error, please notify the originator immediately.
If you are not the intended recipient, you should not use, copy, alter, or disclose the contents of this message. All information or opinions expressed in this message and/or any attachments are those of the author and are not necessarily those of Synetrix Holdings Limited.
Synetrix Holdings Limited accepts no responsibility for loss or damage arising from its use, including damage from virus.
-------------------------------------------------------------------------------------------



1 Answer

Martin DeMello

4/14/2005 5:25:00 AM

0

Simon.Mullis@equinoxsolutions.com wrote:
>
> If I want to convert in the other direction - i.e get the ASCII value
> for the characters (1..9)...
>
> 1.upto(9) { |x| puts ?x }
>
> Does not work - it give me the ASCII code for x (which is 120) nine
> times
>
> How can I force ruby to treat the "x" as a variable and not as "x". ($x
> from perlland)

Just to clarify things, the ?<char> notation is a literal value, not a
function call of any sort. It's analogous to constructs like 0xFF
(literal hex number) or for that matter 1.45 (literal floating point
value).

martin