Trans
4/13/2005 6:13:00 PM
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
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.