I am curious as to the general concensus and reaction to me asking a
question that is most probably almost embarrassing in it's stupidity.
Shall we find out?
Let's say I wanted to convert a bunch of numbers to their ascii values.
Why doesn't this work?
1.upto(9) do |x|
聽聽puts ?#{x}
end
I am in my first few hours of Rubydom so please - pity. Not ridicule!
Thanks in advance...
SM
路路路
------------------------------------------------------------------------------------------
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.
-------------------------------------------------------------------------------------------
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
路路路
On 4/13/05, Simon.Mullis@equinoxsolutions.com <Simon.Mullis@equinoxsolutions.com> wrote:
This doesn't work because "puts ?#{x}" isn't a valid statement. If you change this line to "puts x.chr" it should work. I don't know what you wanted to do with the "?#{x}", so I can't really comment on that...
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.
"Trans" <transfire@gmail.com> schrieb im Newsbeitrag news:1113415954.205046.176340@z14g2000cwz.googlegroups.com...
Hi--
Dont; worry this is a low bite board
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
or
1.upto(9) do |x|
puts x.to_s[0]
end
or (with some knowledge of ASCII characterset)
1.upto(9) do |x|
puts ?0 + x
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.
Yeah probably. But for the time being your solution works.