Quick question about how array objects are handled

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!

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 want array.delete_if

http://www.ruby-doc.org/core/classes/Array.html#M000307

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://codeforpeople.com/

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

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

Hi –

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

void

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 :slight_smile:

David

On 18 Jun., 01:52, Teleolurian [email protected] 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

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

On 18 Jun., 08:35, “David A. Black” [email protected] 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

Hi –

On Wed, 18 Jun 2008, Robert K. wrote:

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” :slight_smile: One lesson at a time…

David