Array and ASCII Help needed

Hello there,

I have just recently started Ruby (because it is such a beautiful
language)
and was just about two things (NOTE: I have a very strong Delphi
background
so the code may not be the Ruby way of ding things)

1st : How one would extract the ASCII value from an Object variable, for
example
@l = @txt[k,1]
@n = ?@l
where
@n = 0
The problem with that is that I end up merely extracting the ascii value
for
‘@’ and get errors for trying to something it with unknown ‘l’

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…

Please comment/suggest/criticise where necissary

Thanks anywho
Hiato

Hiato X. wrote:

Hello there,

I have just recently started Ruby (because it is such a beautiful
language)
and was just about two things (NOTE: I have a very strong Delphi
background
so the code may not be the Ruby way of ding things)

1st : How one would extract the ASCII value from an Object variable, for
example
@l = @txt[k,1]
@n = ?@l
where
@n = 0
The problem with that is that I end up merely extracting the ascii value
for
‘@’ and get errors for trying to something it with unknown ‘l’

anything after the ‘?’ returns an ascii value, ie ?1 returns the ascii
for 1

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…

brackets in ruby specific an array. if you wanted to say pull a single
element out of an array it would be something as follows:

@n = [1,2,3,4,5]

@n[2] # => 3 #array elements begin from 0 in ruby, so array element 2
would be 3
I dont know if this is what you were refering to, if not, elaborate.

Please comment/suggest/criticise where necissary

Thanks anywho
Hiato

On 10/30/07, Michael L. [email protected] wrote:

1st : How one would extract the ASCII value from an Object variable, for
for 1
automatically makes @n an array instead of just that single value…

Please comment/suggest/criticise where necissary

Thanks anywho
Hiato


Posted via http://www.ruby-forum.com/.

No, sorry, not quite. The thing is I want to know the ASCII value of
whatever that specific class variable is storing ie if @l = ‘t’ then I
want
the ASCII value of ‘t’, not @ which ?@l produces.

Then the thing with the array is that it is an Array of an Array, ie
Each
master array sotres a sub array as in
[1, → [4,5,6]
2, → [7,8,9]
3] → [10,11,12]

So calling array[1] would return an array, whereas array[1,2] should
return
5 but it returns an array…

@@da = Array.new(i) { Array.new(j) }
martin
AHA!

Thanks very very much!
Works like a charm

Hiato

Martin DeMello wrote:

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.

Nope. What do you think the following will output:

h = Hash.new(‘hi’)

h[‘a’] = h[‘a’] || 10
puts h[‘a’]

Now, how about the following? The same output as above?

h[‘a’] ||= 10
puts h[‘a’]

On 10/30/07, Hiato X. [email protected] 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

On 10/30/07, 7stud – [email protected] wrote:

h[‘a’] = h[‘a’] || 10
puts h[‘a’]

Now, how about the following? The same output as above?

h[‘a’] ||= 10
puts h[‘a’]

Yes, and IRB confirms it (ruby 1.8.6). AFAIK, all the a o= b operators
expand immediately to a = a o b.

martin

On Wed, 31 Oct 2007 05:58:32 +0900, 7stud – [email protected]
wrote:

h[‘a’] = h[‘a’] || 10
puts h[‘a’]

Now, how about the following? The same output as above?

h[‘a’] ||= 10
puts h[‘a’]

Actually, yes. They give the same results. Try it yourself…

-mental

On Oct 30, 2007, at 3:05 PM, MenTaLguY wrote:

Actually, yes. They give the same results. Try it yourself…

good thing too - or i’d have a LOT of code to fix :wink:

a @ http://codeforpeople.com/

On 10/30/07, 7stud – [email protected] wrote:

Martin DeMello wrote:

a ||= b is a common ruby idiom for “set a to b only if it isn’t
already defined” -

I did oversimplify this bit, I’ll admit - to be precise, due to the
semantics of the || operator, if a is nil or false, it’ll be set equal
to b.

martin

From: MenTaLguY [mailto:[email protected]]

On Wed, 31 Oct 2007 05:58:32 +0900, 7stud –

> Martin DeMello wrote:

>> 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.

> Nope. What do you think the following will output:

> h = Hash.new(‘hi’)

> h[‘a’] = h[‘a’] || 10

> puts h[‘a’]

> Now, how about the following? The same output as above?

> h[‘a’] ||= 10

> puts h[‘a’]

Actually, yes. They give the same results. Try it yourself…

i think 7stud meant this,

~> h=Hash.new(“hi”)
=> {}

~> h
=> {}

~> h[‘a’]=h[‘a’] || 10
=> “hi”

~> h
=> {“a”=>“hi”}

~> h[‘b’] ||= 10
=> “hi”

~> h
=> {“a”=>“hi”}

kind regards -botp

On 10/30/07, Peña, Botp [email protected] wrote:

=> “hi”

~> h
=> {“a”=>“hi”}

~> h[‘b’] ||= 10
=> “hi”

~> h
=> {“a”=>“hi”}

Oh, good point. I want to call that a bug, but I can’t quite put my
finger on where the exact problem is - something about [] not
returning a proper lvalue, or foo = foo not being a no-op.

martin

Martin DeMello wrote:

h[‘a’] ||= 10
puts h[‘a’]

Yes, and IRB confirms it (ruby 1.8.6). AFAIK, all the a o= b operators
expand immediately to a = a o b.

Not in this case. Try p h instead of puts h[‘a’] above and you’ll see
different results.