Martin DeMello
10/30/2007 6:51:00 PM
On 10/30/07, Hiato Xaero <hiato3@gmail.com> wrote:
>
> 1st : How one would extract the ASCII value from an Object variable, for
> example
> @l = @txt[k,1]
> @n = ?@l
@n = @l[0]
or even
@n = @txt[k]
This is a quirk in ruby (upto 1.8) where a string is treated as an
array of bytes, and indexing with a single integer returns the ascii
value of the byte in that position (to get the first character, you'd
use @l[0,1]. It is going away in ruby 1.9 - I'm not sure how to get an
ascii value there.
> 2nd : How does one tell Ruby to give a variable (integer) a particular
> number from an array, for example
> @n = @@da[p,@n]
> where
> @@da = [[]]
> is the equivilent of the Delphi way (which works with puts in Ruby) but it
> automatically makes @n an array instead of just that single value...
If you want to define a 2d array, you need to use an array of arrays:
# if you know the dimensions already
@@da = Array.new(i) { Array.new(j) }
# if you want to do it dynamically, e.g. insert a number at (3, 5)
@@da = []
@@da[3] ||= []
@@da[3][5] = @n
a ||= b is a common ruby idiom for "set a to b only if it isn't
already defined" - it expands to a = a || b.
martin