[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

How do arrays in Ruby work?

Peter Wu

3/14/2009 2:13:00 PM

As in how they are organized behind the scenes.

For example, if I say

arr = [1, 2, 3, 4]
arr.slice[1]

Is it going to have to physically relocate everything after 2 in memory?
Would using a linked list be more efficient if a lot of elements are
going to be removed and inserted?
--
Posted via http://www.ruby-....

9 Answers

Luc Traonmilin

3/14/2009 2:38:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

slice only returns the value within the range or at index but does not
modify the original array. slice![1] would effectively remove value at index
1 from arr.
I don't know how it is implemented.

On Sat, Mar 14, 2009 at 3:12 PM, Peter Wu <pwu.380@gmail.com> wrote:

> As in how they are organized behind the scenes.
>
> For example, if I say
>
> arr = [1, 2, 3, 4]
> arr.slice[1]
>
> Is it going to have to physically relocate everything after 2 in memory?
> Would using a linked list be more efficient if a lot of elements are
> going to be removed and inserted?
> --
> Posted via http://www.ruby-....
>
>


--
Luc Traonmilin

Rick DeNatale

3/14/2009 2:44:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

On Sat, Mar 14, 2009 at 10:12 AM, Peter Wu <pwu.380@gmail.com> wrote:

> As in how they are organized behind the scenes.
>
> For example, if I say
>
> arr = [1, 2, 3, 4]
> arr.slice[1]


I think you meant either

arr.slice(1)
or
arr[1]

arr.slice[1] won't work since slice requires arguments, and this is being
interpreted as
(arr.slice)[1]

>
>
> Is it going to have to physically relocate everything after 2 in memory?
> Would using a linked list be more efficient if a lot of elements are
> going to be removed and inserted?



So far we haven't done anything which would add or remove anything

arr.slice(1) simply returns a reference to the object in the second element
of arr. arr is untouched

arr.slice(1,3) would return an array containing the second, third and fourth
elements, the original array would be untouched. AND

The MRI ruby implementation of arrays uses a copy on write scheme, so that
both arr and the returned object would actually share memory for the array
of references. If one or the other changed then a copy would be triggered.

Ruby arrays are implemented rather efficiently both in terms of space and
speed. It's usually best to use them simply and only worry about performance
issues when they arise, and when they do, it will probably be something
other than "I should have used a linked list instead of an array"


--
Rick DeNatale

Blog: http://talklikeaduck.denh...
Twitter: http://twitter.com/Ri...
WWR: http://www.workingwithrails.com/person/9021-ric...
LinkedIn: http://www.linkedin.com/in/ri...

Peter Wu

3/14/2009 4:55:00 PM

0

Rick Denatale wrote:

> So far we haven't done anything which would add or remove anything
>
> arr.slice(1) simply returns a reference to the object in the second
> element
> of arr. arr is untouched
>
> arr.slice(1,3) would return an array containing the second, third and
> fourth
> elements, the original array would be untouched.

That's my mistake, arr.slice!(1) was the intention. But I see what
you're saying.
--
Posted via http://www.ruby-....

Miguel Regedor

3/15/2009 12:30:00 PM

0

On Mar 14, 2:12 pm, Peter Wu <pwu....@gmail.com> wrote:
> As in how they are organized behind the scenes.
>
> For example, if I say
>
> arr = [1, 2, 3, 4]
> arr.slice[1]
>
> Is it going to have to physically relocate everything after 2 in memory?
> Would using a linked list be more efficient if a lot of elements are
> going to be removed and inserted?
> --
> Posted viahttp://www.ruby-....

if you have the array a = [1,2,3,4,5]

b = a.slice!(0,3)
will end up with a == [4,5] and b == [1,2,3]

b = a.slice(0,3)
will end up with a == [1,2,3,4,5] and b == [1,2,3]

for the last one you can also use b = a[0..2] , you will get the same
result

David Johnston

7/25/2014 2:35:00 AM

0

On 7/24/2014 7:39 PM, Arthur Lipscomb wrote:
> On 7/24/2014 11:10 AM, Jim G. wrote:
>> Ian J. Ball sent the following on 7/24/2014 1:06 AM:
>>> In article <lqpp5m$ir7$1@dont-email.me>,
>>> Arthur Lipscomb <arthur@alum.calberkeley.org> wrote:
>>>
>>>> On 7/23/2014 12:56 PM, Jim G. wrote:
>>>>> Obveeus sent the following on 7/23/2014 11:47 AM:
>>>>>> On 7/23/2014 11:49 AM, Ian J. Ball wrote:
>>>>>>
>>>>>>> Extant (recorded) - This show would be a lot more enjoyable if they
>>>>>>> would just show the audience everyone's cards and let things play
>>>>>>> out
>>>>>>> from there. Instead, we have no idea what the "Bad Guys" are
>>>>>>> after, and
>>>>>>> that is making the show *more annoying*, not "more
>>>>>>> suspenseful"... :|
>>>>>>
>>>>>> That style of storytelling (intentionally keeping things from viewers
>>>>>> and always putting forth more new mysteries than are answered) is
>>>>>> why I
>>>>>> quit watching.
>>>>>
>>>>> It's crazy to think that writers can set the entire table in one or
>>>>> two
>>>>> episodes (in the case of EXTANT) and then have enough things to
>>>>> resolve
>>>>> so that you have enough content to fill the rest of the season. Or to
>>>>> put it another way, when you're only two episodes into a show like
>>>>> this,
>>>>> of *course* you're still gonna be getting more new mysteries than
>>>>> resolutions.
>>>>
>>>> Exactly! Let them finish setting things up. I know people have been
>>>> burned before, but good grief. Whatever happened to patience?!?!
>>>
>>> Do both of you realize that THAT IS NOT THE ONLY WAY TO WRITE SHOWS LIKE
>>> THIS?!
>>
>> Sure, you can write it like Columbo and remove the mysteries right away
>> and then waste your time just watching Peter Falk wander around and
>> figure out what you already know, but most mystery fans don't like
>> things that way, and Columbo was an exception to the rule when it
>> managed to succeed despite itself. And all of the credit goes to Falk
>> for that, who I'll admit *was* fun to watch bumble about and
>> deliberately set up other people to underestimate him.
>>
>>> You're acting like the way they're writing "Extant" is the ONLY way you
>>> can write a show like that.
>>
>> No, but it's the best way to write a mystery.
>>
>>> Utter bullshit.
>>
>> Not really.
>>
>>> And, more to the point, every other show that has tried to write a show
>>> this way has pretty much FAILED.
>>
>> In addition to LOST, which I mention elsewhere, here's a short list off
>> the top of my head of some *current* mystery/skiffy shows that I like
>> and that have been successful enough to earn at least a second season.
>> And *NONE* of them gave all of the answers in the first two episodes,
>> and none of them stopped bringing in new mystery subplots in that short
>> amount of time:
>>
>> ARROW
>> CONTINUUM
>> GRIMM
>> HAVEN
>> HEMLOCK GROVE
>> MARVEL'S AGENTS OF S.H.I.E.L.D.
>> ORPHAN BLACK
>> PERSON OF INTEREST
>> RESURRECTION
>> SLEEPY HOLLOW
>> THE BLACKLIST
>> TRUE BLOOD
>>
>
>
> To add to the list, I was thinking Babylon 5 (yeah, I know it's not
> current). Due to behind the scenes issues, the show's protagonist
> wasn't even introduced until season 2! But it all worked for the most
> part flawlessly (at least the first 4 seasons). Imagine if by episode 2
> people were complaining the writers hadn't explained the hole in
> Sinclare's memory, what happened to B4 or why the Mimbari ended the war.

Of course interesting things were happening in the meantime that had
nothing to do with the mytharc. That isn't happening with Extant.

Obveeus

7/25/2014 3:29:00 AM

0



On 7/24/2014 9:39 PM, Arthur Lipscomb wrote:

>> Ian J. Ball sent the following on 7/24/2014 1:06 AM:

>>> But no one wants to write shows like that because it's HARDER, because
>>> the second way involves smart CHARACTER-driven writing, while the first
>>> way involves lazy, slapshod PLOT-driven writing (and "idiot plotting"),
>>> which is the bullshit the audience like you guys keeps letting them get
>>> away with. [roll]
>>
>> You like character-driven because you like soaps. I get that. But very
>> few people seem to agree with you if forced to choose. For my part, I
>> like a mix of something like 80-85% mystery/skiffy (plot-driven) stuff
>> and 15-20% soap (character-driven) stuff. And I'm guessing that you
>> expected EXTANT to be almost all soap and are unhappy with the fact that
>> it's not.

It's not? Really? Looks a lot like soap with badly written characters
doing illogical things just to further the plot.

Arthur Lipscomb

7/25/2014 5:51:00 AM

0

On 7/24/2014 7:34 PM, David Johnston wrote:
> On 7/24/2014 7:39 PM, Arthur Lipscomb wrote:
>> On 7/24/2014 11:10 AM, Jim G. wrote:
>>> Ian J. Ball sent the following on 7/24/2014 1:06 AM:
>>>> In article <lqpp5m$ir7$1@dont-email.me>,
>>>> Arthur Lipscomb <arthur@alum.calberkeley.org> wrote:
>>>>
>>>>> On 7/23/2014 12:56 PM, Jim G. wrote:
>>>>>> Obveeus sent the following on 7/23/2014 11:47 AM:
>>>>>>> On 7/23/2014 11:49 AM, Ian J. Ball wrote:
>>>>>>>
>>>>>>>> Extant (recorded) - This show would be a lot more enjoyable if they
>>>>>>>> would just show the audience everyone's cards and let things play
>>>>>>>> out
>>>>>>>> from there. Instead, we have no idea what the "Bad Guys" are
>>>>>>>> after, and
>>>>>>>> that is making the show *more annoying*, not "more
>>>>>>>> suspenseful"... :|
>>>>>>>
>>>>>>> That style of storytelling (intentionally keeping things from
>>>>>>> viewers
>>>>>>> and always putting forth more new mysteries than are answered) is
>>>>>>> why I
>>>>>>> quit watching.
>>>>>>
>>>>>> It's crazy to think that writers can set the entire table in one or
>>>>>> two
>>>>>> episodes (in the case of EXTANT) and then have enough things to
>>>>>> resolve
>>>>>> so that you have enough content to fill the rest of the season. Or to
>>>>>> put it another way, when you're only two episodes into a show like
>>>>>> this,
>>>>>> of *course* you're still gonna be getting more new mysteries than
>>>>>> resolutions.
>>>>>
>>>>> Exactly! Let them finish setting things up. I know people have been
>>>>> burned before, but good grief. Whatever happened to patience?!?!
>>>>
>>>> Do both of you realize that THAT IS NOT THE ONLY WAY TO WRITE SHOWS
>>>> LIKE
>>>> THIS?!
>>>
>>> Sure, you can write it like Columbo and remove the mysteries right away
>>> and then waste your time just watching Peter Falk wander around and
>>> figure out what you already know, but most mystery fans don't like
>>> things that way, and Columbo was an exception to the rule when it
>>> managed to succeed despite itself. And all of the credit goes to Falk
>>> for that, who I'll admit *was* fun to watch bumble about and
>>> deliberately set up other people to underestimate him.
>>>
>>>> You're acting like the way they're writing "Extant" is the ONLY way you
>>>> can write a show like that.
>>>
>>> No, but it's the best way to write a mystery.
>>>
>>>> Utter bullshit.
>>>
>>> Not really.
>>>
>>>> And, more to the point, every other show that has tried to write a show
>>>> this way has pretty much FAILED.
>>>
>>> In addition to LOST, which I mention elsewhere, here's a short list off
>>> the top of my head of some *current* mystery/skiffy shows that I like
>>> and that have been successful enough to earn at least a second season.
>>> And *NONE* of them gave all of the answers in the first two episodes,
>>> and none of them stopped bringing in new mystery subplots in that short
>>> amount of time:
>>>
>>> ARROW
>>> CONTINUUM
>>> GRIMM
>>> HAVEN
>>> HEMLOCK GROVE
>>> MARVEL'S AGENTS OF S.H.I.E.L.D.
>>> ORPHAN BLACK
>>> PERSON OF INTEREST
>>> RESURRECTION
>>> SLEEPY HOLLOW
>>> THE BLACKLIST
>>> TRUE BLOOD
>>>
>>
>>
>> To add to the list, I was thinking Babylon 5 (yeah, I know it's not
>> current). Due to behind the scenes issues, the show's protagonist
>> wasn't even introduced until season 2! But it all worked for the most
>> part flawlessly (at least the first 4 seasons). Imagine if by episode 2
>> people were complaining the writers hadn't explained the hole in
>> Sinclare's memory, what happened to B4 or why the Mimbari ended the war.
>
> Of course interesting things were happening in the meantime that had
> nothing to do with the mytharc. That isn't happening with Extant.


The first few episodes of B5 were pretty crappy if I recall correctly.
Heck, the first season, season and a half was often mediocre at best.
Now that being said, I was enjoying the show overall, sort of the same
way I enjoyed the first two seasons of TNG. But these are shows you
take as a whole with the later seasons significantly rose coloring the
glasses of the earlier seasons (well maybe not TNG). In B5's case in
particular the little arc tidbits that didn't mean anything at the time
suddenly take on a larger meaning in hindsight. I rewatched B5 about 10
years ago and enjoyed it on a whole other level that I didn't enjoy it
the first time I watched because there were so many little arc touches
that had meaning that weren't there before. Same thing when I rewatched
Buffy and Angel a few years back. I enjoyed it all so much more because
of things set up in 1 episode that I knew would pay off several episodes
later or a season later.

That's not to imply I think Extant is a great show or anything. I agree
there are some issues with the writing. But it most definitely is
holding my interest. I don't know if it's going to be a great show or
if it's going to fizzle. But I definitely want to know where it's
going. And I find the things happening with the robot kid to be
interesting that as far as we know, has nothing to do with the mytharc.

Jim G.

7/25/2014 5:38:00 PM

0

Arthur Lipscomb sent the following on 7/24/2014 8:39 PM:
> I'll give the writers the benefit of the doubt that they thought through
> the first season and know the answers to the shows mysteries. But even
> if they are giving themselves some wiggle room to add additional layers
> or elements in later seasons should the show be a success, there's
> nothing wrong with that.

Exactly. And honestly, LOST is the only show I can think of that
deliberately left out some answers, despite knowing *exactly* how much
time they had left to provide them. Other shows may have been cancelled
with little or no notice, but they've done what they could to at least
wrap up *some* things.

Bottom line: there's not a beloved (in retrospect) show out there for
anyone on this group that would have survived to become beloved if
everything had had to be spelled out after just the first two episodes.

--
Jim G. | A fan of the good and the bad, but not the mediocre
"Everyone is relevant to someone." -- Harold Finch, PERSON OF INTEREST

Jim G.

7/25/2014 10:23:00 PM

0

Obveeus sent the following on 7/24/2014 10:29 PM:
>
>
> On 7/24/2014 9:39 PM, Arthur Lipscomb wrote:
>
>>> Ian J. Ball sent the following on 7/24/2014 1:06 AM:
>
>>>> But no one wants to write shows like that because it's HARDER, because
>>>> the second way involves smart CHARACTER-driven writing, while the first
>>>> way involves lazy, slapshod PLOT-driven writing (and "idiot plotting"),
>>>> which is the bullshit the audience like you guys keeps letting them get
>>>> away with. [roll]
>>>
>>> You like character-driven because you like soaps. I get that. But very
>>> few people seem to agree with you if forced to choose. For my part, I
>>> like a mix of something like 80-85% mystery/skiffy (plot-driven) stuff
>>> and 15-20% soap (character-driven) stuff. And I'm guessing that you
>>> expected EXTANT to be almost all soap and are unhappy with the fact that
>>> it's not.
>
> It's not? Really?

No, it's not "almost all soap." It's more than 50% soap, sure, but much
less than I was expecting...which was to watch the pilot and then bail.
We have her stint in space and her pregnancy; her buddy's stint in space
and his "death"; what's going on now that they're both home; her d-bag
hubby's gig and its ties to the Asian dude who has ties to the dirty
ISEA CEO, etc. I still have episode three waiting for me, so we'll soon
see if this one survives the three-and-done hurdle. Probably sometime
this weekend...

> Looks a lot like soap with badly written characters
> doing illogical things just to further the plot.

If I didn't expect/accept some of that, I'd stop watching television
altogether.

--
Jim G. | A fan of the good and the bad, but not the mediocre
"Everyone is relevant to someone." -- Harold Finch, PERSON OF INTEREST