[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

quick question about how array objects are handled

Chance Dinkins

6/17/2008 11:38:00 PM

Btw, thanks in advance for any help - this community seems great!

I'm using the following line to try to remove objects from an array that
begin with .
_array.each{|item| item.gsub(/^[\.][.]*/){|match|
_array.delete(match)}}

and although the match is functioning properly, the _array still
contains the values after being deleted. I've wondered if
Array.delete("param") actually returns the resulting array but that
doesnt seem to be the case. How do I go about actually altering the
state of the current array if the above is not done correctly?

Thanks for your help!
--
Posted via http://www.ruby-....

10 Answers

Teleolurian

6/17/2008 11:53:00 PM

0

I don't think there's an in place editor for delete_if, so you'd want

_array = _array.delete_if {|x| x.match(/^\./)}

Stephen Celis

6/17/2008 11:56:00 PM

0

You want array.delete_if

http://www.ruby-doc.org/core/classes/Array.ht...

On Jun 17, 2008, at 6:38 PM, Chance Dinkins wrote:

> Btw, thanks in advance for any help - this community seems great!
>
> I'm using the following line to try to remove objects from an array
> that
> begin with .
> _array.each{|item| item.gsub(/^[\.][.]*/){|match|
> _array.delete(match)}}
>
> and although the match is functioning properly, the _array still
> contains the values after being deleted. I've wondered if
> Array.delete("param") actually returns the resulting array but that
> doesnt seem to be the case. How do I go about actually altering the
> state of the current array if the above is not done correctly?
>
> Thanks for your help!
> --
> Posted via http://www.ruby-....
>


Chance Dinkins

6/18/2008 3:46:00 AM

0

Thanks guys, I apperciate it - these.. dynamic methods (is that what
they are considered?) are a little strange to me.
--
Posted via http://www.ruby-....

ara.t.howard

6/18/2008 4:19:00 AM

0


On Jun 17, 2008, at 9:46 PM, Chance Dinkins wrote:

> Thanks guys, I apperciate it - these.. dynamic methods (is that what
> they are considered?) are a little strange to me.

they are normal functions but one's which take anonymous functions
(blocks) as arguments. ruby makes this very easy on the eyes, but
even C has this concept: do a 'man qsort' and you'll see

void
qsort(void *base, size_t nel, size_t width, int (*compar)(const
void *, const void *));

note the 'compar' function you can pass in. so, in c, you have to
define that function up front and then pass a pointer to it. in ruby
you can do things like

compar = lambda{|a,b| a <=> b}

array.qsort &compar

or, more compactly

array.qsort{|a,b| a <=> b}

of course the method is called 'sort' and not 'qsort', but the
principle is exactly the same.


kind regards.

a @ http://codeforp...
--
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama




David A. Black

6/18/2008 6:33:00 AM

0

Hi --

On Wed, 18 Jun 2008, Teleolurian wrote:

> I don't think there's an in place editor for delete_if, so you'd want
>
> _array = _array.delete_if {|x| x.match(/^\./)}

delete_if operates on the array in place:

irb(main):001:0> a = [1,2,3]
=> [1, 2, 3]
irb(main):002:0> a.delete_if {|e| e == 1 }
=> [2, 3]
irb(main):003:0> a
=> [2, 3]


David

--
Rails training from David A. Black and Ruby Power and Light:
ADVANCING WITH RAILS June 16-19 Berlin
ADVANCING WITH RAILS July 21-24 Edison, NJ
See http://www.r... for details and updates!

David A. Black

6/18/2008 6:35:00 AM

0

Hi --

On Wed, 18 Jun 2008, Chance Dinkins wrote:

> Thanks guys, I apperciate it - these.. dynamic methods (is that what
> they are considered?) are a little strange to me.

The methods that expect a code block as part of the method call are
iterators. Basically the code block is a piece of executable code that
you're making available to be called from the method itself.
Typically, that means that a method will fire the code block once for
each element in a collection object like an array.


David

--
Rails training from David A. Black and Ruby Power and Light:
ADVANCING WITH RAILS June 16-19 Berlin
ADVANCING WITH RAILS July 21-24 Edison, NJ
See http://www.r... for details and updates!

David A. Black

6/18/2008 6:38:00 AM

0

Hi --

On Wed, 18 Jun 2008, ara.t.howard wrote:

>
> On Jun 17, 2008, at 9:46 PM, Chance Dinkins wrote:
>
>> Thanks guys, I apperciate it - these.. dynamic methods (is that what
>> they are considered?) are a little strange to me.
>
> they are normal functions but one's which take anonymous functions (blocks)
> as arguments. ruby makes this very easy on the eyes, but even C has this
> concept: do a 'man qsort' and you'll see
>
> void
> qsort(void *base, size_t nel, size_t width, int (*compar)(const void *,
> const void *));
>
> note the 'compar' function you can pass in. so, in c, you have to define
> that function up front and then pass a pointer to it. in ruby you can do
> things like
>
> compar = lambda{|a,b| a <=> b}
>
> array.qsort &compar
>
> or, more compactly
>
> array.qsort{|a,b| a <=> b}
>
> of course the method is called 'sort' and not 'qsort', but the principle is
> exactly the same.

I'd leave room, though, for the fact that in Ruby, you can provide a
code block to a method, as part of the syntax of the method call, but
you can also send anonymous functions in the argument list. So there's
a sense of "The Block", so to speak, separate from any functions you
might include as regular arguments. (Yes, I do know about the &block
idiom in method signatures :-)


David

--
Rails training from David A. Black and Ruby Power and Light:
ADVANCING WITH RAILS June 16-19 Berlin
ADVANCING WITH RAILS July 21-24 Edison, NJ
See http://www.r... for details and updates!

Robert Klemme

6/18/2008 9:53:00 AM

0

On 18 Jun., 08:35, "David A. Black" <dbl...@rubypal.com> wrote:
> On Wed, 18 Jun 2008, Chance Dinkins wrote:
> > Thanks guys, I apperciate it - these.. dynamic methods (is that what
> > they are considered?) are a little strange to me.
>
> The methods that expect a code block as part of the method call are
> iterators. Basically the code block is a piece of executable code that
> you're making available to be called from the method itself.
> Typically, that means that a method will fire the code block once for
> each element in a collection object like an array.

Yes, but not in all cases. The presence of the block does not tell
you anything about the frequency of invocation. Just think about
File.open for example. So I'd say the general statement "The methods
that expect a code block as part of the method call are iterators" is
misleading.

Kind regards

robert

Robert Klemme

6/18/2008 9:54:00 AM

0

On 18 Jun., 01:52, Teleolurian <teleolur...@gmail.com> wrote:
> I don't think there's an in place editor for delete_if, so you'd want
>
> _array = _array.delete_if {|x| x.match(/^\./)}

You could as well do

_array.delete_if {|s| s[0] == ?.}

Cheers

robert

David A. Black

6/18/2008 11:47:00 AM

0

Hi --

On Wed, 18 Jun 2008, Robert Klemme wrote:

> On 18 Jun., 08:35, "David A. Black" <dbl...@rubypal.com> wrote:
>> On Wed, 18 Jun 2008, Chance Dinkins wrote:
>>> Thanks guys, I apperciate it - these.. dynamic methods (is that what
>>> they are considered?) are a little strange to me.
>>
>> The methods that expect a code block as part of the method call are
>> iterators. Basically the code block is a piece of executable code that
>> you're making available to be called from the method itself.
>> Typically, that means that a method will fire the code block once for
>> each element in a collection object like an array.
>
> Yes, but not in all cases. The presence of the block does not tell
> you anything about the frequency of invocation. Just think about
> File.open for example. So I'd say the general statement "The methods
> that expect a code block as part of the method call are iterators" is
> misleading.

I said "typically" :-) One lesson at a time....


David

--
Rails training from David A. Black and Ruby Power and Light:
ADVANCING WITH RAILS June 16-19 Berlin
ADVANCING WITH RAILS July 21-24 Edison, NJ
See http://www.r... for details and updates!