Newbie question re: variable assignment

This is it... Many thanks.

1.upto(9) { |x| puts "#{x}"[0] }

I was missing the literal form v. method info.

Just so I know -- Where would I look to find this info? Rdoc? ri?

Thanks again.

SM

...It's all getting a little bit clearer...

路路路

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

Hi--
Dont; worry this is a low bite board :slight_smile:

Why doesn't this work?

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

Well, its cuz you're trying to do string interpolation directly into
ruby code, which can't be done. The interpolation (i.e. #{x}) only works
in strings. Secondly, the ? is a literal form and not a method, so you
can;t pass it arguments. To get the ascii value of a string use str[n]
where n is the index of the character you wish to convert, in this case
0.

  1.upto(9) do |x|
        puts "#{x}"[0]
  end

BTW, I believe this will change in future verions of Ruby since Ruby
will be gaining more advanced encoding. So instead there will be a
specific method, like String#ascii(n), or something, to do this.
[Correct me if I am misinformed all.] But for now the above is the way.

T.

------------------------------------------------------------------------------------------
Equinox Converged Solutions
Tel: +44 (0)1252 405 600
http://www.equinoxsolutions.com
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.
-------------------------------------------------------------------------------------------

This is it... Many thanks.

1.upto(9) { |x| puts "#{x}"[0] }

I was missing the literal form v. method info.

Just so I know -- Where would I look to find this info? Rdoc? ri?

Pickaxe ("Programming Ruby" by Dave Thomas and Andy Hunt).

路路路

Simon.Mullis@equinoxsolutions.com wrote:

Thanks again.

SM

...It's all getting a little bit clearer...

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

Hi--
Dont; worry this is a low bite board :slight_smile:

Why doesn't this work?

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

Well, its cuz you're trying to do string interpolation directly into
ruby code, which can't be done. The interpolation (i.e. #{x}) only works
in strings. Secondly, the ? is a literal form and not a method, so you
can;t pass it arguments. To get the ascii value of a string use str[n]
where n is the index of the character you wish to convert, in this case
0.

  1.upto(9) do |x|
        puts "#{x}"[0]
  end

BTW, I believe this will change in future verions of Ruby since Ruby
will be gaining more advanced encoding. So instead there will be a
specific method, like String#ascii(n), or something, to do this.
[Correct me if I am misinformed all.] But for now the above is the way.

T.

------------------------------------------------------------------------------------------
Equinox Converged Solutions
Tel: +44 (0)1252 405 600
http://www.equinoxsolutions.com
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.
-------------------------------------------------------------------------------------------

Just to show one more way to do it (probably not the best depending on
your usage), the following works:

聽聽"0123456789".each_byte { |c| puts c }

Don't want to type "0123456789"? Try:

聽聽(0..9).to_a.join.each_byte { |c| puts c }

Like I said, roundabout and not very efficient. But, nonetheless,
another "valid" approach :slight_smile:

Jacob Fugal