[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: Array and ASCII Help needed...

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

13 Answers

7stud --

10/30/2007 8:59:00 PM

0

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']

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

MenTaLguY

10/30/2007 9:06:00 PM

0

On Wed, 31 Oct 2007 05:58:32 +0900, 7stud -- <bbxx789_05ss@yahoo.com> wrote:
> 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...

-mental


Martin DeMello

10/30/2007 9:09:00 PM

0

On 10/30/07, 7stud -- <bbxx789_05ss@yahoo.com> wrote:
> 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']

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

martin

Martin DeMello

10/30/2007 9:12:00 PM

0

On 10/30/07, 7stud -- <bbxx789_05ss@yahoo.com> 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

ara.t.howard

10/30/2007 9:29:00 PM

0


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 ;-)

a @ http://codeforp...
--
share your knowledge. it's a way to achieve immortality.
h.h. the 14th dalai lama



Peña, Botp

10/31/2007 2:06:00 AM

0

From: MenTaLguY [mailto:mental@rydia.net]
# 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

Sebastian Hungerecker

10/31/2007 3:20:00 PM

0

Martin DeMello wrote:
> On 10/30/07, 7stud -- <bbxx789_05ss@yahoo.com> wrote:
> > 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']
>
> 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.


--
Jabber: sepp2k@jabber.org
ICQ: 205544826

Martin DeMello

10/31/2007 5:14:00 PM

0

On 10/30/07, Peña, Botp <botp@delmonte-phil.com> wrote:
>
> 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"}

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

Rhino

7/30/2013 6:58:00 PM

0

On 30/07/2013 12:22 PM, David Johnston wrote:
> On 7/30/2013 6:35 AM, Rhino wrote:
>> On 2013-07-29 9:56 PM, David Johnston wrote:
>>> This was a first year episode and I liked that year better than the
>>> second when they replaced the black FBI agent with a prettier performer,
>>> a former model I think. They also changed the theme song for the worse
>>> because the performer they'd replace sang the first theme song. She was
>>> a pretty good singer and had a better voice in general than her
>>> replacement, I thought.
>>>
>>> In any case I don't remember ever seeing this particular episode, Jess
>>> goes camping and then has to call Brooke to come out and join her when
>>> she finds a derelict canoe with blood in it. The missing person has been
>>> abducted by murderous environmental activists. One of them had been
>>> involved in a fatal tree spiking. And who cares that nobody knows of
>>> any case where anyone died from tree spiking?
>>
>> I'm not clear on what show you're talking about. The only show named
>> Missing that I can recall is Ashley Judd's summer show where she played
>> a former CIA agent but it only lasted one season. I don't remember any
>> black FBI agents or characters named Brooke in that so you must be
>> talking about some other show....
>>
>
> I am. This missing was about a girl who gets inducted into the FBI
> whether she likes it or not because she has psychic dreams that give
> clues to the location of missing people. Based on a young adult series
> of novels.

I've never heard of this show. When/where does it air? Or is this a
long-cancelled show that you are just (re-)discovering? Is the title
"Missing: Deliverance from Evil"?

--
Rhino

Ian J. Ball

7/30/2013 7:02:00 PM

0

On Tuesday, July 30, 2013 5:35:32 AM UTC-7, Rhino wrote:
> On 2013-07-29 9:56 PM, David Johnston wrote:
>
> > This was a first year episode and I liked that year better than the
> > second when they replaced the black FBI agent with a prettier performer,
> > a former model I think. They also changed the theme song for the worse
> > because the performer they'd replace sang the first theme song. She was
> > a pretty good singer and had a better voice in general than her
> > replacement, I thought.
>
> I'm not clear on what show you're talking about. The only show named
> Missing that I can recall is Ashley Judd's summer show where she played
> a former CIA agent but it only lasted one season. I don't remember any
> black FBI agents or characters named Brooke in that so you must be
> talking about some other show....

This show was called "1-800-MISSING", IIRC.

And David's right - season #1 was better than season #2.

But, really, you can't go wrong with Caterina Scorsone... :)